Question

How do I create a batch file that will copy files from a camera to a directory that is prompted from the command line?

example folder structure: {shared drive start folder} <year> <month> <(prompt user for name)> [delete pictures after copied]

I am looking to get this to work to copy pictures from various cameras to one shared folder and have it all sorted by year then month and finally by the user prompted name. I know very little command line commands, all the switches and %'s.. i get lost pretty quickly.

Windows 7 computers and most likely SD cards in readers because most of the cameras don't show up with drive letters (this is part of my problem)

The code:
Show errors:
        ECHO ON

type of file:
        SET type=jpg

to set where the files are going:
        NET USE M:\public\am_class\

to get user input i would use "SET /P" so i would do: 
        SET /P SDdrive=Enter Sd Card drive letter:

Get month and year(and day in case its needed later) to create initial folders:
        FOR /F "TOKENS=1* DELIMS= "%%A IN ('DATE/T') DO SET CDATE=%%B
        FOR /F "TOKENS=1,2 eol=/ DELIMS= "%%A IN ('DATE/T') DO SET mm=%%B
        FOR /F "TOKENS=1,2 DELIMS=/ eol=/ "%%A IN ('echo %CDATE%') DO SET dd=%%B
        FOR /F "TOKENS=2,3 DELIMS=/ "%%A IN ('echo %CDATE%') DO SET yyyy=%%B
        SET date=%mm%%dd%%yyyy%

change dir to the correct location... this im not sure about but the "NET USE" gives me a direction  to look in (figured out and placed correct code):
        cd\
        cd /DM:\public\am_class\"

make new folders in above main folder
        mkdir "%yyyy%"
        cd "%yyyy%"
        mkdir "%mm%"
        cd "%mm%"

!!next question is this correct to get it to create a folder from the user prompted info? (This is corrected and correct)
        SET /P foldername=Please enter assignment number and press enter:
        mkdir "%foldername%"
        cd "%foldername%"

Go to SDdrive:
        %SDdrive%

Find the Path of the files:
        FOR /F "TOKENS=2 DELIMS\" %%A IN ('dir /b /s *.%type%') DO SET p1= %%A

Change folder and copy to "foldername" folder
        CD"%p1%"
        COPY *.* %foldername%       

Delete the SDcard pics after complete:
        Set /P delete=Delete SD card pictures (y/n)?
        IF /I "%delete%"=="y" GOTO delY
        IF /I "%delete%"=="y" GOTO delN
        :delY
        %SDdrive%
        del /q *.*
        explorer.exe \\landis.xxx\public\am_class\%foldername%\    
        :delN
        explorer.exe \\landis.xxx\public\am_class\%foldername%\ 

Pause for testing only (will be removed in final version):
        Pause

I hope that helps some.

Était-ce utile?

La solution

Use the net use command to map the windows share to a drive like X:\ and then use

xcopy with some appropriate arguments to copy the files.

Autres conseils

Like I said in my comment, your question is very broad with a lot of things to consider. I wrote a batch-file that I have used for years on many versions of Windows to download my pictures from SD cards. It does a good job, but it does not take into account that two different SD cards may have the same picture names on them -- even though they are different pictures. It simply skips pictures that have already been downloaded with the same filename in the destination folder.

I'm not going to write your code for you, nor do I have the time right now to help you navigate your way through learning the batch-file language, but I can at least post the two batch files that I use so that you can see some of the techniques that I used to accomplish what you're trying to do.

I created a shortcut in the SendTo folder that points to the DownloadPictures.bat so that when I insert an SD card into the card reader, I can send the images folder on the SD card to the shortcut and it downloads all of the pictures, separating them into folders based on the year/month/day they were taken. Then it opens each distinct destination folder using explorer.exe. The CMD window does not show anything during the download (i.e., nothing is echoed to the window), however, the title shows the overall progress and current image filesize (e.g., "1/300 7341254 bytes").

In order to grep the year, month and day from the file dates, I use another batch-file I wrote called SubStr.bat that lets me get a substring of the output of any DOS command and assign it to a variable. You must use Delayed Command Line Expansion for everything to work.

Please remember that this is not meant to be a solution for your question, but is simply an example that shows how to do some of what you are asking -- so that you can use it as a reference as you work on your own solution.

DownloadPictures.bat

@echo off

setlocal ENABLEDELAYEDEXPANSION

cd /d c:\

title Counting the files to download...
set _total=0
for /f "delims=" %%i in ('dir %1 /a-d /b /s') do set /a _total+=1

set _cnt=0
for /f "delims=" %%i in ('dir %1 /a-d /b /s') do (
  set /a _cnt+=1
  if /I "%%~xi" neq ".ctg" (
    title !_cnt!/%_total%: %%i - %%~zi bytes
    call substr "echo %%~ti" _date 0 n n "" sub 0 10
    call substr "set _date" _year 0 n n "" end 4
    call substr "set _date" _month 0 n n "" sub 6 2
    call substr "set _date" _day 0 n n "" sub 9 2
    set _dir=Q:\Photographs\Downloads\!_year!.!_month!.!_day!
    md !_dir! > nul 2>&1
    if not defined _old_dir set _old_dir=!_dir!
    if "!_dir!" neq "!_old_dir!" (explorer !_dir!&set _old_dir=!_dir!)
    if not exist !_dir!\%%~nxi copy %%i !_dir! > nul 2>&1
  )
)
explorer !_dir!

echo All the pictures in directory %1 have been downloaded.
endlocal

SubStr.bat

@echo off
if "%7"=="" goto Help

:ExtractString
  :: Remove the first and last " and convert all "" to ".
  set __command_=%1
  set __command_=%__command_:""="%
  set __command_=%__command_:~0,-1%
  set __command_=%__command_:~1%

  :: Execute the command and direct the output to a unique file.
  %__command_% > %TEMP%\_záfileáz_

  :: Extract the specified line from the output file.  (Note: You can't use 'skip'
  :: with a value of '0'.)  I used '«' as the delimiter because it is a character
  :: that I will never encounter and this will ensure that I get the whole line
  :: returned from the 'for' statement.
  if "%3"=="0" (
    for /f "delims=«" %%i in (%TEMP%\_záfileáz_) do if not defined _závaráz_ (set _závaráz_=%%i)
  ) else (
    for /f "skip=%3 delims=«" %%i in (%TEMP%\_záfileáz_) do if not defined _závaráz_ (set _závaráz_=%%i)
  )

  if /i "%7"=="all" goto Finish
  if /i "%7"=="sub" set _závaráz_=!_závaráz_:~%8,%9!
  if /i "%7"=="end" set _závaráz_=!_závaráz_:~-%8!

:Finish
  :: Kill spaces, quotes.
  if /i "%4"=="y" set _závaráz_=%_závaráz_: =%
  if /i "%5"=="y" set _závaráz_=%_závaráz_:"=%

  :: Remove unwanted characters (pad the front with an unlikely string so that the
  :: FOR statement will never complain because of an empty set).  The %%~i notation
  :: strips quotes out of the string and spaces are delimiters.  This is why they
  :: each (spaces and quotes) have their own parameters above...
  set __remove_=%6
  set __remove_=ßa¯¦¯aß %__remove_:~1%
  set __remove_=%__remove_:~0,-1%
  set __remove_=%__remove_:""="%
  for %%i in (%__remove_%) do set _závaráz_=!_závaráz_:%%~i=!

  :: Set the output variable.
  set %2=!_závaráz_!

:Cleanup
  set _závaráz_=
  del %TEMP%\_záfileáz_
  set __command_=
  set __remove_=

goto TheEnd

:Help
echo.
echo SubStr
echo ================================================================================
echo.
echo Syntax:
echo.
echo   SubStr ["command"] [output variable] [Lines to Skip] [Remove Spaces] 
echo          [Remove Quotes] [Remove Other] [action [var1] [var2]]
echo.
echo Parameters:
echo.
echo   Command         - The command that creates the output characters.  If the
echo                     command includes calling another batch file, issue the
echo                     command by using the 'call' function
echo                     (e.g., "call another.bat").  When your command is passed,
echo                     it must be enclosed in quotes.  If part of the command needs
echo                     to also have quotes (i.e., for long filenames), the strings
echo                     within the command that need to be quoted need to be double
echo                     quoted (e.g., "dir ""c:\win 2000\notepad.exe"" /x").  When
echo                     the command is executed, the initial and final quotes will
echo                     be stripped off and all sets of double quotes ("") will be
echo                     replaced with single quotes (").
echo.
echo   Output Variable - The name of the variable to use (set var=copied text).
echo.
echo   Lines to Skip   - The number of lines before the line from which you want to
echo                     copy text.
echo.
echo   Remove Spaces   - Removes spaces out of the copied text.
echo.
echo   Remove Quotes   - Removes quotes from the copied text.
echo.
echo   Remove Other    - A string of strings that should be removed from the copied
echo                     text.  The set of strings or characters must be enclosed in
echo                     a single set of double quotes.  At times, some characters
echo                     may not be elimated (e.g., a comma) unless it too is
echo                     enclosed in quotes.  To do this, the quotes must be doubled.
echo                     For example: "a b c d "","" e f h"
echo.
echo   Action          - Action to perform:
echo                       All - Copies entire string.
echo                       Sub - Extracts part of the string where var1 is the
echo                             starting position and var2 is the number of
echo                             characters to copy. var1 is zero based.  A negative
echo                             value for var2 omits the specified number of
echo                             characters from the end of the string.
echo                       End - Only extracts the specified number of characters
echo                             from the end of the string (specified by var1).
echo.
echo Example:
echo.
echo   SubStr "dir c:\windows\system32\notepad.exe /-c" _filesize 5 y n "" sub 20 18
echo.
echo   This command assigns the size of notepad.exe to the variable _filesize.
echo   Try running it and then type 'set _' at the command prompt.
echo.
echo Notes:
echo.
echo   Make sure delayed command line expansion is enabled in the calling batch file
echo   via the "setlocal ENABLEDELAYEDEXPANSION" command, or enable it globally by
echo   editing the registry as follows (and then reopen your CMD windows):
echo.
echo     Location: HKEY_CURRENT_USER\Software\Microsoft\Command Processor
echo         Item: DelayedExpansion
echo         Type: dword
echo        Value: 1
echo.
echo ================================================================================
echo.

:TheEnd

Thank you James L and User.

James: Post 1: I though about asking some one to do it for me but then I would never learn anything from it. ignoring your telling me to the the towel in i got a working program.

Post 2: Very very useful post. Not only did it have code (i got to work no problem) you gave a description of how you used it. the first code is going to take me a while to decode what is what and how you did it. The second code it VERY HELPFUL, its well documented and i understand more of whats going on it it.

User: This ended up being what i used to get my program to work. I had to keep it simple :)

Thank you both for your help with this posted problem.

James: thank you very much for all the code and useful information that your second post was teeming with.

This is the final working code for copying pictures from an SD card to a specific user named folder sorted by year then month then project name. This is set for the SD Drive to be drive letter "F:" you can change it to what you need or delete "@SET cameradrive=F:" and remove the "REM" from the line above and it will prompt the user for the drive letter. It takes the user input and makes it all capitals to make a cleaner looking folder. One thing to note: this doesn't work if the folder name has a space in it. I have to figure out how to fix that (ill be posting a question on it here). It then opens the folder in explorer so you can verify that everything went OK. After you have checked then you can delete the files on the SD card by pressing "y" and hitting enter. If you don't want to delete them then press "n" and hit enter. This copies EVERY PICTURE on the SD card in the nested folder so if you have multiple photos you want to go different places this wont work for you. also if your pictures aren't jpg then just change the "SET type=" to what ever your file type is. I placed a program called "USB Ejector" into the root of the SD card so that when the batch file is completed it automatically safely removes the card from windows.

@ echo on
@ SET type=jpg
@ SET userfolder=C:

@cd /dy:\landis\public\am_class

@REM SET /P cameradrive=Enter Camera Drive Letter (ie D:) and hit Enter:

@SET cameradrive=F:

set /p foldername=Type Folder name and hit Enter:




@REM  This makes everything uppercase.
@ SETLOCAL ENABLEDELAYEDEXPANSION

@ SET _SAMPLE="%foldername%"

@ CALL :UCase _SAMPLE _RESULTS
@ ECHO.%_RESULTS%

@ CALL :LCase _SAMPLE _RESULTS
@ ECHO.%_RESULTS%

@ ENDLOCAL
@ GOTO:EOF


@ :LCase
@ :UCase
@ :: Converts to upper/lower case variable contents
@ :: Syntax: CALL :UCase _VAR1 _VAR2
@ :: Syntax: CALL :LCase _VAR1 _VAR2
@ :: _VAR1 = Variable NAME whose VALUE is to be converted to upper/lower case
@ :: _VAR2 = NAME of variable to hold the converted value
@ :: Note: Use variable NAMES in the CALL, not values (pass "by reference")

@ SET _UCase=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
@ SET _LCase=a b c d e f g h i j k l m n o p q r s t u v w x y z
@ SET _Lib_UCase_Tmp=!%1!
@ IF /I "%0"==":UCase" SET _Abet=%_UCase%
@ IF /I "%0"==":LCase" SET _Abet=%_LCase%
@ FOR %%Z IN (%_Abet%) DO SET _Lib_UCase_Tmp=!_Lib_UCase_Tmp:%%Z=%%Z!
@ SET %2=%_Lib_UCase_Tmp%
@REM GOTO:EOF
@REM

@ SET foldername=%_RESULTS%





@REM SETTING THE YEAR AND MONTH VARIABLES
@ SET yyyy=%date:~10,4%
@ SET mm=%date:~4,2%

@REM CREATING THE FOLDERS WITH THE NAMES AND NESTING THEM CORRECTLY
@ mkdir %yyyy%
@ cd %yyyy%
@ mkdir "MSU Orders"
@ cd "MSU Orders"
@ mkdir %mm%
@ cd %mm%
@ mkdir "%foldername%"
@ cd "%foldername%"

@REM COPY THE CONTENTS OF THE CAMREA PIC FOLDER TO THE NEW FOLDER
@ xcopy /e /v %cameradrive%\DCIM\100NIKON "\\landis\public\am_class\%yyyy%\%mm%\%foldername%\"

@ explorer.exe \\landis\public\am_class\%yyyy%\%mm%\%foldername%\

@REM Delete the originals prompt and then actions
@ SET /P delete=Delete Original Photos from Camera (y/n)?
@ IF /I %delete%==y GOTO :dely
@ IF /I %delete%==n GOTO :deln
@ GOTO :deln
@ :dely
@ cd /d%cameradrive%\DCIM\100NIKON
@ del /q *.*

@ GOTO :done

@ :deln
@ GOTO :done



@ :done
@ cd /dc:\
@ F:\USB_Disk_Eject.exe /removethis
@ pause
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top