Link to home
Start Free TrialLog in
Avatar of Tony Giangreco
Tony GiangrecoFlag for United States of America

asked on

Can't use Folder Variables with Robocopy

I’m trying to setup an automated backup using Robocopy to a network folder with the system date used in the folder name. I currently have a system variable used to create a folder name, but when use the variable in the Robocopy destination folder, the value of the variable gets lost. Here is the batch file I’m executing:


cd\
@ECHO on 
   SETLOCAL 
   IF [%1]==[] goto s_start 

   ECHO GETDATE.cmd 
   ECHO Returns the date independent of regional settings 
   ECHO Creates the environment variables %v_year% %v_month% %v_day% 
   ECHO. 
   ECHO SYNTAX 
   ECHO       GETDATE 
   ECHO. 
   ECHO. 
   GOTO :eof 

   :s_start 

   FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO ( 
      FOR /f "tokens=2 delims= " %%A IN ('date /t') DO ( 
         SET v_first=%%G 
         SET v_second=%%H 
         SET v_third=%%I 
         SET v_all=%%A 
      ) 
   ) 

      SET %v_first%=%v_all:~0,2% 
      SET %v_second%=%v_all:~3,2% 
      SET %v_third%=%v_all:~6,4% 

      echo set month
      SET /a d1=%v_all:~0,2% 

      echo set day
      SET /a d2=%v_all:~3,2% 

      echo set year
      SET /a d3=%v_all:~6,4% 

REM Create folder w/sys date - works well
	set /a %bkup% = x:\#Bkup-%d1%_%d2%_%d3%
	md %bkup%
	md %bkup%\Docs
	robocopy.exe 
		C:\Users\TG\Documents 
		%bkup%\TG-Docs\ *.* /E /r:3 /NP 
		/XD "c:\Recycler" "C:\Users\TG\Recycler" "c:\System Volume Information" 
		/Log:x:\Log-TG-Docs-%bkup%.txt

Open in new window

Avatar of LeeDerbyshire
LeeDerbyshire
Flag of United Kingdom of Great Britain and Northern Ireland image

In the parameters passed to robocopy.exe, is %bkup%\TG-Docs\ in line 46 supposed to be the destination folder?  I don't see how it could be the source folder, since you've only just created the folder with md.  Or is something else creating it?  But if it's the destination, then the *.* is in the wrong place.  Sorry, but the line feed after C:\Users\TG\Documents at line 45 makes it a bit ambiguous.  Also, if it is the destination, then the name doesn't quite match the name in the md command at line 43 (i.e. there's a TG- missing from line 43, or it should be omitted from line 46).
There is no need for a pattern which *.* is.
put echo in front of the robocopy command and see what the command is.

The issue is the the exclusion /XD has to preceed each entry i.e.
/XD "c:\Recycler" "C:\Users\TG\Recycler" "c:\System Volume Information"  is likely invalid

/XD "c:\Recycler"  /XD "C:\Users\TG\Recycler" /XD "c:\System Volume Information"

a better option is to use
/XD "recycler" /XD "system Volume Information"

Not sure you are excluding location that are not part of the source and should never be reached.
The log is over written everytime it runs.
/LOG+:X will append data if that is what you are looking for.


What output are you getting in the log?
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tony Giangreco

ASKER

Great solution and follow through. Theapp ran perfectly on the 1st try. Thanks!!!