使用FORFILES批量移动30天的文件夹 [英] Batch to move folders that are 30 days old using FORFILES
问题描述
我目前正在工作,只能将文件夹从一个目录移动到一个文件夹.我创建的当前bacth文件如下.
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
我遇到的问题是使用目标目录中有空格的目录,例如"C:\ Move Test".我想知道是否有办法将文件夹移动到名称中带有空格的另一个目录.
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.
推荐答案
您的命令行看起来很奇怪,有几个错误:
Your command line looks quite strange, there are several mistakes:
ForFiles /P "C:\Batch test" /D -4 /C "CMD /C if @ISDIR==TRUE echo Move @FILE %target%" &Move @File %target%
怎么了?
- 您说的是30天的年龄,但是您指定了
/D -4
,尽管/D -30
是正确的;请注意,/D
将检查最后修改日期,而不是创建日期! - (第一个)
move
命令前面有一个echo
,因此显示而不是执行move
命令.为避免这种情况,只需删除echo
; - 在
forfiles
之后还有第二个move
命令,因为它出现在forfiles /C
部分的右引号后面,命令连接运算符&
后面;这完全超出了forfiles
的范围,因此对@file
进行了字面处理;和号和孤立的move
命令需要删除; - 回答您的实际问题:要避免在文件/目录路径中出现空格(或其他特殊字符)的问题,请在其周围加上引号; 但是:由于该部分出现在命令字符串中已经引号的
forfiles /C
之后,因此您不能简单地使用""
,您需要对其进行转义;forfiles
中有两个选项:1.使用\"
和2.使用0x22
(根据forfiles /?
帮助,您可以通过其十六进制代码以0xHH
格式指定字符,代码0x22
代表"
字符);我强烈建议使用选项2,因为在变体1中,仍然出现"
字符,这可能会影响命令解释器cmd
,因为它不熟悉\"
转义(其转义字符为^
);
- you are talking about 30 days of age, but you specify
/D -4
, although/D -30
was correct; note that/D
checks for the last modification date, not for the creation date! - there is an
echo
in front of the (first)move
command, so themove
command is displayed rather than executed; to avoid that, simply removeecho
; - there is a second
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; - to answer your actual question: to aviod problems with spaces (or other special characters) appearing in file/directory paths, put quotation marks around them; but: since that part appears within the command string after
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^
);
因此,更正后的命令行如下所示:
ForFiles /P "C:\Batch test" /D -30 /C "CMD /C if @ISDIR==TRUE Move @FILE 0x22%target%0x22"
为了完整起见,这就是选项1的样子:
For the sake of completeness, this is how option 1. would look like:
ForFiles /P "C:\Batch test" /D -30 /C "CMD /C if @ISDIR==TRUE Move @FILE \"%target%\""
请注意,您无需在此处引用源代码,因为@FILE
(像所有与路径相关的@
样式变量一样)扩展为已经加引号的字符串.
Note that you do not need to quote the source here, because @FILE
(like all path-related @
-style variables) expand to already quoted strings.
这篇关于使用FORFILES批量移动30天的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!