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-08-10 06:55
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Batch script to automatically delete file directories older than N days View 3,332 Replies 35
Original Poster Posted 2008-08-18 16:03 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
I am a temporary network administrator in my unit. Backing up the data of the OA server is one of the daily work contents. Now I am copying all the folders and files that should be backed up on the OA server to a folder named with the current date (such as 2008-08-18) in full, and it is also a batch processing that is automatically executed every day. Since the content of the backup is getting larger and larger (a backup reaches 27.6G), I want to find a batch processing file that can automatically delete the backup directories older than five days before the backup (only keep the backup records of the past five days). Thanks in advance! My system is XP_SP1

[ Last edited by HAT on 2008-10-20 at 23:35 ]
Floor 2 Posted 2008-08-18 16:26 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
```@echo off
>"%temp%\MyDate.vbs" echo dt=date()-5
>>"%temp%\MyDate.vbs" echo s=right(year(dt),4) ^& "-" ^& right("0" ^& month(dt),2) ^& "-" ^& right("0" ^& day(dt),2)
>>"%temp%\MyDate.vbs" echo wscript.echo s
for /f %%a in ('cscript /nologo "%temp%\MyDate.vbs"') do set FiveDay=%%a
for /f %%a in ('dir /b /ad') do (
if "%%a" lss "%FiveDay%" (
echo rd /s /q "%%a"
)
)
```
Floor 3 Posted 2008-08-18 17:41 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
Thank you very much for the reply from friend HAT! Just now I tested the above batch file in the directory and it didn't work. I created a 000 folder in the root directory of drive D, and backed up the server files (including directories) to the folders from 2008-08-12 to 2008-08-18 generated in this directory. Now I just want to keep those after 2008-08-14. How to operate? I'm sorry, I'm a DOS newbie. Please be specific. Thanks again!
Floor 4 Posted 2008-08-18 17:48 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
```batch
@echo off
>"%temp%\MyDate.vbs" echo dt=date()-5
>>"%temp%\MyDate.vbs" echo s=right(year(dt),4) ^& "-" ^& right("0" ^& month(dt),2) ^& "-" ^& right("0" ^& day(dt),2)
>>"%temp%\MyDate.vbs" echo wscript.echo s
for /f %%a in ('cscript /nologo "%temp%\MyDate.vbs"') do set FiveDay=%%a
for /f %%a in ('dir /b /ad "D:\000"') do (
if "%%a" lss "%FiveDay%" (
rd /s /q "%%a"
)
)
```
Floor 5 Posted 2008-08-18 18:22 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
The test is still not successful... Please be more specific, thanks.
Floor 6 Posted 2008-08-18 19:24 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Save the code as xxx.bat. Double-click to run it and paste the result here.

>"%temp%\MyDate.vbs" echo dt=date()-5
>>"%temp%\MyDate.vbs" echo s=right(year(dt),4) ^& "-" ^& right("0" ^& month(dt),2) ^& "-" ^& right("0" ^& day(dt),2)
>>"%temp%\MyDate.vbs" echo wscript.echo s
for /f %%a in ('cscript /nologo "%temp%\MyDate.vbs"') do set FiveDay=%%a
for /f %%a in ('dir /b /ad "D:\000"') do (
if "%%a" lss "%FiveDay%" (
rd /s /q "D:\000\%%a"
)
)
pause
Floor 7 Posted 2008-08-18 20:35 ·  中国 浙江 杭州 中移铁通
银牌会员
★★★
Credits 1,436
Posts 739
Joined 2007-10-11 17:44
18-year member
UID 99469
Gender Male
Status Offline
Such things already exist in the forum, so don't post or reply.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
pusofalse -1 2008-08-23 22:59
Floor 8 Posted 2008-08-19 10:10 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
Running result, the picture has been uploaded


[ Last edited by sziang on 2008-8-19 at 10:34 AM ]
Floor 9 Posted 2008-08-19 10:47 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Can't see the picture. First, upload the picture to the forum, then put the link inside the img tag.
Floor 10 Posted 2008-08-19 10:53 ·  中国 广东 佛山 电信
新手上路
Credits 15
Posts 8
Joined 2008-08-19 10:13
17-year member
UID 123562
Gender Male
Status Offline
Come to earn some points to view higher-level ones...
Floor 11 Posted 2008-08-19 15:29 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
Backup folder 000 in the root directory of drive D

bat file

Running situation of bat file


[ Last edited by sziang on 2008-8-19 at 03:34 PM ]
Floor 12 Posted 2008-08-19 15:32 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Run the code on the 6th floor and post the result for us to see.
Floor 13 Posted 2008-08-19 16:35 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
Did you see the picture? Please help
Floor 14 Posted 2008-08-19 17:21 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
The code correction is as follows:

>"%temp%\MyDate.vbs" echo dt=date()-5
>>"%temp%\MyDate.vbs" echo s=right(year(dt),4) ^& "-" ^& right("0" ^& month(dt),2) ^& "-" ^& right("0" ^& day(dt),2)
>>"%temp%\MyDate.vbs" echo wscript.echo s
for /f %%a in ('cscript /nologo "%temp%\MyDate.vbs"') do set FiveDay=%%a
for /f "tokens=*" %%a in ('dir /b /ad "D:\000"') do (
if "%%a" lss "%FiveDay%" (
rd /s /q "D:\000\%%a"
)
)
pause
Floor 15 Posted 2008-08-21 15:47 ·  中国 江苏 盐城 电信
新手上路
Credits 16
Posts 7
Joined 2008-08-18 15:18
17-year member
UID 123498
Gender Male
Status Offline
Still no use, the running result is the same as I posted that day
Forum Jump: