批处理脚本:使用两个命令进行循环 [英] Batch Script: For Loop with Two commands

查看:394
本文介绍了批处理脚本:使用两个命令进行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究批处理脚本,以遍历文件夹并生成文件的列表,这些文件的上次修改时间是按文件排序的.现在,它可以按预期工作,但无法搜索子文件夹,下面是我的代码:

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脚本,该脚本将编辑工作表以使其看起来更漂亮. Vb和bat脚本必须在一起,但是它们可以在任何目录中使用.它们旨在帮助清除包含旧文件的大型目录. (一旦我知道这100%可行,它最终也会删除旧文件.)

EDIT To 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?

谢谢

推荐答案

恐怕您的请求不清楚.但是,下面的行列出了当前文件夹中的文件及其下面的所有子文件夹,每个文件一行,并按上次修改日期对 all 个文件进行排序,首先是较新的文件.

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 /?.

这篇关于批处理脚本:使用两个命令进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆