批处理文件:遍历文件修改,因为给定的日期 [英] Batch File: Iterate Over Files Modified Since a Given Date

查看:114
本文介绍了批处理文件:遍历文件修改,因为给定的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个执行命令的批处理文件(删除文件,一个简单的例子,虽然我的命令是定制/不同)的每个文件相匹配的通配符(*说.JPG),因为一些日期被修改(如2010年1月1日或以后)。日期可以是在批处理文件中硬codeD或作为命令行参数传递。

I want to create a batch file that executes a command (delete the file, for a simple example, though my command is custom/different) on each file matching a wildcard (say *.jpg) that was modified since some date (such as 1-1-2010 or later). The date can either be hardcoded in the batch file or passed as a command line param.

伪code:


foreach file C:\Images\*.jpg modified-since 1-1-2010 do
  del file

我倒是preFER一个纯粹的批处理文件,而不是PowerShell中,VBScript中,等。如果使用纯批处理语言是不可能的,我可以用一个小/免费的第三方命令行程序,以协助产生改性自文件列表。我需要支持在Windows 2000上运行或更高版本。

I'd prefer a pure batch file, not PowerShell, VBScript, etc. If using pure batch language isn't possible, I could use a small/free third-party command line program to assist with generating the modified-since file list. I need to support running in Windows 2000 or later.

(略从pipitas答案修改)

(Slightly modified from the answer by pipitas)

@ECHO OFF
for /f "usebackq tokens=1-7* delims=/: " %%I in (`dir/o-d/tw ^| findstr /I .jpg`) do (
  if "%%K%%I%%J %%N %%L%%M" GEQ "20100801 AM 0000" (
    del /P %%P
  )
) 

一些注意事项:删除/ p不要求每个删除Y / N确认。我躲过%字符,这样他们就.bat文件内工作。该^是必要的逃生管道。的有usebackq使得反引号限定一个要执行的命令(而不是搜索文字串或文件),代币设置的变量的数目,以解析的线放置在与%我开始,并delims设置分隔符(包括结尾间隔)用于解析线到这些标记。我说的.jpg作为FINDSTR搜索字符串,因为我只是想处理.jpg文件。一些可能的问题是,您的区域可能会打印日期不同,需要不同数量的变量或不同的分隔符,你可能有文件名嵌入.JPG的一些假阳性的比赛(需要FINDSTR正则表达式等)

Some notes: Remove /P to not require Y/N confirmation on each delete. I escaped the % characters so they would work inside a .bat file. The ^ is necessary to escape the pipe. The "usebackq" makes the backticks delimit a command to execute (instead of searching a literal string or file), tokens sets the number of variables to place the parsed line in starting with %I, and delims sets the delimiters (including the trailing space) used to parse the line into those tokens. I added .jpg as the findstr search string, since I only wanted to process .jpg files. Some possible problems are that your locale might print dates differently, requiring a different number of variables or different delimiters, and you might have some false positive matches with an embedded ".jpg" in the file name (requiring a findstr regex, etc.).

如果您不需要支持Windows 2000和更早的版本,使用FORFILES约翰内斯的建议将使这容易得多。

If you don't need to support Windows 2000 and earlier, using forfiles as suggested by Johannes would make this much easier.

推荐答案

一是少数preliminaries:

First a few preliminaries:


  • DIR / OD / TW / S .. \\路径\\为\\ somedir :结果
    列出所有目录中的文件+子目录,按日期+最后修改时间进行排序,

  • DIR / O-D / TW / S .. \\路径\\为\\ somedir :结果
    相同,但以相反的顺序(最老的最后一个)。

  • dir /od /tw /s ..\path\to\somedir :
    lists all the directory's files+subdirs, sorted by date+time of last modification,
  • dir /o-d /tw /s ..\path\to\somedir :
    same, but in reverse order (oldest last).

接下来,我使用一个本地的例子为现有路径。让我们去:

Next, I use a local example for an existing path. Let's go:

dir /tc /o-d /s gstmp\*yell*.tif ^| findstr 2010
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: D479-1658

 Verzeichnis von C:\downloads\gstmp

08/06/2010  05:01 PM           19 compression-g4-emptypage-tiffsep1(Yellow).tif
08/06/2010  05:00 PM           19 compression-lzw-emptypage-tiffsep1(Yellow).tif
08/06/2010  04:57 PM           19 compression-g3-tiffsep1(Yellow).tif
08/06/2010  04:57 PM           19 compression-crle-tiffsep1(Yellow).tif
08/06/2010  04:56 PM           19 compression-pack-share-tiffsep1(Yellow).tif
08/06/2010  04:53 PM           19 compression-g4-share-tiffsep1(Yellow).tif
08/06/2010  04:52 PM           19 compression-lzw-share-tiffsep1(Yellow).tif
08/06/2010  04:51 PM           19 compression-default-share-tiffsep1(Yellow).tif
08/06/2010  03:59 PM           19 compression-none-share-tiffsep.Yellow.tif
08/06/2010  03:55 PM           19 share-tiffsep.Yellow.tif
              10 Datei(en)       190 Bytes    

我们不喜欢在这一刻额外的标题和摘要信息。我们可以通过东西过滤摆脱这一点。我会简单地用'2010'。

We don't like the additional header and summary info in this moment. We can get rid of this by filtering for something. I'll simply use '2010'.

但nNow真正的问题开始:创建日期和时间可能在不同的格式进行打印,这取决于你的'区域'。你可以看到自己,但目前怎么找我。

But nNow the real problem starts: creation date and time can possibly be printed in different formats, depending on your 'locale'. You can see yourself, how it currently looks for me.

那岂不是工作,如果我们可以在这将完全匹配数字顺序排列的格式显示的日期?那么我们就可以实现一个决定如何通过简单的数字比较删除一些逻辑。让我们尝试(如果这对我的作品,也不一定会为你工作 - 你可能需要采用的语言环境)。注意'后引号(``)我用,还有^ -sign:

Wouldn't it be working, if we could make the date displayed in a format that would exactly match numerical order? Then we could implement some logic that decides about deletion through simple number comparison. Let's try (if this works for me, it will not necessarily work for you -- you may need to adopt to your locale). Note the 'backquotes' ("``") I'm using, as well as the ^-sign:

for /f "usebackq tokens=1-7* delims=/: " %I in (`dir/tc/o-d/s gstmp\*yell*.tif ^
   ^| findstr 2010`) do echo.    %I %J %K %L %M %N %O %P

下面输出:

08 06 2010 05 01 PM 19 compression-g4-emptypage-tiffsep1(Yellow).tif
08 06 2010 05 00 PM 19 compression-lzw-emptypage-tiffsep1(Yellow).tif
08 06 2010 04 57 PM 19 compression-g3-tiffsep1(Yellow).tif
08 06 2010 04 57 PM 19 compression-crle-tiffsep1(Yellow).tif
08 06 2010 04 56 PM 19 compression-pack-share-tiffsep1(Yellow).tif
08 06 2010 04 53 PM 19 compression-g4-share-tiffsep1(Yellow).tif
08 06 2010 04 52 PM 19 compression-lzw-share-tiffsep1(Yellow).tif
08 06 2010 04 51 PM 19 compression-default-share-tiffsep1(Yellow).tif
08 06 2010 03 59 PM 19 compression-none-share-tiffsep.Yellow.tif
08 06 2010 03 55 PM 19 share-tiffsep.Yellow.tif

快到了,但还没有完全。我们需要改变我们的标记的顺序%I引用%j%K %K%I引用%j 使我们的日期显示为 08 2010 06 而不是 08 06 2010 。此外,如果我们写正确的令牌不带空格,以及因此它可以很容易地比较得到一个日期+时间字符串。另外,我们跳过%0 和一个箭头取代它,只是为了好玩:

Almost there, but not fully yet. We need to change the order of our tokens %I %J %K to %K %I %J so that our date displays as 2010 08 06 instead of 08 06 2010. Also, if we write the right tokens without spaces, well get a date+time string so it can be easily compared. Plus, we skip the %O and replace it with an arrow, just for the fun of it:

for /f "usebackq tokens=1-7* delims=/: " %I in (`dir/tc/o-d/s gstmp\*yell*.tif ^
   ^| findstr 2010`) do echo.    %K%I%J%L%M%N ==^> %P

现在输出变为这样:

201008060501PM ==> compression-g4-emptypage-tiffsep1(Yellow).tif
201008060500PM ==> compression-lzw-emptypage-tiffsep1(Yellow).tif
201008060457PM ==> compression-g3-tiffsep1(Yellow).tif
201008060457PM ==> compression-crle-tiffsep1(Yellow).tif
201008060456PM ==> compression-pack-share-tiffsep1(Yellow).tif
201008060453PM ==> compression-g4-share-tiffsep1(Yellow).tif
201008060452PM ==> compression-lzw-share-tiffsep1(Yellow).tif
201008060451PM ==> compression-default-share-tiffsep1(Yellow).tif
201008060359PM ==> compression-none-share-tiffsep.Yellow.tif
201008060355PM ==> share-tiffsep.Yellow.tif

现在你几乎准备好比较的第一个字段。

Now you are nearly ready for comparing the first field.

不过考虑这个问题:我得到我在次12小时周期, AM PM 。因此,为了数字上比较,我们不得不更换PM和数值AM每个,以这样的方式,我们的上午04时51分的翻译下午4点50分仍然会以正确的方式数字进行排序。

But consider this: I get my times in cycles of 12 hours, with AM and PM. So in order to compare numerically, we have to replace PM and AM with a numerical value each, in such a way that our translation of 04:51 AM and 04:50 PM still will sort numerically in the right way.

现在这个怎么样:

for /f "usebackq tokens=1-7* delims=/: " %I in (`dir/tc/o-d/s gstmp\*yell*.tif ^
     ^| findstr 2010`) do  (
        if "%K%I%J %N %L%M" GEQ "20100806 PM 0457"  (
            echo.   [ %K-%I-%J %L:%M %N    %P. ]
        )
 )

这列出了应删除所有文件,也呈现出各自的日期。为真正删除,我们只需要替换最后一个回声。 ...%P DEL / P / F / S%P

This lists all files which should be deleted, also showing their respective dates. To really delete, we just need to replace the last echo. ... %P. by a del/p/f/s %P.

这篇关于批处理文件:遍历文件修改,因为给定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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