|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
   『楼 主』:
[原创]用批处理查找符合时间范围的文件
使用 LLM 解释/回答一下
用批处理查找符合时间范围的文件
起因:
看到 shijianxin 兄的提问,问题是如何查找创建于某一时间段的文件。动手前我最先想到的是 Windows Resource Kits 或 2003 下的 forfiles.exe 看了一下帮助后很让我失望,forfiles 只能选择大于或等于,或者小于或等于某日期的文件,而不能选择某一时间段的文件。看来只好动手写批处理了。想找一下问题的入手点,发觉比较困难,想到用 Ritchie Lawrence 的 DateToDays 函数,效率、速度会怎样?我马上否定了这种方案,再想到用 forfiles 分别生成一个大于和小于某日期的文件列表,然后取这两个文件重复的部分。效率、速度仍然不怎么样,再想一下于是乎就有了下面的脚本。
说明:这是一个最简单的版本,只有代码的主干,也比较便于理解所以先贴出来。我随后会将其完善成实用的批程序的。
用法:filedate YY-MM-DD YY-MM-DD ,参数1为起始日期,参数2为结束日期,如:2006-06-30 2006-07-09
- @echo off
- if "%1" NEQ "$" (
- set Bdate=%1
- set Edate=%2
- for /f "tokens=1,2 delims=§" %%a in ('"%~0" $^|sort') do (
- call :sub "%%a" "%%b"
- )
- ) else (
- for %%i in (*) do echo %%~ti§%%i
- echo.%Bdate% 00:00
- echo.%Edate% 99:99
- )
- goto :EOF
- :sub
- if "%~1" == "%Edate% 99:99" set flag=0
- if "%flag%" == "1" echo."%~2"
- if "%~1" == "%Bdate% 00:00" set flag=1
- goto :EOF
无奈何发表于 2006-07-31 21:48
Last edited by 无奈何 on 2006-8-3 at 22:57 ]
Find files that meet the time range with batch processing
Origin:
I saw Brother shijianxin's question, which was how to find files created in a certain time period. Before starting, the first thing I thought of was Windows Resource Kits or forfiles.exe under 2003. After looking at the help, I was very disappointed. forfiles can only select files that are greater than or equal to or less than or equal to a certain date, and cannot select files in a certain time period. It seems I have to write a batch processing myself. I wanted to find the starting point of the problem, but I found it difficult. I thought of using Ritchie Lawrence's DateToDays function, how would the efficiency and speed be? I immediately denied this plan. Then I thought of using forfiles to generate a file list greater than and less than a certain date respectively, and then take the overlapping parts of these two files. The efficiency and speed are still not very good. Then I thought, so there is the following script.
Description: This is the simplest version, only the main part of the code, and it is also relatively easy to understand, so I will post it first. I will improve it into a practical batch program later.
Usage: filedate YY-MM-DD YY-MM-DD, parameter 1 is the start date, parameter 2 is the end date, for example: 2006-06-30 2006-07-09
- @echo off
- if "%1" NEQ "$" (
- set Bdate=%1
- set Edate=%2
- for /f "tokens=1,2 delims=§" %%a in ('"%~0" $^|sort') do (
- call :sub "%%a" "%%b"
- )
- ) else (
- for %%i in (*) do echo %%~ti§%%i
- echo.%Bdate% 00:00
- echo.%Edate% 99:99
- )
- goto :EOF
- :sub
- if "%~1" == "%Edate% 99:99" set flag=0
- if "%flag%" == "1" echo."%~2"
- if "%~1" == "%Bdate% 00:00" set flag=1
- goto :EOF
Helplessly posted on 2006-07-31 21:48
Last edited by 无奈何 on 2006-8-3 at 22:57 ]
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-7-31 22:14 |
|
|
shijianxin
初级用户
 
积分 115
发帖 52
注册 2005-8-7
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
版主办事效率真的是高!佩服一下~可惜我看不懂是个菜鸟不过还是十分感谢你的帮助~来这里学习了很多!论坛有你们这样的人才精彩~~谢谢~~
The efficiency of the moderator is really high! I admire it~ Unfortunately, I can't understand it, I'm a novice, but I'm still very grateful for your help~ I've learned a lot here! The forum is wonderful with talents like you~~ Thank you~~
|

愿你们永远开心 |
|
2006-8-1 14:24 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
   『第 3 楼』:
使用 LLM 解释/回答一下
恐怕新手看无奈何的代码还是一头雾水吧,建议老鸟可以参考参考,至于新手嘛,可以看看我的代码:
@echo off
if "%1" == "" goto :error
set dateEnd=%2
if "%dateEnd%" == "" set dateEnd=%date:~0,10%
set strFileType=%3
if "%strFileType%" == "" set strFileType=*.*
for /r %4 %%i in (%strFileType%) do if %%~ti gtr %1 if %%~ti lss %dateEnd% echo %%~ti %%i | sort
goto :end
:error
echo 参数不正确!
:end
pause
参数1:开始日期
参数2:结束日期(省略则为当前日期)
参数3:文件类型(省略则为所有文件类型)
参数4:文件目录(省略则为当前目录)
至少要有一个参数,指定后一个参数时必须先指定前一个参数,如果想省略前面的参数而只指定后面的参数则需用逗号来指定想省略的参数。如:
findfile 2006-07-01,,,c:\ 查找2006-07-01以后至今c盘下的所有类型文件
findsfile 2006-07-01,,*.exe 查找2006-07-01以后到当前日期的当前目录下exe文件
不过,需要注意的是,打印的结果为 修改时间是指定日期范围内的文件列表,而并非 创建时间
如果要打印创建时间为指定时间范围内的列表的话,建议还是用dir /tc /od列出列表后,再用for来进行处理。
I'm afraid newbies will still be confused by the code that's hard to understand. It's suggested that veterans can refer to it. As for newbies, you can take a look at my code:
@echo off
if "%1" == "" goto :error
set dateEnd=%2
if "%dateEnd%" == "" set dateEnd=%date:~0,10%
set strFileType=%3
if "%strFileType%" == "" set strFileType=*.*
for /r %4 %%i in (%strFileType%) do if %%~ti gtr %1 if %%~ti lss %dateEnd% echo %%~ti %%i | sort
goto :end
:error
echo Incorrect parameters!
:end
pause
Parameter 1: Start date
Parameter 2: End date (if omitted, it's the current date)
Parameter 3: File type (if omitted, it's all file types)
Parameter 4: File directory (if omitted, it's the current directory)
There must be at least one parameter. When specifying the latter parameter, you must first specify the previous one. If you want to omit the previous parameters and only specify the subsequent one, you need to use commas to specify the parameters to be omitted. For example:
findfile 2006-07-01,,,c:\ Find all types of files under the C drive from after 2006-07-01 to now
findsfile 2006-07-01,,*.exe Find exe files in the current directory from after 2006-07-01 to the current date
However, it should be noted that the printed result is the list of files whose modification time is within the specified date range, not the creation time
If you want to print the list of files whose creation time is within the specified time range, it's suggested to use dir /tc /od to list the list first, and then use for to process it.
|
|
2006-8-1 14:30 |
|
|
electronixtar
铂金会员
      
积分 7493
发帖 2672
注册 2005-9-2
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
无奈何 版主的bat达到了……看不懂的境界!
Helpless, the version master's bat has reached... an incomprehensible realm!
|

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>" |
|
2006-8-1 15:11 |
|
|
buddiyar
初级用户
 
积分 160
发帖 75
注册 2006-6-28
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
作个记号
以后慢慢学习
呵呵
Make a mark, learn slowly later, heh heh
|
|
2006-8-30 19:16 |
|
|
IceCrack
中级用户
   DOS之友
积分 332
发帖 168
注册 2005-10-6 来自 天涯
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
哎 几天登陆不上。多了几个精华帖啊!抓紧时间学习一下
Hey, couldn't log in for a few days. There are a few more featured posts! Hurry up and study them
|

测试环境: windows xp pro sp2 高手是这样炼成的:C:\WINDOWS\Help\ntcmds.chm |
|
2006-8-30 20:24 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
Originally posted by electronixtar at 2006-8-1 15:11:
无奈何 版主的bat达到了……看不懂的境界!
对于3742668版主代码的有些地方,我同样达到了看不懂的境界
<img src="images/smilies/face-sad.png" align="absmiddle" border="0">
Originally posted by electronixtar at 2006-8-1 15:11:
Helplessly, the version owner's bat has reached... a realm that I can't understand!
For some parts of the code of moderator 3742668, I also reached a realm where I can't understand them
:(
|

尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。 |
|
2006-8-30 20:31 |
|
|
kennyfan
中级用户
  
积分 259
发帖 112
注册 2006-9-18
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
复杂啊..看来看去..看到俺头都晕咯!~~
It's complicated.. After looking around.. I'm so dizzy that my head spins!~~
|
|
2006-9-19 21:24 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
现在 3742668 斑竹的可以看懂了,无奈何斑竹的还有点看不懂
Now 3742668 moderator can understand, but some moderators still can't understand
|
|
2006-10-13 23:40 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
这个建议无奈何版主简单介绍一下这个P处理的工作机制,总体来说我个人认为是P处理本身调用自己来执行相应的功能和过滤
This suggestion is helpless. The moderator simply introduces the working mechanism of P processing. Generally speaking, personally I think that P processing itself calls itself to perform corresponding functions and filtering.
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-10-14 02:43 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
一直想把这段代码完善一下,做成一个类似 forfiles.exe 批处理,由于很多的原因没有完成。
我也一直以为没有多少人对这段代码如何工作感兴趣,如果早一点看到有想深入了的讨论的话,我也会早一点分享我的一点点心得。
代码的原理很简单,将所有文件的“修改时间”与“文件名”组成一行显示,然后添加两个标记行,用 sort 排序一下上面的输出,取出两个标记行中间的部分就是我们所要获取的符合时间段的文件。
我简要解释一下代码,主程序段由 if 和 else 两部分组成,程序从命令行正常调用会执行 if 部分,但是这一部分是一段递归循环调用,代码会以 $ 为第一参数重新调用批处理本身,但是再次执行时执行的是 else 部分。所以如果想将程序转换为顺序程序的话,可以将 else 部分置于 if 部分前,并将输出定向到文件,然后再在 if 部分用 for 将文件打开并进行处理就行了。关于标记部分,%Bdate% 中保存的是靠前的时间,在其后追加 00:00 可以保证在排序后所有大于 %Bdate% 时间的文件都会排列在其后面。%Edate% 保存的是靠后的时间,99:99 是非法的时间,但可以保证排序后所有小于 %Edate% 时间的文件都会排列在其前面。:sub 段就比较好理解了就是截取两个标记之间的部分。这样递归调用处理的好处是避免写入临时文件。有问题的欢迎继续讨论。
I've always wanted to improve this code and make a batch processing like forfiles.exe, but for many reasons, it hasn't been completed.
I also always thought that not many people were interested in how this code worked. If I had seen a discussion where someone wanted to delve deeper earlier, I would have shared my little insights earlier.
The principle of the code is very simple. Display a line composed of the "modification time" and "file name" of all files, then add two marker lines, sort the above output with sort, and take the part between the two marker lines is the files that meet the time period we want to obtain.
I will briefly explain the code. The main program segment consists of if and else parts. The program will execute the if part when called normally from the command line, but this part is a recursive loop call. The code will re - call the batch processing itself with $ as the first parameter, but when it is executed again, the else part is executed. So if you want to convert the program into a sequential program, you can place the else part before the if part and direct the output to a file, and then process it by opening the file with for in the if part. Regarding the marker part, %Bdate% stores the earlier time, appending 00:00 to it can ensure that all files with a time greater than %Bdate% will be arranged after it after sorting. %Edate% stores the later time, 99:99 is an illegal time, but it can ensure that all files with a time less than %Edate% will be arranged before it after sorting. The :sub segment is relatively easy to understand, which is to intercept the part between the two markers. The advantage of this recursive call processing is to avoid writing temporary files. Welcome to continue the discussion if you have any questions.
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-10-14 06:14 |
|
|
vkill
金牌会员
     
积分 4103
发帖 1744
注册 2006-1-20 来自 甘肃.临泽
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
用 sort 排序一下
就是这个没有看懂
Sort it with sort. Just didn't understand this.
|
|
2006-10-14 08:04 |
|
|
xyxFlysky
新手上路

积分 11
发帖 5
注册 2006-10-19
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
。。。
唉,要慢慢来呀,没几个能看懂的 -_-
...
Alas, we have to take it slow. Not many can understand it -_-
|

公道不在人心,是非只在时势 |
|
2006-10-20 09:41 |
|
|
9527
银牌会员
     努力做坏人
积分 1185
发帖 438
注册 2006-8-28 来自 北京
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
对于很多人还没有看懂,粗略在解释一下代码...
@echo off
if "%1" NEQ "$" (
set Bdate=%1
set Edate=%2
for /f "tokens=1,2 delims=§" %%a in ('"%~0" $^|sort') do (
call :sub "%%a" "%%b"
)
) else (
for %%i in (*) do echo %%~ti§%%i
echo.%Bdate% 00:00
echo.%Edate% 99:99
)
goto :EOF
:sub
if "%~1" == "%Edate% 99:99" set flag=0
if "%flag%" == "1" echo."%~2"
if "%~1" == "%Bdate% 00:00" set flag=1
goto :EOF
本人表达能力不是很好,基本上说是根本不成,对于以上代码,很多人还是不太理解,好,我们可以分三部分来解释。
1.运行P处理,对于IF的判断他会重新调用本身并以 $ 为参数1来运行,其次就会执行以下代码:
) else (
for %%i in (*) do echo %%~ti§%%i
echo.%Bdate% 00:00
echo.%Edate% 99:99
2.以上代码是获得当前所有文件的修改日期以及文件名称的列表,主要形式体现如下
例如我们开始日期设置为 2006-06-30 截至日期为 2006-07-09
那么ELSE后面的语句执行结果会有如下形式
2006-05-03 00:00§good.jpg
2006-07-01 00:00§namejm.txt
2006-08-13 00:00§ok.exe
2006-07-06 00:00§est.txt
2006-06-30 00:00
2006-07-09 99:99
然而得到文件修改日期和名称并没有完成 接着就要SORT排序一下了,把符合日期的排序在 2006-06-30 和 2006-07-09 之间,结果如下:
2006-05-03 00:00§good.jpg
2006-06-19 00:00§est.txt
2006-06-30 00:00
2006-07-01 00:00§namejm.txt
2006-07-06 00:00§est.txt
2006-07-09 99:99
2006-08-13 00:00§ok.exe
3,最后一段代码取得在日期2006-06-30和2006-07-09之间的
2006-07-01 00:00§namejm.txt
2006-07-06 00:00§est.txt
并取得以“§”为分隔符后面的符合条件的文件名
基本情况报告完毕,希望无奈何不要介意.......
Last edited by 9527 on 2006-10-20 at 20:42 ]
For many people who haven't understood it yet, let's briefly explain the code...
@echo off
if "%1" NEQ "$" (
set Bdate=%1
set Edate=%2
for /f "tokens=1,2 delims=§" %%a in ('"%~0" $^|sort') do (
call :sub "%%a" "%%b"
)
) else (
for %%i in (*) do echo %%~ti§%%i
echo.%Bdate% 00:00
echo.%Edate% 99:99
)
goto :EOF
:sub
if "%~1" == "%Edate% 99:99" set flag=0
if "%flag%" == "1" echo."%~2"
if "%~1" == "%Bdate% 00:00" set flag=1
goto :EOF
My expression ability is not very good. Basically, it's really not good. For the above code, many people still don't understand it very well. Well, we can explain it in three parts.
1. When running the P processing, for the IF judgment, it will call itself again and run with $ as parameter 1. Then the following code will be executed:
) else (
for %%i in (*) do echo %%~ti§%%i
echo.%Bdate% 00:00
echo.%Edate% 99:99
2. The above code is to get the list of the modification dates and file names of all current files. The main form is reflected as follows
For example, if we set the start date as 2006-06-30 and the end date as 2006-07-09
Then the execution result of the statement after ELSE will be in the following form
2006-05-03 00:00§good.jpg
2006-07-01 00:00§namejm.txt
2006-08-13 00:00§ok.exe
2006-07-06 00:00§est.txt
2006-06-30 00:00
2006-07-09 99:99
However, getting the file modification date and name is not complete. Then it is necessary to sort it. Sort the ones that meet the date between 2006-06-30 and 2006-07-09. The result is as follows:
2006-05-03 00:00§good.jpg
2006-06-19 00:00§est.txt
2006-06-30 00:00
2006-07-01 00:00§namejm.txt
2006-07-06 00:00§est.txt
2006-07-09 99:99
2006-08-13 00:00§ok.exe
3. The last segment of code gets the ones between the dates 2006-06-30 and 2006-07-09
2006-07-01 00:00§namejm.txt
2006-07-06 00:00§est.txt
And get the file names that meet the conditions after the "§" delimiter
The basic situation report is completed. I hope Wunaaihe don't mind.......
Last edited by 9527 on 2006-10-20 at 20:42 ]
|

我今后在论坛的目标就是做个超级坏人!!! |
|
2006-10-20 20:34 |
|
|
无奈何
荣誉版主
      
积分 1338
发帖 356
注册 2005-7-15
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
Re 9527
谢谢兄的注解。我个人的表达能力太差劲了,解释了一遍也没有说清楚,我也很苦恼,不知道哪一部分说明欠详细,正愁不知怎么回帖呢。^_^看来贴一些实例片断有助于理解。
Re 9527
Thanks for your annotation, brother. My own expression ability is too poor. After explaining once, I still didn't make it clear. I'm also very distressed. I don't know which part of the explanation is not detailed enough. I'm worrying about how to reply. ^_^ It seems that posting some example snippets can help with understanding.
|

☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul
|
|
2006-10-20 23:07 |
|