2

How to pass a spaced path directory to robocopy?

I need to transfer some files in huge amount using robocopy. Usually, I will transform the directory (from path with spaces to non-spaces) then use robocopy. But now, I can not do that due to privilege & efficiency.

The flow is simple : read filename from txt and copy.

I had looking and try many things and seems going nowhere.

@echo off
set src_folder=C:\foo bar\lorem ipsum\
set dst_folder=C:\Users\asd\Desktop\copyFileImageFromMagentoFolder\photo_temp20\
for /f "tokens=*" %%i in (list.txt) do robocopy %src_folder% %dst_folder% %%i
Pause

This code work flawlessly if there is no spaces on path directory.

And i tired to modify my code. Add some syntax :

@echo off
set src_folder=C:\foo bar\lorem ipsum\
set dst_folder=C:\Users\asd\Desktop\copyFileImageFromMagentoFolder\photo_temp20\
for /f "tokens=*" %%i in (list.txt) do robocopy "%src_folder%\%%i" "%dst_folder%\%%i"
Pause

But, robocopy throw me error 123 and error 2 (as i remember).

As a note, the folder and file is exist. So please never ask me that question.


Any suggestion will be appreciate

Yuri
  • 23
  • 7
  • 1
    If `list.txt` contains file names, I see no reason to use __ROBOCOPY__ instead of __COPY__ to copy the files. So I suggest to use `for /F "usebackq eol=| delims=" %%I in ("list.txt") do copy "%src_folder%%%~I" "%dst_folder%"`. `src_folder` path ends with a backslash. Therefore no additional backslash on source argument of command __COPY__. `tokens=*` results in removing all leading spaces from line read from file. A file name can start with a space. So usage of `tokens=*` is in general not advisable. – Mofi May 16 '19 at 07:11
  • 1
    `eol=;` is the default. A file name can start with a semicolon and would be ignored in this case by __FOR__. `eol=|` redefines end of line character to vertical bar which no file name can have and so file names starting with `;` are also processed by __FOR__. `delims=` at end of __FOR__ options argument string redefines set of delimiter characters to an empty list which disables line splitting behavior completely which `tokens=*` does not. That is the reason why leading spaces/tabs are removed from read line on using `tokens=*`. – Mofi May 16 '19 at 07:15
  • 1
    However, __ROBOCOPY__ can be also used which would have the advantage that destination directory is automatically created on not already existing. But then the syntax must be used as help of __ROBOCOPY__ explains on running in a command prompt window `robocopy /?`. So used can be also `for /F "usebackq eol=| delims=" %%I in ("list.txt") do %SystemRoot%\System32\robocopy.exe "%src_folder%" "%dst_folder%" "%%~nxI"`, i.e. "source directory" "destination directory" "file to copy without path". Well, nothing is posted what `list.txt` really contains. So it is not guaranteed that this really works. – Mofi May 16 '19 at 07:24
  • @Mofi I am very satisfy with your answer & explanation. Please post as `answer` rather than `comment` so I can mark as an answer. – Yuri May 16 '19 at 10:01
  • What can i conclude is, `Robocopy` is used for abstract copying whereas `Copy` is used for copying specific file. Is it true? – Yuri May 16 '19 at 10:03
  • @Mofi But, I feel a different speed copying. `Copy` seems slower rather than `Robocopy`. And the progress status is not fully detail as `robocopy` does. – Yuri May 16 '19 at 10:06
  • @Movi Could you please make a syntax for `robocopy`? – Yuri May 16 '19 at 10:07
  • Remove the ending backslashes from folder paths or include an additional dot after the backslash. As `robocopy` is not an internal command but an executable file rules are different and the backslash escapes the double quote that becames part of the path. See [here](https://stackoverflow.com/a/25841519/2861476) for a extended explanation of the problem. – MC ND May 17 '19 at 16:50

1 Answers1

0

did you try double quotes around the path with spaces?

set src_folder="C:\foo bar\lorem ipsum\"

Berger
  • 299
  • 3
  • 13
  • I did. I already use double quote and single quote. On path and syntax. As that answer is very common on SO. I wonder, why it doesnt work at me. – Yuri May 16 '19 at 07:02