Help with a Xcopy Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
creeky
Posts: 7
Joined: 18 May 2013 11:56

Help with a Xcopy Batch File

#1 Post by creeky » 18 May 2013 12:05

Hi all,
First post, I am trying to create a scheduled back up using a batch file. I have created the code and it backs up OK. I have added the /D parameter to prevent long running back ups as not all the data will have changed. This Parameter refuses to work. I dont know what I am doing wrong. I copied the code from a website, Modded the file destinations. The first test seemed to work ok on a short test. But when I put in the full file paths and run the second time, the back up looks at all the files. Below is the file. Can you give me some help please?
Thanks in advance

@echo off
:: variables
set drive=z:\Dannys Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

echo ### Backing up My Documents...
%backupcmd% "%USERPROFILE%\Documents" "%drive%\My Documents"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\Desktop"
%backupcmd% "%USERPROFILE%\Downloads" "%drive%\Downloads"
%backupcmd% "%USERPROFILE%\Music" "%drive%\Music"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\Videos"
%backupcmd% "%USERPROFILE%\Office and Visio exe's" "%drive%\Office and Visio exe's"


echo Backup Complete!
@pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a Xcopy Batch File

#2 Post by foxidrive » 18 May 2013 23:15

If you use Vista or later then you also have a tool called Robocopy.exe and it is downloadable for XP as well.

The copy subroutine accepts the two source and destination folders and is the command I use to mirror directory trees.
It doesn't copy files that already exist, and it deletes files that are no longer on the source - thus creating a mirror backup.

I think that /DST is a new switch so if your Robocopy doesn't support /DST to ignore daylight saving time changes,
then remove the /DST - it will fail to run if it doesn't like the command line.

Code: Select all

@echo off
:: variables
set "drive=z:\Dannys Backup"
set "log=%drive%\backuplog.txt"

echo ### Backing up My Documents...
call :copy "%USERPROFILE%\Documents" "%drive%\My Documents"
call :copy "%USERPROFILE%\Desktop" "%drive%\Desktop"
call :copy "%USERPROFILE%\Downloads" "%drive%\Downloads"
call :copy "%USERPROFILE%\Music" "%drive%\Music"
call :copy "%USERPROFILE%\Videos" "%drive%\Videos"
call :copy "%USERPROFILE%\Office and Visio exe's" "%drive%\Office and Visio exe's"

echo Backup Complete!
pause
goto :eof

:copy
robocopy %1 %2 /DST /FFT /R:0 /MIR /NP /TEE /LOG+:"%log%" /FP /NDL /NJH /NJS

creeky
Posts: 7
Joined: 18 May 2013 11:56

Re: Help with a Xcopy Batch File

#3 Post by creeky » 19 May 2013 14:19

Hi Foxidrive,
Thank you very much for your help, Code works great. FYI I am running win 8, so DST switch works. I previously had separate back up batch files for 3 different users. But this makes sense to have just one. I have had to look at some folders individually (instead of the User Directory all at once) as the mirror copy the Temp Internet Files all the time as these change.

The only thing two I would like to add, If Possible are 1, To Time stamp the last back up made in the log, As this will be scheduled. 2, Or If possible to email me to notify back up has been completed.

I don't know if you would like to help, and it maybe cheeky me asking, so If not no worries.

Thanks again for your help.

Creeky.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a Xcopy Batch File

#4 Post by foxidrive » 19 May 2013 14:24

I'm glad it works for you too.

Regarding the log and timestamp - do you want a timestamped log file, one for each time it runs?


BTW, you can exclude folders by adding them to the end of the robocopy command. Adding this will exclude that folder whenever it encounters it.

/XD "temporary internet files"

similarly you can exclude files and include multiple exclusions.

/XF "c:\*.sys" /XF "*.MKV" /XF "*.mp3" etc

creeky
Posts: 7
Joined: 18 May 2013 11:56

Re: Help with a Xcopy Batch File

#5 Post by creeky » 19 May 2013 14:36

Hi,
Yes I would like to ensure the scheduled back up is carried out successfully. If this is too much work, I would just like a time 'stamp to tell me the last time the back up was run.So I don't have to check the mirror file sizes to confirm.

Thanks

Creeky

creeky
Posts: 7
Joined: 18 May 2013 11:56

Re: Help with a Xcopy Batch File

#6 Post by creeky » 19 May 2013 14:36

PS, thanks for the switches

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a Xcopy Batch File

#7 Post by foxidrive » 19 May 2013 23:35

This includes a timestamped .txt log file for each launch, as well as a timestamped .txt.log file with the success or otherwise of each copy operation.

Code: Select all

@echo off
:: create a timestamp YYYY-MM-DD_HH-MM-SS
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%

:: variables
set "drive=z:\Dannys Backup"
set "log=%drive%\backuplog-%dt%.txt"

echo ### Backing up My Documents...
call :copy "%USERPROFILE%\Documents" "%drive%\My Documents"
call :copy "%USERPROFILE%\Desktop" "%drive%\Desktop"
call :copy "%USERPROFILE%\Downloads" "%drive%\Downloads"
call :copy "%USERPROFILE%\Music" "%drive%\Music"
call :copy "%USERPROFILE%\Videos" "%drive%\Videos"
call :copy "%USERPROFILE%\Office and Visio exe's" "%drive%\Office and Visio exe's"

echo Backup Complete!
pause
goto :eof

:copy
robocopy %1 %2 /DST /FFT /R:0 /MIR /NP /TEE /LOG+:"%log%" /FP /NDL /NJH /NJS

if errorlevel 16 >>"%log%.log" echo ***FATAL ERROR*** --- copying %1 %2 & goto :eof
if errorlevel 8  >>"%log%.log" echo **FAILED COPIES** --- copying %1 %2 & goto :eof
if errorlevel 4  >>"%log%.log" echo *MISMATCHES* --- copying %1 %2    & goto :eof
if errorlevel 2  >>"%log%.log" echo EXTRA FILES --- copying %1 %2      & goto :eof
if errorlevel 1  >>"%log%.log" echo Copy successful --- copying %1 %2   & goto :eof
if errorlevel 0  >>"%log%.log" echo --no change-- --- copying %1 %2    & goto :eof


creeky
Posts: 7
Joined: 18 May 2013 11:56

Re: Help with a Xcopy Batch File

#8 Post by creeky » 20 May 2013 01:34

Thank you very much!
I will try tonight.

creeky
Posts: 7
Joined: 18 May 2013 11:56

Re: Help with a Xcopy Batch File

#9 Post by creeky » 04 Jun 2013 12:53

Right I'm Back.
I have managed to get a bit of time to try and get this right. Which I think I have now (Set Up Wise). I now have 3 batch files, One for each user account. These files are placed in the users start up folder to allow a daily back up when the PC is started and the user logs on. The PC shuts down at 02:00 every morning.
The only issue I have now is that One Batch File (my account) work perfect. but the other two accounts report a error no.5 when asked to back up the current users document folders. It seems to be a weird fault as when backing up the documents folder Robocopy creates 'My Pictures' My Videos' and 'My Music' folders within the My Documents folder even though they do not exist in the original source folder. After creating them the follow error occurs
2013/06/01 23:00:03 ERROR 5 (0x00000005) Time-Stamping Destination Directory z:\Dannys Backup\Molly User Folder\My Documents\My Music\
Access is denied.
2013/06/01 23:00:03 ERROR 5 (0x00000005) Time-Stamping Destination Directory z:\Dannys Backup\Molly User Folder\My Documents\My Pictures\
Access is denied.
2013/06/01 23:00:03 ERROR 5 (0x00000005) Time-Stamping Destination Directory z:\Dannys Backup\Molly User Folder\My Documents\My Videos\
Access is denied.

The Code appears to be exactly the same as my batch file (copied and pasted several times to confirm) but the error still exists.
The batch file is run from the users accounts when they log on and NOT from mine.
Below is the code, Can you check just incase I have missed something please?

Code: Select all

@echo off
:: create a timestamp YYYY-MM-DD_HH-MM-SS
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set dt=%%a
set dt=%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%

:: variables
set "drive=z:\Dannys Backup"
set "log=%drive%\Molly User Folder\backuplog-%dt%.txt"

echo ### Backing up Computer...
echo ### Starting in 30 Seconds, Exit to stop.
timeout 30

echo ### Backing up Computer Files...
call :copy "%USERPROFILE%\Documents" "%drive%\Molly User Folder\Documents"
call :copy "%USERPROFILE%\Desktop" "%drive%\Molly User Folder\Desktop"
call :copy "%USERPROFILE%\Downloads" "%drive%\Molly User Folder\Downloads"
call :copy "%USERPROFILE%\Music" "%drive%\Molly User Folder\Music"
call :copy "%USERPROFILE%\Videos" "%drive%\Molly User Folder\Videos"
call :copy "%USERPROFILE%\Pictures" "%drive%\Molly User Folder\Pictures"
call :copy "%USERPROFILE%\Contacts" "%drive%\Molly User Folder\Contacts"
call :copy "%USERPROFILE%\Favorites" "%drive%\Molly User Folder\Favorites"



echo Backup Complete!
pause
timeout 10

goto :eof

:copy
robocopy %1 %2 /DST /FFT /R:0 /MIR /NP /TEE /LOG+:"%log%" /FP /NDL /NJH /NJS /XF "*.ini" /XF "*.ithmb" /XF "*.itc2"

if errorlevel 16 >>"%log%.log" echo ***IN USE OR WRITES ISSUE*** --- copying %1 %2 & goto :eof
if errorlevel 8  >>"%log%.log" echo **FAILED COPIES** --- copying %1 %2 & goto :eof
if errorlevel 4  >>"%log%.log" echo *MISMATCHES* --- copying %1 %2    & goto :eof
if errorlevel 2  >>"%log%.log" echo EXTRA FILES --- copying %1 %2      & goto :eof
if errorlevel 1  >>"%log%.log" echo Copy successful --- copying %1 %2   & goto :eof
if errorlevel 0  >>"%log%.log" echo --no change-- --- copying %1 %2    & goto :eof



Thanks in advance
Just to add; If I comment out the first Call Copy line, The batch file runs perfect with no errors.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Help with a Xcopy Batch File

#10 Post by Squashman » 04 Jun 2013 13:12

Do they have the proper NTFS permissions to access this folder?
z:\Dannys Backup

creeky
Posts: 7
Joined: 18 May 2013 11:56

Re: Help with a Xcopy Batch File

#11 Post by creeky » 04 Jun 2013 15:19

Hi,
Thanks for the reply, I would presume so as robocopy copies the rest of the folder over OK.
The Back up completes OK it just it reports this error. Which isn't too much of an issue. Although I will be checking the logs regularly to ensure there are no errors.
I have checked permissions and found all looks OK. Weirdly (although I am pretty sure I did not change anything) when I now run both batch files, I do not get any errors during the back up process.
But when its finished and I check the Log file it show the following
**FAILED COPIES** --- copying "C:\Users\Mummy\Documents" "z:\Dannys Backup\Mummy User Folder\Documents"
If I access the other change Log nothing is shown.

How can I find out what is driving the error now?

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a Xcopy Batch File

#12 Post by foxidrive » 04 Jun 2013 22:36

I also get errors and it would seem to be a Robocopy bug on these folders, which are LNKs in the source folder.

Just ignore it.

Code: Select all

2013/06/05 14:33:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory d:\abc\a\My Music\
Access is denied.

2013/06/05 14:33:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory d:\abc\a\My Pictures\
Access is denied.

2013/06/05 14:33:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory d:\abc\a\My Videos\
Access is denied.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Help with a Xcopy Batch File

#13 Post by Ocalabob » 05 Jun 2013 12:44

foxidrive wrote:I also get errors and it would seem to be a Robocopy bug on these folders, which are LNKs in the source folder.

Just ignore it.

foxidrive,
Did you try using the /XJ switch?

/XJ :: eXclude Junction points. (normally included by default).

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with a Xcopy Batch File

#14 Post by foxidrive » 05 Jun 2013 13:59

Ocalabob wrote:
foxidrive wrote:I also get errors and it would seem to be a Robocopy bug on these folders, which are LNKs in the source folder.

Just ignore it.

foxidrive,
Did you try using the /XJ switch?

/XJ :: eXclude Junction points. (normally included by default).


Good solution there Ocalabob - it solves the error.

TechnoMage
Posts: 4
Joined: 05 Jun 2013 18:22
Location: Central Florida

Re: Help with a Xcopy Batch File

#15 Post by TechnoMage » 05 Jun 2013 18:51

Hi Guys! Newcomer here!

I too back up all my data files every day that I post any update to them, add new pictures, etc.

A simple one-liner in a batch file does most of the work.

xcopy "C:\Documents and Settings\Alexi\My Documents\*.*" "M:\My Documents\" /S /Y /H /R /D

In the above example, drive M is a Toshiba, USB 3, external 1TB hard drive.
I also have a 32 gig, USB 3, Flash Drive permanently plugged into my USB 3.0 card, for an additional backup location.

I added additional lines for other data folders that I also want to update, like saved EMail, My Wordperfect files, my Firefox Bookmarks, etc.
I've used XCOPY since the DOS days and find it fast, efficient and very easy to program.

Since XCOPY is programmed to only back up new files, or files that have been changed since the last backup,
my daily backup takes only a few seconds.
I can run my backup batch file manually, or from my Startup Folder or from a Shutdown Batch file containing my
shutdown command. Here's that command:

%windir%\System32\shutdown.exe -s -t 00 -f

Long Live DOS!
TechnoMage 8)

Post Reply