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-27 03:49
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Automatically delete the specified file 100 days after the file is generated, and not in the trash can View 5,076 Replies 21
Original Poster Posted 2007-01-14 22:24 ·  中国 广东 广州 联通
新手上路
Credits 6
Posts 2
Joined 2007-01-14 21:54
19-year member
UID 76518
Gender Male
From 广州市
Status Offline
Due to work needs, there are a large number of computers that need to manually delete specified files 100 days after they are generated. There are always some machines that forget to delete them. Can any expert give some advice? Files are timed to be deleted, but they should not be regarded as viruses by the antivirus software.
Floor 2 Posted 2007-01-14 23:55 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
The key to the problem is to judge what the date was 100 days ago today. It is related to the judgment of carrying over and removing zeros for the units and tens digits like 28, 29, 30, 31, and for the hundreds digit like 12 carrying over and removing zeros. I feel that this key point cannot be easily realized with batch processing.
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 3 Posted 2007-01-15 00:13 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
There is also the judgment of common years and leap years. Although the idea is simple, the code will be very cumbersome.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 4 Posted 2007-01-15 00:14 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
A more direct way is:

Run it every three months!

schtasks /create /tn "My App" /tr "Full path of the program you want to execute" /sc monthly /mo 3
Floor 5 Posted 2007-01-15 00:18 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
2, 3 floors

Look at the code



@echo off
setlocal enabledelayedexpansion
set dy=%date:~0,4%
set dm=%date:~5,2%
set dd=%date:~8,2%
echo %dy%-%dm%-%dd%
echo Calculate the date after 100 days.
echo.
pause>nul

rem First check if the date dd is in the following conditions (3-12), consider January and February specially!
rem Also, the default is that the year dy is a common year, and finally consider leap years.
rem So you can just add one to dd in the end!

for %%i in (3,5,7,8,10,12) do (
rem Check Odd Month (single month).
if "%dm%" equ "%%i" (
set /a dm+=3
set /a dd+=8
rem At this time, dm has become a double month.
goto :checkdd
)
)
rem Check Even Month (double month).
for %%j in (4,6,9,11) do (
if "%dm%" equ "%%j" (
set /a dm+=3
set /a dd+=9
rem At this time, dm has become a single month.
goto :checkdd
)
)

echo.
echo %dm%
pause

:jan-feb
rem Consider the situation of January and February.
if "%dm%" equ "1" (
set /a dm+=3
set /a dd+=10
goto :chckdd
)
if "%dm%" equ "2" (
set /a dm+=3
set /a dd+=10
goto :chckdd
)

:checkdd
rem At this time, dm has become a double month.
rem Check if the date is greater than 30.
if "%dd%" gtr "30" (
rem For example, June 24th should be changed to July 2nd
set dm%+=1 & set /a ddd=!dd!%-30
)

rem At this time, dm has become a single month.
rem Check if the date is greater than 31.
if "%dd%" gtr "31" (
rem For example, July 24th should be changed to August 2nd.
set dm%+=1 set /a ddd=!dd!%-31
)

:checkYear
rem Check common year or leap year, as long as it is not a common year "... neq 0", add one to dd.
set /a rmn=dy%\4
if "%rmn%" neq "0" (set /a dd+=1)

::Output the Result.
echo The day in 100 is: %dy%-%dm%-%dd%
schtasks /create /tn "DeleteTask" /m %dm% /tr "Full path of the program"
pause>nul



Can't see the result, please help correct it.
Thank you!
Floor 6 Posted 2007-01-15 00:33 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
The calculation method for leap years is incorrect. The correct algorithm is: A year that is divisible by 4 but not by 400 is not a leap year. For example, the year 2000 is a leap year, but the year 1900 is not a leap year;

The writing of `set dm%+=1` is incorrect. There is no such variable reference method, and when doing arithmetic operations, the switch `/a` should be added;

In addition, there is no need to handle January and February together. It is more reasonable to handle them together with large months like March, May, etc.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2007-01-15 00:45 ·  中国 江西 赣州 电信
高级用户
★★
论坛上抢劫的
Credits 551
Posts 246
Joined 2006-09-21 12:35
19-year member
UID 63270
Status Offline
Moderator namejm's collection!
:: DateDel2.bat - Delete files modified before a specified number of days in the specified path
:: Will Sort - 2005-9-2 - CMD@WinXP
:: Notes: Parameter %1 specifies the number of days, where yesterday is 1, the day before yesterday is 2, and so on
:: Parameter %1 specifies the file path, and if omitted, the current path will be processed
:: Core algorithm: Ritchie Lawrence, updated 2002-08-13. Version 1.1
::
@echo off & setlocal ENABLEEXTENSIONS
call :Date2Day %date:~0,10% sdays
set /a sdays-=%1
call :Day2Date %sdays% difdate
echo The following files modified before %difdate% in %cd% will be deleted:
for /r %2 %%f in (*.*) do if "%%~tf" LEQ "%difdate%" echo "%%f"
goto :EOF

:Date2Day
setlocal ENABLEEXTENSIONS
for /f "tokens=1-3 delims=/-, " %%a in ('echo/%1') do (
set yy=%%a & set mm=%%b & set dd=%%c
)
set /a dd=100%dd%%%100,mm=100%mm%%%100
set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
endlocal&set %2=%j%&goto :EOF

:Day2Date
setlocal ENABLEEXTENSIONS
set /a i=%1,a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
(if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
endlocal&set %2=%yy%-%mm%-%dd%&goto :EOF

Later, I searched and found that this code is from willsort!! Haha, the second line of the original code clearly states it's from willsort!!
Here are two linkshttp://www.cn-dos.net/forum/viewthread.php?tid=23926&fpage=1&highlight=%E7%BA%AF%E6%95%B0%E5%AD%97&page=1
http://www.cn-dos.net/forum/viewthread.php?tid=16676

[ Last edited by lotus516 on 2007-1-15 at 01:15 AM ]
Floor 8 Posted 2007-01-15 01:05 ·  中国 山东 济南 电信
社区乞丐
★★
此图片另存后死机
Credits -49
Posts 90
Joined 2006-12-02 13:00
19-year member
UID 72412
Gender Male
Status Offline
!!!!!!!!!!!!!!!!!!!!Willsort is regarded as a god!!!!!!!!!!!!!!!!!!!!!

It makes me just want to cry....5555555555555555555555

[ Last edited by tghksj on 2007-1-14 at 12:11 PM ]
我的网络笔记本.非联系本人请勿访问!http://w.vicp.net
Floor 9 Posted 2007-01-15 05:45 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Floor 10 Posted 2007-01-15 05:52 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Originally posted by namejm at 2007-1-14 11:33:
The leap year calculation method is wrong. The correct algorithm is: A year that is divisible by 4 but not by 400 is not a leap year. For example, the year 2000 is a leap year, but the year 1900 is not a leap year;

  set ...


The set... part was a carelessness, thank you

But the problem with January and February is like this

Suppose the current date is January, then from January to February, it changes by 31 days, and from February to March it changes by 28 days (in a common year);
Suppose the current date is March, then from March to April it is 31 days, okay, the program has no logical error;
But from April to May, it is 30 days, then, can you still use set... +=28?
So, you can't group January and March, May, July, August, October, December together!
By the same token, February and April, June, September, November can't be grouped together.

Therefore, only January and February are considered separately, not because grouping them together means they have any problem!
I don't know what everyone's opinion is?
Floor 11 Posted 2007-01-15 05:54 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
The machine does not allow modifying the date to before 1979!!
Floor 12 Posted 2007-01-15 06:36 ·  中国 北京 中国科学院研究生院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline


@echo off
{::setlocal enabledelayedexpansion It is no need to add in! }
set dy=%date:~0,4%
set dm=%date:~5,2%
set dd=%date:~8,2%
echo Today is : %dy%-%dm%-%dd%
echo Calculate the date after 100 days.
echo.
pause>nul

rem First check if the date dd is in the following conditions (3-12), consider January and February specially!
rem Also, the default is that the year dy is a common year, and finally consider leap years.
rem So just add one to dd at the end!

for %%i in (3,5,7,8,10,12) do (
rem Check Odd Month (single month).
if %dm% equ %%i (
set /a dm+=3
set /a dd+=8
rem At this time, dm has become a double month.
goto :checkdd
)
)
rem Check Even Month (double month).
for %%j in (4,6,9,11) do (
if %dm% equ %%j (
set /a dm+=3
set /a dd+=9
rem At this time, dm has become a single month.
goto :checkdd
)
)

::jan and feb
rem Consider the situation of January and February.
if %dm% equ 1 (
set /a dm+=3
set /a dd+=10
goto :checkdd
)
if %dm% equ 2 (
set /a dm+=3
set /a dd+=10
goto :checkdd
)

:checkdd
rem At this time, dm has become a double month.
rem Check if the date is greater than 30.
if %dd% gtr 30 (
rem For example, June 24th should be changed to July 2nd.
set /a dm+=1
set /a ddd=%dd%-30
)

rem At this time, dm has become a single month.
rem Check if the date is greater than 31.
if %dd% gtr 31 (
rem For example, July 24th should be changed to August 2nd.
set /a dm+=1
set /a ddd=%dd%-31
)

:checkYear {Here, Be careful , the code is not the same with the former I writed!}
rem Check if it is a common year or a leap year. As long as it is not a common year "... equ 0", add one to dd.
set /a rmn=dy%4
set /a rmnn=dy%400
if %rmn% neq 0 (echo This year is not leap year!)
if %rmn% equ 0 (
if %rmnn% equ 0 (
echo This year is leap year!
set /a dd+=1
)
)

::Output the Result.
echo The day after 100 days is: %dy%-%dm%-%dd%!

:: "Attach any code executable here!"

pause>nul

Floor 13 Posted 2007-01-16 09:47 ·  中国 浙江 杭州 联通
初级用户
Credits 34
Posts 12
Joined 2007-01-16 08:07
19-year member
UID 76667
Gender Male
Status Offline
First of all, I very much admire everyone's professional knowledge and enthusiasm for learning.

I, the younger brother, have something to say. DOS is not like C++, can it be simple? Don't make it seem very profound, it should be popular!

The landlord's statement is: After the file is generated, the specified file is automatically deleted after 100 days, right?

For example: To delete the temporary folder on drive C. Just create an xxx.bat file. The content is:
@echo off
del /f /s /q %systemdrive%\*.tmp
Then put this bat in the system's built-in scheduled tasks and set it to run every 100 days, and that's it...
Floor 14 Posted 2007-01-18 14:48 ·  中国 广东 广州 天河区 联通
新手上路
Credits 6
Posts 2
Joined 2007-01-14 21:54
19-year member
UID 76518
Gender Male
From 广州市
Status Offline
Got it, very inspiring! Thank you everyone!
Floor 15 Posted 2007-01-20 11:55 ·  中国 重庆 江北区 电信
新手上路
Credits 16
Posts 8
Joined 2007-01-20 11:09
19-year member
UID 77138
Gender Male
Status Offline
The idea on the 13th floor is very inspiring. Why make it too complicated when it can be simple?
Forum Jump: