将杂乱的文件变量传递给 robocopy

Passing messy file variables to robocopy

我正在尝试将一组变量传递给 robocopy,所有变量都包含空格并且可能很长。我需要能够以一种能够 robocopy 捕获完整路径的方式包装参数。

:: 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: 
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

)

问题

问题出在行:

robocopy \"%FILE_PATH%\" \"%OUTPUT_DIR%" "%FILE_NAME%" /R:5 /W:2 /V /LOG+:"%ROBO_LOG%"

错误

返回的错误是:

ERROR : Invalid Parameter #3 : "Data\ "C:\Users\me\stuff and things\

我试过查看其他答案1、2 并修改传递给 robocopy 的内容,在 " 前后添加 \,但是我无法找到/找出使我能够正确通过以下内容的解决方案:

其他积分


1this discussion, 中表达了有关手动路径编辑的建议。这没有帮助,因为我打算传递许多名称可能难以理解的文件。

2 表达了类似的建议 here

这里是一个快速重写,更正了一些东西,删除了不必要的东西,并将 'array' 项目的迭代更改为更简单的东西:

Rem 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"

Rem Define output file path
Set "OUTPUT_ROOT=C:\Users\me\stuff and things\save here"

Rem Create dated folder within the OUTPUT_ROOT location
Rem Obtain date/time timestamps
Set "dt="
Rem Source: 
For /F "Tokens=2 Delims==" %%G In ('%SystemRoot%\System32\wbem\WMIC.exe OS Get
 LocalDateTime /Value 2^>NUL') Do Set "dt=%%~nG"

If Not Defined dt GoTo :EOF

Rem Define output path
Set "OUTPUT_DIR=%OUTPUT_ROOT%\%dt:~0,4%-%dt:~4,2%-%dt:~6,2%_%dt:~8,2%-%dt:~10,2%-%dt:~12,2%"

Rem Define robocopy log file
Set "ROBO_LOG=%OUTPUT_DIR%\robocopy.log"

Echo(
Echo Progressing messy file copying

Rem Iterate over array elements and copy files
For /F "Tokens=1,* Delims==" %%G In ('"(Set SOURCE_FILES[) 2>NUL"') Do (
    Echo Copying: %%H
    %SystemRoot%\System32\Robocopy.exe "%%~dpH." "%OUTPUT_DIR%" "%%~nxH" /R:5 /W:2 /V /LOG+:"%ROBO_LOG%"
    Echo Finished copying: %%H
)