0

I am using robocopy to copy from one directory to another.

I have tested the code using miscellaneous source directories but I get an error when executing one particular directory:

set "source=C:\Program Files (x86)\Phoresis\Backup"

I get the error:

Invalid paramter 3 "(x86)\Phoresis\Backup"

Have tried using single quotes and have wrapped the variable assignments in "" as there are spaces in the directory paths.

Code:

set "newDirectory=F:\Cap2 Flex Backup"

REM Timesatmp a new folder
set "DirName=%date:~-4,4%.%date:~-7,2%.%date:~0,2%.%time:~0,2%.%time:~3,2%
MD \%newDirectory%"

set "source=C:\Program Files (x86)\Phoresis\Backup"

robocopy %source% %DirName% /e /z /Mir

Struggling to get my head around spaces/special characters in vb scripts/batch so any input appreciated.

Cheers

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
dancingbush
  • 1,961
  • 4
  • 23
  • 51
  • 1
    1. change your `robocopy` command line to `robocopy "%source%" "%DirName%" /e /z /Mir`; 2. what is the `MD` command line for? ; you create a directory but `robocopy` does not use it; and there is a leading `\\`` which should be replaced by `"`; – aschipfl Dec 02 '15 at 17:35
  • Ok will try, why do I need qoutes surrounding these variables,? – dancingbush Dec 02 '15 at 17:37
  • Because the stored paths contain spaces, which are interpreted as argument separators, unless an argument is enclosed in `""`... – aschipfl Dec 02 '15 at 17:39
  • Does wrapping the variable with "" at declaration not do this – dancingbush Dec 02 '15 at 17:41
  • 1
    No; `set "VAR=a string"` assigns `a string` to variable `VAR`, without any quotes; the quotes just avoid problems with special characters like `&`, `^`, `(`, `)`, _SPACE_,...; if no such characters appear, it is the same as `set VAR=a string`; so the quoted version is safe whereas the unquoted one isn't; but when expanding (reading) a variable, you have to take care of such characters again; in your case, without the surrounding quotes, the _SPACE_ is interpreted as argument separator, so `C:\Program`, `Files` and `(x86)\Phoresis\Backup` are three arguments; – aschipfl Dec 02 '15 at 17:51
  • Thanks very useful info I will use in Future- will modify code and update u – dancingbush Dec 02 '15 at 19:44
  • Please clarify what you want to do with the `MD` command; should it create the destination root directory, which contains then the timestamp folder of `DirName`? once I got the intention, I'll post an answer with all the information... – aschipfl Dec 02 '15 at 19:49
  • Yes that's correct the idea is to create a new directory containing a time stamped folder (DirName) every time the code executes – dancingbush Dec 02 '15 at 19:52

1 Answers1

0

The main issue in your code is, that there are no quotes "" around the source and destination directories in the robocopy command line. Since they contain spaces, such are interpreted as argument separators; so %source%, which is expanded to the value C:\Program Files (x86)\Phoresis\Backup, will be interpreted as three arguments C:\Program, Files and (x86)\Phoresis\Backup. To avoid that, put "" around the path arguments like "%source%".

Here is the fixed code:

set "newDirectory=F:\Cap2 Flex Backup"
md "%newDirectory%"

rem Timesatmp a new folder
set "DirName=%newDirectory%\%date:~-4,4%.%date:~-7,2%.%date:~0,2%.%time:~0,2%.%time:~3,2%"

set "source=C:\Program Files (x86)\Phoresis\Backup"

robocopy "%source%" "%DirName%" /e /z /Mir

Additional issues/notes:

  • although you used quotes in your set command lines, which avoid troubles with some special characters (&, ^, (, ), SPACE, etc.), the "" are not part of the values stored in the variables; so when expanding (reading) the variables, you need to take care of such special characters once again, by surrounding the read variable by "";
  • the md command has been fixed (there was a leading \, but no opening ") and moved up;
  • variable DirName contains now also the parent (root) destination directory F:\Cap2 Flex Backup, which is stored in variable newDirectory, to avoid relative paths for robocopy; since source contains an absolute path too, the script can be located and executed anywhere;
  • regard that the timestamp retrieved by %date% and %time% depend on the locale and region settings of the system; therefore this script is not quite portable; see this post to overcome that;
Community
  • 1
  • 1
aschipfl
  • 28,946
  • 10
  • 45
  • 77
  • Very helpful thanks. One question- (I did note the batch had to be executed in the destination directory to work correctly so something to do with relative paths?)- can u expand on point 3 ? – dancingbush Dec 03 '15 at 12:19
  • 1
    You're welcome! yes, I used absolute paths for both source and destination, so the batch file can be located and executed anywhere... – aschipfl Dec 03 '15 at 12:26