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,338 Replies 35
Floor 16 Posted 2008-08-21 16:38 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
[ Last edited by flyinspace on 2008-8-21 at 04:45 PM ]
知,不觉多。不知,乃求知
Floor 17 Posted 2008-08-21 16:42 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Ask. What is the file name you generated? 20080812? Like that?
知,不觉多。不知,乃求知
Floor 18 Posted 2008-08-21 16:49 ·  新加坡
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
The top floor has a description
2008-08-18
Floor 19 Posted 2008-08-21 16:51 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Now suppose all the files in your folder are generated by date. (I don't care what the file names are)

Don't mess with creating your own folders randomly. : ) Hehe.

Then here's a simple code. But it's not very safe, you need to ensure that the files are not messed up.

Here's a one - sentence code.

"skip" means skip the first 5.


for /f "skip=5 tokens=* " %%i in ('dir /b /o-d /ad-h-s') do echo The file name to delete is: , syntax format:


This code can be tested. If it meets your conditions, then you can take out the things after rd...

Remember, the above conditions need to be met under the condition that you said a backup folder is created every day. Moreover, there should be no other folders in this backup folder. Only backup files can exist.

[ Last edited by flyinspace on 2008 - 8 - 21 at 04:59 PM ]
知,不觉多。不知,乃求知
Floor 20 Posted 2008-08-21 18:01 ·  中国 江苏 常州 武进区 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Translate an old post for you. I don't know if the time format matches yours


@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 rd /s /q "%ph%\%%k"
)
pause&exit
简单!简单!再简单!
Floor 21 Posted 2008-08-23 22:46 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
Are VBS files runnable under Windows 98, XP, 2000, 2003 systems?

HAT's automatic delete files N days ago VBS is somewhat incomprehensible

--------------------------------------------------------------------------------
I want to automatically delete files older than 15 days in the directory rardata under win2000. How to modify it? Forfile is more convenient under win2003.




@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"
)
)

[ Last edited by qwertl on 2008-8-23 at 10:56 PM ]
Floor 22 Posted 2008-08-23 23:32 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
```
@echo off
setlocal enabledelayedexpansion
set SrcDir=D:\rardata
>"%tmp%\MyDel.vbs" echo dt=date()-15
>>"%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
for /f "tokens=*" %%a in ('dir /s /b /a-d "%SrcDir%"') do (
set FileDate=%%~ta
set FileDate=!FileDate:-=!
set FileDate=!FileDate:~0,8!
if !FileDate! lss %DateFlag% (
del /f /q "%%a"
)
)
```
Floor 23 Posted 2008-08-24 18:58 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
The format of %data% in win2000 is: Sunday 2008-08-24
Floor 24 Posted 2008-08-24 20:05 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Still don't know how to modify this? You can take a look at the help of set.
@echo off
setlocal enabledelayedexpansion
set SrcDir=D:\rardata
>"%tmp%\MyDel.vbs" echo dt=date()-15
>>"%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
for /f "tokens=*" %%a in ('dir /s /b /a-d "%SrcDir%"') do (
set FileDate=%%~ta
set FileDate=!FileDate:-=!
set FileDate=!FileDate:~4,10!
if !FileDate! lss %DateFlag% (
del /f /q "%%a"
)
)
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
qwertl +2 2008-09-15 02:13
Floor 25 Posted 2008-08-24 22:31 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
Thanks for HAT's patient guidance!

Now I'm looking at the content of the delay variable, and there's really a lot of batch processing content.

If the batch processing to be executed is not stored in the D:\rardata directory, the dir after for /f "tokens=*" %%a in ('dir /s /b /a-d "%SrcDir%"' should specify the directory path of the file to be deleted, right?

What do these two sentences do?
s=right(year(dt),4) ^& right("0" ^& month(dt),2) ^& right("0" ^& day(dt),2)

wscript.echo s

[ Last edited by qwertl on 2008-8-24 at 10:46 PM ]
Floor 26 Posted 2008-08-24 23:06 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Batch scripts can be placed anywhere because the data directory is specified here:
set SrcDir=D:\rardata

s=right(year(dt),4) ^& right("0" ^& month(dt),2) ^& right("0" ^& day(dt),2)
wscript.echo s
Call VBS to get the formatted system date:
http://www.cn-dos.net/forum/viewthread.php?tid=41857&fpage=1&highlight=%2BHAT
Floor 27 Posted 2008-08-25 20:47 ·  中国 浙江 宁波 北仑区 电信
初级用户
Credits 110
Posts 41
Joined 2007-03-31 17:20
19-year member
UID 83521
Gender Male
Status Offline
I've finished this. The method used is relatively old-fashioned

set usbdisk=E:
set befor2days=
set nowdate=

echo wscript.echo dateadd("d",-2,date) >%tmp%\tmp.vbs
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
if %m% LSS 9 set m=0%m%
if %d% LSS 9 set d=0%d%
set/a befor2days=%y%%m%%d%

echo wscript.echo dateadd("d",-0,date) >%tmp%\tmp.vbs
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set y=%%i
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set m=%%j
for /f "tokens=1,2,3* delims=-" %%i in ('cscript /nologo %tmp%\tmp.vbs') do set d=%%k
if %m% LSS 9 set m=0%m%
if %d% LSS 9 set d=0%d%
set/a nowdate = %y%%m%%d%

........Omit in the middle, the date is sorted into the format of 20080825 and used as the directory.

dir %usbdisk%\ /ad/on/b>%tmp%\usbdir.tmp
for /f "delims=" %%i in (%tmp%\usbdir.tmp) do (
if %%i lss %befor2days% (
rd /s/q %usbdisk%\%%i
)
下面 is to judge whether it is 2 days ago. If it is, delete it; if not, skip.
Hope the experts don't laugh at me. The solution I came up with is a bit clumsy.
Floor 28 Posted 2008-09-15 02:09 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
We often need to search for files in daily work. When it comes to the most powerful file search tool under Linux, find is definitely the one. find can filter files from file names, reading and modification times, sizes, types, etc., and can complete almost all daily search needs.

Let's take a simple example:

find /tmp/ -name 'galeki*'

This means finding files starting with "galeki" in the /tmp directory. -name indicates that it is filtering by file name. Isn't it very simple and clear? In addition to file names, find has more powerful filtering conditions:

1. Time:

Parameter Meaning
-atime n Files whose last access time was within n*24 hours Using Linux's find is simple
-ctime n Files whose last status change was within n*24 hours
-mtime n Files whose last modification time was within n*24 hours
-newer file Files that are newer than the file referred to by file
Floor 29 Posted 2008-10-20 23:28 ·  中国 湖南 永州 电信
初级用户
Credits 64
Posts 37
Joined 2007-03-14 03:38
19-year member
UID 81659
Gender Male
Status Offline
None of the tests succeeded..

The time format of 2000 is:

Current date: 2008-10-20 Monday

Hope some expert can give a complete one
Floor 30 Posted 2008-10-21 01:15 ·  中国 福建 三明 电信
初级用户
★★
Credits 175
Posts 108
Joined 2008-05-19 01:24
18-year member
UID 119060
Gender Male
Status Offline
Here's the translation:

Translate an old post for you, tested successfully

@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 "delims=0 tokens=*" %%i in ("!p%%a!") do set p%%a=%%i
)
if %p3% gtr %TT% goto ppp
:nxt
set/a p2-=1
if %p2% equ 0 set/a p1-=1&&set p2=12
SET/a PN1=%p1%%%4
set PN2=28
if %PN1% EQU 0 set pN2=29
for /f "tokens=%p2%" %%i in ("31 %pN2% 31 30 31 30 31 31 30 31 30 31") do set/a p3=%p3%+%%i
if %p3% leq %tt% goto nxt
:ppp
set/a p3=%p3%-%tt%
set p2=0%p2%
set p2=%p2:~-2,2%
set p3=0%p3%
set p3=%p3:~-2,2%
for /r "C:\New Folder\" %%i in (*.*) do set t=%%~ti &&call :loop "%%~fi"
pause
goto :eof
:loop
if not "%~1" == "%~f0" (
IF %p1%%p2%%p3% gtr %t:~0,4%%t:~5,2%%t:~8,2% del %1
)
Forum Jump: