中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-08 22:17
47,811 topics / 349,895 posts / today 0 new / 48,253 members
DOS批处理 & 脚本技术(批处理室) » [Original] Find files that meet the time range with batch processing
Printable Version  16,497 / 23
Floor1 无奈何 Posted 2006-07-31 22:14
荣誉版主 Posts 356 Credits 1,338
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 ]
Floor2 shijianxin Posted 2006-08-01 14:24
初级用户 Posts 52 Credits 115
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~~
Floor3 3742668 Posted 2006-08-01 14:30
荣誉版主 Posts 718 Credits 2,013
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:

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.
Floor4 electronixtar Posted 2006-08-01 15:11
铂金会员 Posts 2,672 Credits 7,493
Helpless, the version master's bat has reached... an incomprehensible realm!
Floor5 buddiyar Posted 2006-08-30 19:16
初级用户 Posts 75 Credits 160
Make a mark, learn slowly later, heh heh
Floor6 IceCrack Posted 2006-08-30 20:24
中级用户 Posts 168 Credits 332 From 天涯
Hey, couldn't log in for a few days. There are a few more featured posts! Hurry up and study them
Floor7 namejm Posted 2006-08-30 20:31
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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
Floor8 kennyfan Posted 2006-09-19 21:24
中级用户 Posts 112 Credits 259
It's complicated.. After looking around.. I'm so dizzy that my head spins!~~
Floor9 vkill Posted 2006-10-13 23:40
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
Now 3742668 moderator can understand, but some moderators still can't understand
Floor10 9527 Posted 2006-10-14 02:43
银牌会员 Posts 438 Credits 1,185 From 北京
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.
Floor11 无奈何 Posted 2006-10-14 06:14
荣誉版主 Posts 356 Credits 1,338
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.
Floor12 vkill Posted 2006-10-14 08:04
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
Sort it with sort. Just didn't understand this.
Floor13 xyxFlysky Posted 2006-10-20 09:41
新手上路 Posts 5 Credits 11
...

Alas, we have to take it slow. Not many can understand it -_-
Floor14 9527 Posted 2006-10-20 20:34
银牌会员 Posts 438 Credits 1,185 From 北京
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 ]
Floor15 无奈何 Posted 2006-10-20 23:07
荣誉版主 Posts 356 Credits 1,338
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023