I've got a problem with setting the path variable for the destination I want to copy files within a forfiles command to (cmd /c copy @path destination
).
The code:
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
fails to copy the files in the designated directory but passing the Path directly into the command like:
.....
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
works.
How would I need to pass the variable correctly?
Edited to working code after comments from @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
To clarify on @aschipfl comment because I did not realize the time stamp stays the same throughout the whole process - Is this what your comment implied?
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
Here's a quick example showing the methodology, I implied and suggested throughout my comments.
@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