The company I work at utilizes a batch file to backup user profiles. The script originally used robocopy, but now also uses xcopy in some cases. We run this script manually when re-imaging users to copy all of their files/folders, as well as some customizations, to their personal network drive. We then can access this information on their new laptop and run the script again to re-apply everything to their new machine.
We noticed over hundreds of uses that occasionally this script would just not backup either the user's Desktop or Documents files and/or folders. We cannot find a consistent variable that causes this hiccup in our script though, and I have been tasked with updating the batch script so that we can eliminate this issue.
My thought was to include a check of sorts into the program to ensure that everything was copied over, and if not to run the backup of that specific destination again. To do this I run the following lines to grab an image of the file size of that folder, and all of it's sub-folders:
set backupdesktopsize=.set secondline=.
@FOR /F "tokens=1 delims=^^" %%G IN ('dir "%BackupDestination%\Desktop" /s /a') DO (set backupdesktopsize=!secondline!set secondline=%%G)
I am then able to echo !backupdesktopsize!
and get:
x Files(s) xxx,xxx bytes
So I run this script for both the original Desktop location found on the local drive of the machine, and then I run it again for the backup destination on the personal network drive. I can then echo each individual variable and confirm that they are both returning the data I am looking for. I am also able to check the properties of the folders themselves and confirm that what my program is running is gathering the correct data.
The issue then comes in when I attempt to run a boolean to compare the two variables that I have created. As it stands my code for this is currently:
if "!desktopsize!" == "!backupdesktopsize!" (echo The backup of the Desktop file(s) and folder(s) was a success!) else (echo The backup of the Desktop file(s) and folder(s) did not match. RoboCopy will run the Desktop back up again.)
When the program gets to this point it crashes without error every time.
I know that my problem is within the boolean itself, but I am unable to understand what I am missing. I found another post here where a user had a similar issue and found out it was the white space in the string that was causing issues when comparing. So, I then used the code he implemented to remove the white space from the strings I had with this:
set backupdesktopsize=%backupdesktopsize: =%
And this does work to change what was previously echoed to be the following instead.
xFiles(s)xxx,xxxbytes
Still, when attempting to run the boolean the program continues to crash. I have played around with changing the syntax of the boolean a lot, and looked around online, but cannot figure out what I am missing. I would really appreciate it if anyone would be able to offer some solutions, or alternatives for how I can build some redundancy into this script.
While it is tempting to condense the script, and have the echo
on a single line, this is Windows batch -- the usual guidelines for style & good taste do not apply.
You should not have this nested if/else echo
on a single line.
This is better:
if "!desktopsize!" == "!backupdesktopsize!" echo The backup of the Desktop file(s) and folder(s) was a success!
if not "!desktopsize!" == "!backupdesktopsize!" echo The backup of the Desktop file(s) and folder(s) did not match. RoboCopy will run the Desktop back up again.
When taking multiple actions, use appropriate goto
s:
if "!desktopsize!" == "!backupdesktopsize!" goto success
REM -- Otherwise, not the same, skip down to the error condition
goto error
:error
echo The backup of the Desktop file(s) and folder(s) did not match. RoboCopy will run the Desktop back up again.
REM -- Take action here, or go back and repeat from a previous label
goto end
:success
echo The backup of the Desktop file(s) and folder(s) was a success!
REM -- Good to go!
goto end
:end