I am currently working that moves ONLY folders from one directory to a folder. The current bacth file I have created is as follows.
Set /p target=Select Target Destination:
ForFiles /P "C:\Batch test" /D -4 /C "CMD /C if @ISDIR==TRUE echo Move @FILE %target%" &Move @File %target%
pause
The problem I have is with using a target directory that has a space in it for instance "C:\Move Test". I was wondering if there was a way to still move folders to another directory that has a space in the name.
You can replace the double quotes with their hexadecimal equivalent 0x22:
If you get problems with other characters you can also replace them; 0x26=&, 0x28=(, 0x29=).
Your command line looks quite strange, there are several mistakes:
What is wrong:
/D -4
, although/D -30
was correct; note that/D
checks for the last modification date, not for the creation date!echo
in front of the (first)move
command, so themove
command is displayed rather than executed; to avoid that, simply removeecho
;move
command afterforfiles
, as it appears behind the closing quotation mark of theforfiles /C
part, and behind the commmand concatenation operator&
; this is completely out of scope offorfiles
, therefore@file
was treated literally; the ampersand and that orphanedmove
command needs to be removed;forfiles /C
, which is already quoted, you cannot simply use""
, you need to escape them; there are two options inforfiles
: 1. using\"
, and 2. using0x22
(according to theforfiles /?
help, you can specify characters by their hexadecimal codes in the format0xHH
, the code0x22
represents a"
character); I strongly recommend option 2., because in variant 1. there still appear"
characters that could impact the command interpretercmd
as it is not familiar with the\"
escaping (its escape character is^
);Thus the corrected command line looks like this:
For the sake of completeness, this is how option 1. would look like:
Note that you do not need to quote the source here, because
@FILE
(like all path-related@
-style variables) expand to already quoted strings.