I'm trying to pass a set of variables to robocopy
, all variables contain spaces and are potentially very long. I need to be able to wrap arguments in a manner that enables robocopy
capturing full path.
:: Define array of input files to copy
set "MASTER_SOURCE=\\path\with\some file\and messy. folder name\bla bla --- bla\stuff"
set "SOURCE_FILES[0]=%MASTER_SOURCE%\this file with a pesky name.csv"
set "SOURCE_FILES[1]=%MASTER_SOURCE%\this other file with a pesky name.csv"
:: Define output file path
SET "OUTPUT_ROOT=C:\Users\me\stuff and things\save here"
:: Create dated folder within the OUTPUT_ROOT location
:: Obtain date/time timestamps
:: Source: https://stackoverflow.com/a/19706067/1655567
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
:: Define output path
set "OUTPUT_DIR=%OUTPUT_ROOT%\%fullstamp%"
:: Create output path
mkdir "%OUTPUT_DIR%"
echo.
echo Progressing messy file copying
echo Created output: %OUTPUT_DIR%.
:: Define robocopy log file
set "ROBO_LOG=%OUTPUT_DIR%\robocopy.log"
:: Iterate over array elements and copy files
:: Set counter
set "x=0"
:SymLoop
if defined SOURCE_FILES[%x%] (
call set "FILE_FULL_PATH=%%SOURCE_FILES[%x%]%%"
echo Copying: %FILE_FULL_PATH%
for /F "delims=" %%I IN ("%FILE_FULL_PATH%") do (
call set "FILE_NAME=%%~nxI"
echo File name: %FILE_NAME%
call set "FILE_PATH=%%~dpI"
echo File path: %FILE_PATH%
)
robocopy \"%FILE_PATH%\" \"%OUTPUT_DIR%" "%FILE_NAME%" /R:5 /W:2 /V /LOG+:"%ROBO_LOG%"
call echo Finished copying: %FILE_NAME%
set /a "x+=1"
GOTO :SymLoop
)
Problem
The problem is with line:
robocopy \"%FILE_PATH%\" \"%OUTPUT_DIR%" "%FILE_NAME%" /R:5 /W:2 /V /LOG+:"%ROBO_LOG%"
Error
The returned error is:
ERROR : Invalid Parameter #3 : "Data\ "C:\Users\me\stuff and things\
I've tried looking at other answers1, 2 and modifying what is passed to robocopy, adding \
before and after "
, but I wasn't able to find / figure out solution that would enable me to properly pass the following:
FILE_PATH
- Path to the folder where the file is located with spaces and other undesired characterOUTPUT_DIR
- Path to the directory where the file is to be copied to, including spaces and other undesired charactersFILE_NAME
- name of the file is to be copied
Other points
- I need
SOURCE_FILES
as an array as I've to iterate over file path to source single file located across numerous LAN shares, etc. so I can't just pass the path the folder with something like*.csv
. The idea is to expandSOURCE_FILES
to a number of items.
1 In this discussion, suggestions concerned with manual path editing are expressed. This is not helpful as I'm intending to pass a number of files with potentially difficult names.
2 Similar advice was expressed here.