Forfiles 在使用路径变量时复制文件

Forfiles copy failes when using path variable

我在设置目标路径变量时遇到问题,我想在 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

复制指定目录下的文件失败,而是直接将Path传递给命令,如:

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

这是一个显示方法的简单示例,我在我的评论中暗示并建议了这一方法。

@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