0

我在为目标设置路径变量时遇到问题,我想将 forfiles 命令中的文件复制到 ( cmd /c copy @path destination)。编码:

REM source folder location
set source=\\Path\TO\File\SOURCE\FOLDER
REM destination folder location
set set destination=E:\LOCAL\FOLDER\
REM log_file location
set log_file=E:\LOCAL\FOLDER\Logfile.txt

SET /a "copy_count+=0"
PUSHD  %source%
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c copy @path 0x22%destination%0x22 & echo @file %date%, %time%>>0x22%log_file%0x22"
') do SET /a "copy_count"+=1
POPD

无法复制指定目录中的文件,但将路径直接传递到命令中,例如:

.....
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c copy @path E:\LOCAL\FOLDER\ & echo @file %date%, %time%>>0x22%log_file%0x22"
') do SET /a "copy_count"+=1
POPD

作品。

我需要如何正确传递变量?

在@Compo发表评论后编辑为工作代码

REM source folder location
set "source=\\Path\TO\File\SOURCE\FOLDER"
REM destination folder location
set "destination=E:\LOCAL\FOLDER\"
REM log_file location
set "log_file=E:\LOCAL\FOLDER\Logfile.txt"

SET /a "copy_count+=0"
PUSHD  %source%
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c If @IsDir==FALSE copy @path %destination% & echo @file %date%, %time%>>%log_file%"
') do SET /a "copy_count"+=1
POPD

澄清@aschipfl评论,因为我没有意识到时间戳在整个过程中保持不变 - 这是您的评论所暗示的吗?

SET /a "copy_count+=0"
PUSHD  %source%
(for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c If @IsDir==FALSE copy @path %destination% & echo @file %date%, %time%)>>%log_file%"
') do SET /a "copy_count"+=1
POPD
4

1 回答 1

1

这是一个显示方法的快速示例,我在我的评论中暗示和建议。

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "source=\\Path\TO\File\SOURCE\FOLDER"
Set "destination=E:\LOCAL\FOLDER"
Set "log_file=E:\LOCAL\FOLDER\Logfile.txt"

PushD "%source%"

Set "copy_count=0"
(For /F "Tokens=1,*" %%G In ('%SystemRoot%\System32\forfiles.exe /S /D -2 /C ^
 "%SystemRoot%\System32\cmd.exe /D /C If @IsDir==FALSE Echo @Path 0x25DATE0x25, 0x25TIME0x25"'
) Do Copy /Y %%G "%destination%" 1> NUL 2>&1 && Set /A copy_count+=1 & Echo %%~nxG %%H) 1> "%log_file%"

PopD
于 2020-11-26T15:57:33.057 回答