Help w. Creating a DOS Copy Command

Discussion in 'Software' started by swalsh19, Dec 19, 2006.

  1. swalsh19

    swalsh19 Private First Class

    It has been awhile since I've done some DOS batch programming. I would like to create a simply batch file that would copy files to a USB backup drive. However I would like the command to be able to check to see if the file has been updated or not instead of copying all the files each time.

    Is this at all possible?
     
  2. Ken3

    Ken3 MajorGeek

    Yes it's possible. Use the XCOPY command with the /D switch. Look at the help for XCOPY. Help for the switch is as follows ... "Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time."

    Here's an example that I use for backing up My Documents to a network drive. NOTE: I had to use the SUBST command in order to the XCOPY to work right -- don't know why but it works.

    @ECHO OFF
    REM [Xcopy ALL files from My Documents to H: excluding files contained in Copy_ALL_EXCLUDE.txt
    subst E: "%userprofile%\My Documents"
    REM pause
    @echo ------------- >> E:\Backups\Copy_ALL_Backup.log
    @echo %date% %time% >> E:\Backups\Copy_ALL_Backup.log
    @echo ------------- >> E:\Backups\Copy_ALL_Backup.log
    echo Copying files ...
    xcopy E:\*.* "H:\My Documents\" /D/S/E/Y/EXCLUDE:E:\Backups\Copy_ALL_EXCLUDE.TXT >>E:\Backups\Copy_ALL_Backup.log
    subst E: /D
    exit

    As you can see, I also create a quick and dirty log file of what's been backed up.

    Hope this helps.
     
  3. swalsh19

    swalsh19 Private First Class

    Ken,

    Thank-you this is PERFECT!

    :D :D :D
     
  4. swalsh19

    swalsh19 Private First Class

    Ken,

    OK so this is what I made derived primarily from the above.

    @ECHO OFF
    REM pause
    Erase c:\test\Backup.log
    @echo ------------- >> c:\test\Backup.log
    @echo %date% %time% >> c:\test\Backup.log
    @echo ------------- >> c:\test\Backup.log
    echo Copying files ...
    xcopy c:\Test\*.* "d:\Test\" /D/S/E/Y/EXCLUDE:c:\test\Backup.log >>c:\test\Backup.log
    erase d:\test\Backup.log

    pause
    exit


    Now I had to add the line -> erase d:\test\Backup.log because it was copying over this file to the destination. I don't want this file copied. Also in the log would it be possible to NOT have the Backup.log file listed as an updated file?
     
  5. Ken3

    Ken3 MajorGeek

    Just change the locations/name of the folder for the BACKUP.LOG file. You can place the batch job in a different location/folder as well. Also the /EXCLUDE switch uses a file, BACKUP_EXCLUDE (whatever you want to call it) containing a list of those files and folders you don't want to have backed up (you can use wildcards too; and each on a separate line). So from your derivation....

    @ECHO OFF
    REM pause
    REM Erase c:\test\Backup.log [I added "REM" since the command won't be necessary]
    @echo ------------- >> c:\batchjob\Backup.log
    @echo %date% %time% >> c:\batchjob\Backup.log
    @echo ------------- >> c:\batchjob\Backup.log
    echo Copying files ...
    xcopy c:\Test\*.* "d:\Test\" /D/S/E/Y/EXCLUDE:c:\batchjob\Backup_exclude.txt >>c:\batchjob\Backup.log
    REM erase d:\test\Backup.log

    If you don't need to exclude anything remove the part that's underlined.

    I made these changes so it's clear to me (and hopefully you and anyone else) what each item is.

    Hope this clarifies it for you. :)

    I'll be in and out of this board until Friday (22nd) and may not get in here through the holidays. Hopefully I've answered most of your questions.
     
  6. Ken3

    Ken3 MajorGeek

    Neet to clarify something on the /Exclude switch. This is how it works with Windows 2000 (I'm using at work). Since you don't state which OS, I assumed 2000. If using XP then what you done may be correct (I looked at some help information on XP for XCOPY). Experiment and enjoy :)
     
  7. Ken3

    Ken3 MajorGeek

    Thanks to Theefool, there's an extra switch that didn't need to be there. Remove the "/s" - that doesn't copy over "empty" folders whereas "/e" does.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds