We help IT Professionals succeed at work.

# RoboCopy behavior

on
Hello All,

I am struggling with what i thought would be a simple RoboCopy script. However, I am getting some very odd behavior I have never encountered  before and I am at my wits end.

A bit about the environment the to locations are 2 server 2016 vms and are on the same subnet. Source server is a production file server and destination server test bed for program that a department is considering using. The source location contains sub folders and images mostly with about 55gb of used space. The script is triggered via a scheduled task.

@echo Off
set DirDate=%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
set Name1=IMG_DB_Sync_"%DirDate%"
set Name3=IMG_DB_RoboCopy"%DirDate%"
echo ====%time% - Parameters have been set==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log ; echo ====%time% - RoboCopy Starting for Image DataBase Sync==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log
@echo Off
Robocopy /mir /S /j /R:5 /W:15 /MT:8 "\\Source_Serverr\SharedFolder$\!IMAGE DATABASE" "‪\\Dest_Server\SharedFolder$\!IMAGE DATABASE" /xf ".DS_Store" /LOG:\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name3%.log /v /ts /fp echo ====%time% - RoboCopy for IMG_DB Sync Finished==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log
;
@echo Off
echo ====%time% - Sending IMG_DB_Sync_RoboCopy log==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log echo .. powershell.exe -command "& {Send-MailMessage -SmtpServer 'smtp.server.com' -From 'foldersync@domain.com' -To 'IT@domain.com' -Subject 'File Sync' -Body 'Please see attached log for details' -Attachments '\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name3%.log'}"
echo ====%time% - Sent IMG_DB_RoboCopy log file==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log ; @echo Off echo ====%time% - Log Cleanup Starting. ==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log
ForFiles /p "\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS" /s /d -30 /c "cmd /c del @file & echo @path" >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log
echo: >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log echo ====%time% - Log Cleanup Completed. ==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log
echo ..
;
@echo Off
echo ====%time% - Image DataBase has been synced==== >>\\Source_Server\it_share$\Script_REPO\IMG_DB_Sync\LOGS\%Name1%.log echo .. ; End  Now to the meat of my issue. No matter how i run this script or if i just run the robocopy command direct from a cmd window I encounter this issue. When the robocopy job is running it appears to be creating a directory in "C:\Windows\System32\ΓÇ¬" with folder structure of the destination portion of the the command followed by the folder and file structure of the data we are trying to copy. So it reads "C:\Windows\System32\ΓÇ¬\Dest_Server\SharedFolder$\!IMAGE DATABASE"

The issue come into play is when this is happening it is filling the c drive as it is only a 40gb drive for just the OS. Instead of increasing the drive size I would like to see if there a reason\solution for this behavior.

Thanks in advanced for looking at this.
Comment
Watch Question

## View Solution Only

Expert of the Year 2019
Top Expert 2016
Commented:
Okay, as far as the problem you are having, there are actually some funny characters in your file.  When I did a SELECT ALL on the code snippet you posted in the question, and placed it into an editor here I could see that there was something funny on the ROBOCOPY line.  Below is a hex dump of the start of the BAT file I saved your code as, and notice that the same three characters that you are seeing show up there in the destination parm to the ROBOCOPY command (highlighted).  So not sure how they got there, but that's why you are seeing the destination going to the C: drive.  I expect that C:\Windows\System32\ was the current directory when the BAT ran (maybe scheduled) and the funny character(s) were taken as a folder name relative to that.  So cleaning that up should fix your problem.

That being said, I also would suggest the adjustments I made in the version below.  Using SET variables is always a good idea in scripts, and can really help in a lot of ways.  It helps eliminate typing errors, it clearly names long file paths and names so that when they are referenced in the script you know what their purpose / usage it, and it makes the script a lot cleaner and more readable.  Maintenance becomes a lot easier too, since if a path or filename changes you may be able to change it in one places instead of many.  I also did a little cleaning up on a few other items as well - many of us BAT experts here recommend not adding quotes in the SET strings, but rather always quoting those path variables when they are used in the script code.  It is more consistent and typically works out better.  You only needed the ECHO OFF once at the start of the script, and not sure what the semi-colon lines were, or some of the other ECHO commands, but I left some of those.  Hope this helps, questions welcome.

@echo Off
setlocal

set SourceDir=\\Source_Serverr\SharedFolder$\!IMAGE DATABASE set DestDir=\\Dest_Server\SharedFolder$\!IMAGE DATABASE

set DirDate=%date:~-10,2%.%date:~-7,2%.%date:~-4,4%
set LogDir=\\Source_Server\it_share\$\Script_REPO\IMG_DB_Sync\LOGS
set LogFile1=%LogDir%\IMG_DB_Sync_%DirDate%.log
set LogFile3=%LogDir%\IMG_DB_RoboCopy%DirDate%.log

echo ====%time% - Parameters have been set==== >>"%LogFile1%"

echo ====%time% - RoboCopy Starting for Image DataBase Sync==== >>"%LogFile1%"
Robocopy /mir /S /j /R:5 /W:15 /MT:8 "%SourceDir%" "%DestDir%" /xf ".DS_Store" /LOG:"%LogFile3%" /v /ts /fp
echo ====%time% - RoboCopy for IMG_DB Sync Finished==== >>"%LogFile1%"

echo ====%time% - Sending IMG_DB_Sync_RoboCopy log==== >>"%LogFile1%"
echo ..
powershell.exe -command "& {Send-MailMessage -SmtpServer 'smtp.server.com' -From 'foldersync@domain.com' -To 'IT@domain.com' -Subject 'File Sync' -Body 'Please see attached log for details' -Attachments '%LogFile3%'}"
echo ====%time% - Sent IMG_DB_RoboCopy log file==== >>"%LogFile1%"

echo ====%time% - Log Cleanup Starting. ==== >>"%LogFile1%"
ForFiles /p "%LogDir%" /s /d -30 /c "cmd /c del @file & echo @path" >>"%LogFile1%"
echo: >>"%LogFile1%"
echo ====%time% - Log Cleanup Completed. ==== >>"%LogFile1%"
echo ..

echo ====%time% - Image DataBase has been synced==== >>"%LogFile1%"
echo ..


»bp

Commented:
Bill,

I just wanted to say thank you for looking at this. I am using all your adjustments you made and its working exactly as I would expect.  I would have never thought to look at the bat file in hex. I'm not sure how or why those three characters ended up in there. Additionally your other tips and pointers are very helpful and will use them moving forward.

Out of curiosity, what editor are you using? I wrote the original bat file in Notepad++.

Again thank you for looking at this.