![]() |
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-10 07:57 |
47,811 topics / 349,897 posts / today 2 new / 48,255 members |
| DOS批处理 & 脚本技术(批处理室) » [Closed] Batch script to automatically delete file directories older than N days |
| Printable Version 3,339 / 35 |
| Floor1 sziang | Posted 2008-08-18 16:03 |
| 新手上路 Posts 7 Credits 16 | |
|
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 ] |
|
| Floor2 HAT | Posted 2008-08-18 16:26 |
| 版主 Posts 5,017 Credits 9,023 | |
|
```@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" ) ) ``` |
|
| Floor3 sziang | Posted 2008-08-18 17:41 |
| 新手上路 Posts 7 Credits 16 | |
|
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!
|
|
| Floor4 HAT | Posted 2008-08-18 17:48 |
| 版主 Posts 5,017 Credits 9,023 | |
|
```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" ) ) ``` |
|
| Floor5 sziang | Posted 2008-08-18 18:22 |
| 新手上路 Posts 7 Credits 16 | |
|
The test is still not successful... Please be more specific, thanks.
|
|
| Floor6 HAT | Posted 2008-08-18 19:24 |
| 版主 Posts 5,017 Credits 9,023 | |
|
Save the code as xxx.bat. Double-click to run it and paste the result here.
|
|
| Floor7 abcd | Posted 2008-08-18 20:35 |
| 银牌会员 Posts 739 Credits 1,436 | |
|
Such things already exist in the forum, so don't post or reply.
|
|
| Floor8 sziang | Posted 2008-08-19 10:10 |
| 新手上路 Posts 7 Credits 16 | |
|
Running result, the picture has been uploaded
[ Last edited by sziang on 2008-8-19 at 10:34 AM ] |
|
| Floor9 HAT | Posted 2008-08-19 10:47 |
| 版主 Posts 5,017 Credits 9,023 | |
|
Can't see the picture. First, upload the picture to the forum, then put the link inside the img tag.
|
|
| Floor10 wangwei4106 | Posted 2008-08-19 10:53 |
| 新手上路 Posts 8 Credits 15 | |
|
Come to earn some points to view higher-level ones...
|
|
| Floor11 sziang | Posted 2008-08-19 15:29 |
| 新手上路 Posts 7 Credits 16 | |
|
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 ] |
|
| Floor12 HAT | Posted 2008-08-19 15:32 |
| 版主 Posts 5,017 Credits 9,023 | |
|
Run the code on the 6th floor and post the result for us to see.
|
|
| Floor13 sziang | Posted 2008-08-19 16:35 |
| 新手上路 Posts 7 Credits 16 | |
|
Did you see the picture? Please help
|
|
| Floor14 HAT | Posted 2008-08-19 17:21 |
| 版主 Posts 5,017 Credits 9,023 | |
|
The code correction is as follows:
|
|
| Floor15 sziang | Posted 2008-08-21 15:47 |
| 新手上路 Posts 7 Credits 16 | |
|
Still no use, the running result is the same as I posted that day
|
|
| 1 2 3 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |