China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-22 13:38
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Original] Find files that meet the time range with batch processing DigestI View 15,480 Replies 23
Original Poster Posted 2006-07-31 22:14 ·  中国 辽宁 葫芦岛 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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



  1. @echo off
  2. if "%1" NEQ "$" (
  3. set Bdate=%1
  4. set Edate=%2
  5. for /f "tokens=1,2 delims" %%a in ('"%~0" $^|sort') do (
  6. call :sub "%%a" "%%b"
  7. )
  8. ) else (
  9. for %%i in (*) do echo %%~ti§%%i
  10. echo.%Bdate% 00:00
  11. echo.%Edate% 99:99
  12. )
  13. goto :EOF

  14. :sub
  15. if "%~1" == "%Edate% 99:99" set flag=0
  16. if "%flag%" == "1" echo."%~2"
  17. if "%~1" == "%Bdate% 00:00" set flag=1
  18. 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

Floor 2 Posted 2006-08-01 14:24 ·  中国 广东 东莞 电信
初级用户
★★
Credits 115
Posts 52
Joined 2005-08-07 22:10
20-year member
UID 41435
Gender Male
Status Offline
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~~
愿你们永远开心
Floor 3 Posted 2006-08-01 14:30 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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.
Floor 4 Posted 2006-08-01 15:11 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
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'>"
Floor 5 Posted 2006-08-30 19:16 ·  中国 江苏 苏州 电信
初级用户
★★
Credits 160
Posts 75
Joined 2006-06-28 01:07
19-year member
UID 57661
Gender Male
Status Offline
Make a mark, learn slowly later, heh heh
Floor 6 Posted 2006-08-30 20:24 ·  中国 辽宁 大连 教育网
中级用户
★★
DOS之友
Credits 332
Posts 168
Joined 2005-10-06 00:00
20-year member
UID 43171
Gender Male
From 天涯
Status Offline
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
Floor 7 Posted 2006-08-30 20:31 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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没商量。
考虑问题复杂化,解决问题简洁化。
Floor 8 Posted 2006-09-19 21:24 ·  中国 广东 广州 越秀区 电信
中级用户
★★
Credits 259
Posts 112
Joined 2006-09-18 04:55
19-year member
UID 62928
Gender Male
Status Offline
It's complicated.. After looking around.. I'm so dizzy that my head spins!~~
Floor 9 Posted 2006-10-13 23:40 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Now 3742668 moderator can understand, but some moderators still can't understand
Floor 10 Posted 2006-10-14 02:43 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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.
我今后在论坛的目标就是做个超级坏人!!!
Floor 11 Posted 2006-10-14 06:14 ·  中国 浙江 宁波 鹏博士宽带
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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

Floor 12 Posted 2006-10-14 08:04 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Sort it with sort. Just didn't understand this.
Floor 13 Posted 2006-10-20 09:41 ·  IANA 局域网IP(Private-Use)
新手上路
Credits 11
Posts 5
Joined 2006-10-19 09:15
19-year member
UID 66825
Gender Male
Status Offline
...

Alas, we have to take it slow. Not many can understand it -_-
公道不在人心,是非只在时势
Floor 14 Posted 2006-10-20 20:34 ·  中国 北京 联通
银牌会员
★★★
努力做坏人
Credits 1,185
Posts 438
Joined 2006-08-28 12:00
19-year member
UID 61449
From 北京
Status Offline
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 ]
我今后在论坛的目标就是做个超级坏人!!!
Floor 15 Posted 2006-10-20 23:07 ·  中国 浙江 宁波 鹏博士宽带
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
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

Forum Jump: