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 17:28
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] Select and delete time-named files and classify them View 2,667 Replies 41
Original Poster Posted 2008-05-23 10:49 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
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 ]
Floor 2 Posted 2008-05-23 11:20 ·  中国 上海 联通
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
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?
Floor 3 Posted 2008-05-23 11:25 ·  中国 江苏 南京 电信
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
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
Floor 4 Posted 2008-05-23 13:34 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Run in the D:\tabiplaza\yado\debug folder,
Not tested...

@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"
set "str=!str:~0,6!"
if !str! leq %yy% (del/q "%%a") else (
if not exist !str!\ md !str!
move "%%a" "!str!"
)
)
pause


[ Last edited by 26933062 on 2008-5-23 at 03:38 PM ]
致精致简!
Floor 5 Posted 2008-05-23 14:01 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
Originally posted by 26933062 at 2008-5-23 01:34 PM:
Run it in the D:\tabiplaza\yado\debug folder,
Not tested...
:

@echo off&setlocal enabledelayedexpansion
for /f "tokens=1,2 delims=:-\. " %%a in ("%date%") d ...



Thanks a lot. I'm testing it, but I encountered some problems. I'll continue to ask for advice.

1. I saved it in D:\tabiplaza\yado\debug and named it debug.bat.
2. I want it to output some prompt information, but the black screen of cmd always flashes and disappears. Why is that?

Hehe, I feel really stupid, but there's no way. Learning is always such a process. Please give me more advice. I'm very grateful!
Floor 6 Posted 2008-05-23 14:05 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
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.
致精致简!
Floor 7 Posted 2008-05-23 14:10 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
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
Floor 8 Posted 2008-05-23 14:15 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
I said it's wrong here, you re - copy the code from floor 4 and give it a try...
致精致简!
Floor 9 Posted 2008-05-23 14:17 ·  中国 江苏 南京 联通
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
@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
Floor 10 Posted 2008-05-23 14:28 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Sweat! Indeed,
It has been modified and tested successfully...
致精致简!
Floor 11 Posted 2008-05-23 14:31 ·  中国 江苏 南京 电信
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
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
Floor 12 Posted 2008-05-23 14:34 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
I can test it!
if !str! leq %yy% (del/q "%%a")
Is this sentence for deleting files?
致精致简!
Floor 13 Posted 2008-05-23 14:42 ·  中国 江苏 南京 电信
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
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?
Floor 14 Posted 2008-05-23 14:47 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
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?
:

200701dfjg.log
200702dfgs.log
200703fhhhgh.log
200710sfysh.log
200711sdfhsh.log
200712sfdhsh.log
200801sdfhsghgh.log
200802sdfhgsh.log
200805sfdhstsr.log
200803sfdhs.log
200804sghsgh.log
200701dfjgfg.log
200702dfgssdfg.log
200703fhhhghsdg.log
200710sfyshsfg.log
200711sdfhshsdfg.log
200712sfdhshsfg.log
200801sdfhsghghsfg.log
200802sdfhgshsdg80.log
200805sfdhstsrsdfg678.log
200803sfdhssgj68.log
200804sghsghh6.log
致精致简!
Floor 15 Posted 2008-05-23 14:54 ·  中国 江苏 南京 电信
初级用户
★★
E生迅徒
Credits 118
Posts 56
Joined 2008-05-22 16:34
18-year member
UID 119421
Gender Male
From 南京
Status Offline
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!
Forum Jump: