将杂乱的文件变量传递给 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 的内容,在 "
前后添加 \
,但是我无法找到/找出使我能够正确通过以下内容的解决方案:
FILE_PATH
- 包含空格和其他不需要的字符的文件所在文件夹的路径
OUTPUT_DIR
- 文件要复制到的目录路径,包括空格和其他不需要的字符
FILE_NAME
- 要复制的文件名
其他积分
- 我需要
SOURCE_FILES
作为数组,因为我必须遍历文件路径以找到位于多个 LAN 共享等中的单个文件源,所以我不能只将路径传递给文件夹之类的东西*.csv
。这个想法是将 SOURCE_FILES
扩展到多个项目。
1 在 this 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
)
我正在尝试将一组变量传递给 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 的内容,在 "
前后添加 \
,但是我无法找到/找出使我能够正确通过以下内容的解决方案:
FILE_PATH
- 包含空格和其他不需要的字符的文件所在文件夹的路径OUTPUT_DIR
- 文件要复制到的目录路径,包括空格和其他不需要的字符FILE_NAME
- 要复制的文件名
其他积分
- 我需要
SOURCE_FILES
作为数组,因为我必须遍历文件路径以找到位于多个 LAN 共享等中的单个文件源,所以我不能只将路径传递给文件夹之类的东西*.csv
。这个想法是将SOURCE_FILES
扩展到多个项目。
1 在 this 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
)