![]() |
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 19:10 |
47,811 topics / 349,895 posts / today 0 new / 48,253 members |
| DOS批处理 & 脚本技术(批处理室) » [Help] Select and delete time-named files and classify them |
| Printable Version 2,878 / 41 |
| Floor1 easonL | Posted 2008-05-23 10:49 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
Now we need to process log files. The naming rule of the file name is as follows:
.log Example) D:\tabiplaza\yado\debug\20080522000200.log Batch processing requirements: 1. Delete files that are six months ago. 2. Classify files that are five months ago. Put each file into the corresponding folder according to the month, and name the folder with the month. For example: D:\tabiplaza\yado\debug\20080522000200.log -> folder: 200805 Since I am initially exposed to batch processing and basically at the entry-level state, I ask the moderator and all the experts to help come up with ideas! Thank you very much! (In addition, I didn't find any results when searching, and it may also be that the search keywords are incorrect. I hope to get some guidance!) [ Last edited by easonL on 2008-5-26 at 03:19 PM ] |
|
| Floor2 HAT | Posted 2008-05-23 11:20 |
| 版主 Posts 5,017 Credits 9,023 | |
|
Assuming that the logs from 5 months ago have been classified, do we need to check if the files in the judgment classification folder exceed 6 months when processing next month?
|
|
| Floor3 easonL | Posted 2008-05-23 11:25 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
Needed, my idea is:
If we execute the batch processing next month, first, judge according to the file name. If it is a folder from six months ago, directly delete the entire folder. For example: Now it is May 2008, then the folder 200712 I will directly delete it |
|
| Floor4 26933062 | Posted 2008-05-23 13:34 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
Run in the D:\tabiplaza\yado\debug folder,
Not tested... [ Last edited by 26933062 on 2008-5-23 at 03:38 PM ] |
|
| Floor5 easonL | Posted 2008-05-23 14:01 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
Originally posted by 26933062 at 2008-5-23 01:34 PM: |
|
| Floor6 26933062 | Posted 2008-05-23 14:05 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
It flashed and then disappeared. There was an error in the code. Sorry, I carelessly wrote %yy% as %yy%%. Now it has been corrected, let's try again.
|
|
| Floor7 easonL | Posted 2008-05-23 14:10 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
When running to the line "for /f "delims=" %%a in ('dir/b/ad') do if %%a leq %yy%% rd/q/s "%%a"", an error occurs:
The syntax of the command is incorrect. The reason is under investigation |
|
| Floor8 26933062 | Posted 2008-05-23 14:15 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
I said it's wrong here, you re - copy the code from floor 4 and give it a try...
|
|
| Floor9 easonL | Posted 2008-05-23 14:17 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:-\. " %%a in ("%date%") do ( set /a yy=%%a,mm=100%%b%%100-5 ) if %mm% leq 0 set /a nn=1 set /a m=(12+mm)%%12,y=yy-nn if %m% equ 0 set m=12 set m=0%m% set yy=%y%%m:~-2% for /f "delims=" %%a in ('dir/b/ad') do if %%a leq %yy% rd/q/s "%%a" for /f "delims=" %%a in ('dir/b/a-d *.log') do ( set "str=%%~nxa" if !str:~0,6! leq %yy% (del/q "%%a") else ( if not exist !str:~0,6!\ md !str:~0,6! move "%%a" "!str:~0,6!" ) ) pause ---------------------------------------------------------------------------------------- The error when running after adding the two operation variables %yy% later is: 6! was unexpected at this time. It seems there is still an error in the last if sentence. Hehe, take some time to look again, thank you |
|
| Floor10 26933062 | Posted 2008-05-23 14:28 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
Sweat! Indeed,
It has been modified and tested successfully... |
|
| Floor11 easonL | Posted 2008-05-23 14:31 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
Hehe, yes! Now it can be classified, thanks!
Boss, but the function to delete files that have exceeded the time has not been implemented, hehe |
|
| Floor12 26933062 | Posted 2008-05-23 14:34 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
I can test it!
if !str! leq %yy% (del/q "%%a") Is this sentence for deleting files? |
|
| Floor13 easonL | Posted 2008-05-23 14:42 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
Ah? No way, when I tested, I always created the corresponding folder and put the files in it (including those from six months ago). I think it might be a difference in directory structure?
The directory structure here is: Files and folders are placed under D:\tabiplaza\yado\debug\ Is that so? |
|
| Floor14 26933062 | Posted 2008-05-23 14:47 |
| 银牌会员 Posts 879 Credits 2,268 | |
|
I created the following log files in the abcd folder.
Then I ran the code from floor 4. In the abcd folder, only the 200801-----2008-05 folder remains, and the corresponding files are inside. There are no longer any files starting with 2007. Is that incorrect? : |
|
| Floor15 easonL | Posted 2008-05-23 14:54 |
| 初级用户 Posts 56 Credits 118 From 南京 | |
|
Fainted, it turns out the directory structure is different. Hehe, it's my fault for not being able to understand the code well. I'm a newbie, newbie.
Boss, two questions: 1. The bat files are placed in D:\tabiplaza\yado\debug, which should be correct! 2. What's the directory path of your abcd folder? Hey, if I weren't in a hurry to get it done, I would definitely wait until I can fully understand the code before asking you carefully! |
|
| 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |