批处理-遍历文本文件 [英] Batch - Looping through text file
问题描述
我正在尝试使用以下命令在多个目录中查找所有30天以上的文件:
I'm trying to find all files older than 30 days in several directories using this command:
[Directory] && forfiles /d -30 /c "cmd /c echo @path"
输出为.txt文件.
文本文件包含目录的路径: C:\ Directory1 C:\ Directory2 C:\ Directory3等
The text file contains the path to the directory: C:\Directory1 C:\Directory2 C:\Directory3 etc
我正在尝试使用文本文件遍历几个目录,但是我需要提供2个命令: cd(更改为我需要信息的文件所在的目录)和获取信息的实际命令)
I'm trying to loop through several directories using a text file but I need to provide 2 commands: cd (to change to the directory whose files I need info on) and the actual command to get the information)
如果我创建一个手动输入目录名称的批处理文件,我将具有以下内容:
If I create a batch file entering the directory names manually I have something like this:
cd "C:Directory1" && forfiles /d -30 /c "cmd /c echo @path"
cd "C:Directory2" && forfiles /d -30 /c "cmd /c echo @path"
cd "C:Directory3" && forfiles /d -30 /c "cmd /c echo @path"
如何输入"cd"在循环开始时输入命令,然后是txt文件中的目录以及命令(forfiles /d -30 /c "cmd /c echo @path")
How do I enter the "cd" command at the beggining of the loop, then the directory which is in the txt file and the rest of the command (forfiles /d -30 /c "cmd /c echo @path")
到目前为止,我有:
for /f "usebackq tokens=*" %%A in ("C:\list.txt") do forfiles /d -30 /c "cmd /c echo @path %%A
谢谢!
推荐答案
带有/F
选项的FOR
命令用于读取文件,并将文件的每次迭代分配给变量%%A
.因此,请将该变量与FORFILES
命令的/P
选项一起使用.
The FOR
command with the /F
option is used to read a file and it assigns each iteration of the file to the variable %%A
. So use the that variable with the /P
option of the FORFILES
command.
for /f "usebackq tokens=*" %%A in ("C:\list.txt") do forfiles /P "%%~A" /d -30 /c "cmd /c echo @path"
这篇关于批处理-遍历文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!