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

LVL 25
Tony GiangrecoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LeeDerbyshireCommented:
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).
arnoldCommented:
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?
oBdACommented:
Where to start?
set /a %bkup% = x:\#Bkup-%d1%_%d2%_%d3%
1. You're using the arithmetic assignment here; you're probably getting an error about a missing operand. If not, the variable would be 0, because the interpreter will treat any strings as variables.
2. You're not assigning the variable "bkup" a value, you're assigning a value to the expanded content of the variable "bkup".
3. Even without the % signs around it, you'd assign the variable "bkup " (including the trailing space!) a value (with a leading space!), because of the spaces around the equal sign. You'd need to use "%bkup %" later, not "%bkup%".
md %bkup%
Double quotes around the path are missing; if %bkup% (which has a rather undefined value, see above) contains a space, the command will fail. In addition, it's unnecessary anyway, since robocopy will create the target folder if it doesn't exist.
md %bkup%\Docs
See above, and this command would make the first one redundant as well; "md" will create the complete tree specified.
And here's the main reason for the failure:
robocopy.exe
      C:\Users\TG\Documents

A batch script is read and executed line by line; your script is running the command "robocopy.exe" (which won't do anything without arguments), then the command "C:\Users\TG\Documents", which isn't a program, etc.
You can't just add spaces or line breaks to a batch script just for formatting reasons, the interpreter can and will be very literal-minded.

Try the script below instead; I've used a different method to retrieve the date, and rearranged the time stamp to Year_Month_Date, which creates a folder structure that sorts by date by default. If you feel like it, you can re-order that again in line 13.

@echo off
setlocal enabledelayedexpansion
REM *** Get the current date (and time) information, independent of the format settings.
REM *** Runs on XP, Win7, W2k3, W2k8, W2k8R2 (and probably Vista)
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Day DayOfWeek Hour Minute Month Quarter Second WeekInMonth) do (if !%%a! LSS 10 set %%a=0!%%a!)
REM *** At this point, the variables Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
set TimeStamp=%Year%_%Month%_%Day%
set Source=C:\Users\TG\Documents
set Target=X:\#Bkup-%TimeStamp%\TG-Docs
set LogFile=X:\Log-TG-Docs-%TimeStamp%.txt
robocopy.exe "%Source%" "%Target%" *.* /E /r:3 /NP /XD "C:\Recycler" "C:\Users\TG\Recycler" "C:\System Volume Information" /Log:"%LogFile%"

Open in new window


arnold,
/XD can be followed by any number of folder paths; after /XD, all following paths are assumed to be excluded until the next option (starting with "/") is found.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tony GiangrecoAuthor Commented:
Great solution and follow through. Theapp ran perfectly on the 1st try. Thanks!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.