I want to copy all files created on today date from Folder A to Folder B in batch file.
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a/%%b/%%c)
set MyPath=D:\Temp p
set DestPath=D:\Temp p\1\
forfiles -m *.* /D %mydate% /C "cmd /c copy %MyPath%\@file %DestPath%"
The answer is:
The system cannot find the file specified.
But if I change the folder "Temp p" to "Temp" (I mean omit the space) it works fine. How should I do that?
There are two things I want to advice you:
Enclose paths in pairs of quotation marks!
Besides the SPACE, there are more characters that are considered as delimiters by command prompt, which are TAB, ,
, ;
, =
. If you put ""
around all paths in general, you are not going to run into problems with those characters. There are some other characters that are allowed within paths and have special meanings in batch files (like ^
, &
, (
, )
); also those become protected when using quotation marks.
Always use the set
syntax set "VARIABLE=Value"
!
That way the quotation marks do not become part of the value assigned to the variable, but some special characters like ^
, &
, (
, )
, <
, >
, |
and even "
do not cause any trouble, and trailing SPACEs at that command line do not become part of the value unintendedly. Another great advantage is that you need to care about surrounding ""
only when you expand (read) a variable like "%VARIABLE%"
, since as aforementioned the ""
are not contained in the value. So you can use a variable even as part of a file path like "%VARIABLE%\file.txt"
to have the entire path in between ""
.
Note that this syntax only works when command extensions are enabled, but this is the default for the Windows command prompt anyway, and disabling them makes only sense when you want to execute a batch file that has been written for MS-DOS (command.com
).
So for your code portion (without checking the intrinsic function) the above means:
for /F "tokens=2-4 delims=/ " %%a in ('date /T') do (set "mydate=%%a/%%b/%%c")
set "MyPath=D:\Temp p"
set "DestPath=D:\Temp p\1\"
forfiles /P "%MyPath%" /M "*.*" /D %mydate% /C "cmd /C copy @file \"%DestPath%\""
The forfiles
command constitutes a problem if it is used as you tried, because the value @file
includes the surrounding ""
(unfortunately). So (assuming the currently iterated item is called filename.ext
), the path %MyPath%\@file
will look like D:\Temp p\"filename.ext"
when the variables are replaced by their values. To work around that I added the option /P "%MyPath%"
, so forfiles
runs the command after the /C
switch within the directory given by %MyPath%
, so you do not need to prepend %MyFile%
to @file
but use the latter alone.
As you might have noticed I did not put ""
around %DestPath%
but wrote \"%DestPath%\"
instead. This is also a forfiles
speciality, because you need to escape quotation marks within the quoted command line string (after /C
) by preceding \
. Otherwise forfiles
removes any literal quotes "
.
Therefore forfiles
carries out the following command for being executed per each loop iteration: cmd /C copy "filename.ext" "%DestPath%"
(reusing the above mentioned sample item).