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 07:57
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Batch script to automatically delete file directories older than N days View 3,341 Replies 35
Floor 31 Posted 2008-10-21 18:38 ·  中国 辽宁 沈阳 和平区 电信
初级用户
Credits 64
Posts 37
Joined 2007-03-14 03:38
19-year member
UID 81659
Gender Male
Status Offline
Originally posted by dddyjq1 at 2008-10-21 01:15 AM:
Turn over an old post for you, tested and successful

@echo off
SET TT=1
set p1=%date:~0,4%
set p2=%date:~5,2%
set p3=%date:~8,2%
setlocal enabledelayedexpansion
for /l %%a in (1,1,3) do (
for /f "del ...


Still not working under 2000

Appears:

There should not be "at this time.
There should not be "at this time.
There should not be "at this time.
There should not be "at this time.
There should not be "at this time.
Floor 32 Posted 2008-10-21 19:48 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
My situation here:

@echo off
set TT=5&set "ph=d:\000"
for /f "tokens=1-3 delims=-/\:. " %%i in ("%date%") do set /a p1=%%i,p2=100%%j%%100,p3=100%%k%%100
if %p3% gtr %TT% goto ppp
:nxt
set/a p2-=1
if %p2% equ 0 set/a p1-=1,p2=12
set/a PN2="!(p1%%4)&!(!(p1%%100))|!(p1%%400)",PN2=28+PN2
for /f "tokens=%p2%" %%i in ("31 %pN2% 31 30 31 30 31 31 30 31 30 31") do set/a p3+=%%i
if %p3% leq %tt% goto nxt
:ppp
set/a p3-=tt
setlocal enabledelayedexpansion
for %%i in (p2 p3) do if !%%i! lss 10 set %%i=0!%%i!
for /f "skip=7 tokens=1,3*" %%i in ('dir /ad /tc "%ph%" 2^>nul^|find /v "字节"') do (
if %p1%-%p2%-%p3% gtr %%i echo rd /s /q "%ph%\%%k"
)
pause&exit
Attachments
aaa.gif
简单!简单!再简单!
Floor 33 Posted 2008-10-21 22:09 ·  中国 辽宁 沈阳 和平区 电信
初级用户
Credits 64
Posts 37
Joined 2007-03-14 03:38
19-year member
UID 81659
Gender Male
Status Offline
Originally posted by terse at 2008-10-21 07:48 PM:
Here is my situation:

@echo off
set TT=5&set "ph=d:\000"
for /f "tokens=1-3 delims=-/\:. " %%i in ("%date%") do set /a p1=%%i,p2=100%%j%%100,p3=100%%k%% ...


Test successful, thanks!
If you also need to delete the files under d:\000, how to handle it? It's troublesome...
Floor 34 Posted 2008-10-21 23:13 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by yuehong at 2008-10-21 22:09:

Test successful, thank you!!
What if the files under d:\000 also need to be deleted? How to handle it? It's troublesome...

Actually, this is what I gave you on floor 20. You actually bumped it up here.
To delete files, you need to first dir /a-d /b, then judge the time, and then del
简单!简单!再简单!
Floor 35 Posted 2008-10-27 16:18 ·  中国 四川 成都 电信
新手上路
Credits 1
Posts 1
Joined 2008-10-27 15:14
17-year member
UID 129239
Gender Male
Status Offline
Very good.
Floor 36 Posted 2008-11-03 11:56 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
In Windows 2000, for the code:

First, for the part of handling `FileDate`:

The original code for setting `FileDate` in Windows 2000:
```
set FileDate=08-10-22 17:56
set FileDate=!FileDate:-=!
set FileDate=!FileDate:~0,8!
if !FileDate! LSS 20081023 (echo ....)
```
Since in Windows 2000, the way of handling date format might have some differences, but the basic operations of replacing `-` and taking the first 8 characters are similar. Just make sure the date comparison is correct according to the actual situation.

For the part related to `MyDel.vbs`:

The code for creating and running the VBScript is the same in terms of logic. It calculates the date minus 11 days and then gets the formatted date string.

For the part of getting the file date and comparing:

The line `set FileDate=%%~ta` gets the file date and time. In Windows 2000, the format of `%%~ta` might be a bit different from XP and 2003, but the subsequent operations of replacing `-` and taking the first 8 characters are the same. Then comparing `!FileDate!` with `%DateFlag%` is the same logic.

The overall translated code remains as the original code structure but adjusted for the Windows 2000 environment considerations as described above.

The translated text of the entire code block as follows:

In win2003 it works normally to delete
set FileDate=2008-10-27 22:20
set FileDate=!FileDate:-=!
set FileDate=!FileDate:~0,8!
if !FileDate! LSS 20081018 (echo ....)

How to modify this code under win2000?
set FileDate=08-10-22 17:56
set FileDate=!FileDate:-=!
set FileDate=!FileDate:~0,8!
if !FileDate! LSS 20081023 (echo ....)

set desfolder=e:\rardata
setlocal enabledelayedexpansion
>"%tmp%\MyDel.vbs" echo dt=date()-11
>>"%tmp%\MyDel.vbs" echo s=right(year(dt),4) ^& right("0" ^&
month(dt),2) ^& right("0" ^& day(dt),2)
>>"%tmp%\MyDel.vbs" echo wscript.echo s
for /f %%a in ('cscript /nologo "%tmp%\MyDel.vbs"') do set DateFlag=%%a
echo %DateFlag%
pause
for /f "tokens=*" %%a in ('dir /s /b /a-d "%desfolder%"') do (
set FileDate=%%~ta The file date obtained here is different from that in xp and 2003.
set FileDate=!FileDate:-=!
set FileDate=!FileDate:~0,8!
if !FileDate! lss %DateFlag% (
echo "%%a"
)
)
pause
Forum Jump: