问题描述
我一直在编写批处理脚本来循环遍历文件夹并生成按上次修改时间排序的文件列表.现在它按预期工作,但它不会搜索子文件夹,下面是我的代码:
I have been working on a batch script to loop through a folder and produce a list of the files sorted by when they were last modified. Right now it works as expected, but it does not search through sub-folders, below is my code:
echo Last executed on %date% at %time% > report.csv
echo. >> report.csv
FOR /F "TOKENS=1-4,* SKIP=4" %%a IN ('DIR /O-D /TW') DO (
IF exist %%e (
IF NOT %%e==.. (
IF NOT %%e==. (
echo %%e >> report.csv
echo %%a %%b >> report.csv
)
)
)
)
:Start
CScript modifyexcelGood.vbs
:End
goto :eof
忽略那些 if 语句,它们只是为了防止我得到错误的文件名.我知道/R 命令用于搜索子文件夹,但似乎我不能同时使用/R 和/F,或者我错了?我觉得有一个简单的解决方案,但我一直找不到.
Ignore those if statements, they are just there to prevent incorrect filenames I was getting. I know the /R command is for searching through subfolders, but it appears I cannot use both /R and /F, or perhaps I am wrong? I feel like there is a simple solution but I have not been able to find it.
谢谢
编辑为了澄清,批处理文件按修改日期对目录中的所有文件进行排序,并抛出在 Excel 工作表中找到的每个文件的文件名和日期(按排序顺序).然后它启动一个 VB 脚本来编辑工作表以使其看起来更美观.Vbs 和 bat 脚本必须一起使用,但它们旨在用于任何目录.它们旨在帮助清理包含旧文件的大型目录.(一旦我知道这 100% 有效,它最终也会删除旧文件.)
EDITTo clarify, the batch file sorts all the files in a directory by date modified and throws the file name and date of each file found in an excel sheet(In sorted order). It then starts a VB script that edits the sheet to make it look nice. The Vbs and bat scripts must be together but they are meant to be used in any directory. They are meant to help clean out large directories that have old files sitting around. (It will eventually delete the old files too once I know this works 100%.)
我可以将它们放在任何目录中并且它工作正常,我只需要它也可以访问任何子文件夹中的文件.
I can put these in any directory and it works fine, I just need it to hit the files in any subfolders as well.
那么我如何编辑我的代码以检查 %%e 是否是一个目录,如果是,则在该目录上运行循环?
So how can I edit my code to make it check if %%e is a directory and if so run the loop on that directory as well?
谢谢
推荐答案
恐怕你的要求不清楚.但是,下面的行列出了当前文件夹中的文件和下面的所有子文件夹,每个文件一行,并按上次修改日期对所有文件进行排序,较新的文件在前.
I am afraid your request is not clear. However, the line below list the files in current folder and all subfolders beneath, one line per file, and sort all files by last modified date, newer files first.
(for /F "delims=" %%a in ('dir /T:W /S /B') do echo %%~Ta %%a) | sort /R > report.csv
如果您对 dir
命令开关(如 /B
)有任何疑问,请键入 dir/?
.
If you have any doubt about dir
command switches (like /B
), type dir /?
.
这篇关于批处理脚本:带有两个命令的 For 循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!