Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Operating System - Microsoft
cancel
Showing results for 
Search instead for 
Did you mean: 

file deletion script depending on dates

SOLVED
Go to solution
Edgar Zapata
Esteemed Contributor

file deletion script depending on dates

I need to delete logs older than X days.

Log file names go like this: ex070401.log

I was considering something like this .bat here:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=890651

But, this will not do because I need to delete the files accordig to the modification date.

Thank you much.

Edgar.
5 REPLIES
Ivan Ferreira
Honored Contributor

Re: file deletion script depending on dates

Check this

http://www.michna.com/software.htm#DelOld


You can download the source code. Is a tool for removing files based on the age.

While is unsupported, you can check the source code and modify to fit your requirements.

Another tool you can try is "obsolete"

http://users.aol.com/pasacaca/obsolete/obsolete.zip
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Jon Finley
Honored Contributor
Solution

Re: file deletion script depending on dates

There a CMD file I've used before, for the same sort of use. Change it as needed.
------------------------------
REM @echo off
for /f "tokens=2,3,4 delims=/ " %%i in ('echo %date%') do (
set /A my_day=%%j-7
set my_month=%%i
set my_year=%%k
)
if %my_day% LEQ 0 set /A my_month-=1
if %my_month%==0 (
set my_month=12
set /A my_year-=1
)

if %my_day% EQU 0 (
if %my_month%==12 set /A my_day=31
if %my_month%==11 set /A my_day=30
if %my_month%==10 set /A my_day=31
if %my_month%==9 set /A my_day=30
if %my_month%==8 set /A my_day=31
if %my_month%==7 set /A my_day=31
if %my_month%==6 set /A my_day=30
if %my_month%==5 set /A my_day=31
if %my_month%==4 set /A my_day=30
if %my_month%==3 set /A my_day=31
if %my_month%==2 set /A my_day=28
if %my_month%==1 set /A my_day=31
)

if %my_day% LSS 0 (
if %my_month%==12 set /A my_day=31+%my_day%
if %my_month%==11 set /A my_day=30+%my_day%
if %my_month%==10 set /A my_day=31+%my_day%
if %my_month%==9 set /A my_day=30+%my_day%
if %my_month%==8 set /A my_day=31+%my_day%
if %my_month%==7 set /A my_day=31+%my_day%
if %my_month%==6 set /A my_day=30+%my_day%
if %my_month%==5 set /A my_day=31+%my_day%
if %my_month%==4 set /A my_day=30+%my_day%
if %my_month%==3 set /A my_day=31+%my_day%
if %my_month%==2 set /A my_day=28+%my_day%
if %my_month%==1 set /A my_day=31+%my_day%
)

set sysdate=%my_year%/%my_month%/%my_day%
set filedate=%my_day%-%my_month%-%my_year%
set my_file = info_%filedate%


echo %sysdate%
echo %filedate%
pause
----------------------

Note that using Powershell would probably be your BEST choice, and it's free from MS.
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

Jon
"Do or do not. There is no try!" - Yoda
Jon Finley
Honored Contributor

Re: file deletion script depending on dates

Oops....

replace the one section with this: No '+'
--------------------------------------
if %my_day% LSS 0 (
if %my_month%==12 set /A my_day=31%my_day%
if %my_month%==11 set /A my_day=30%my_day%
if %my_month%==10 set /A my_day=31%my_day%
if %my_month%==9 set /A my_day=30%my_day%
if %my_month%==8 set /A my_day=31%my_day%
if %my_month%==7 set /A my_day=31%my_day%
if %my_month%==6 set /A my_day=30%my_day%
if %my_month%==5 set /A my_day=31%my_day%
if %my_month%==4 set /A my_day=30%my_day%
if %my_month%==3 set /A my_day=31%my_day%
if %my_month%==2 set /A my_day=28%my_day%
if %my_month%==1 set /A my_day=31%my_day%
)
----------------------------

I read through me script quickly, and thinking that I had made a mistake, added in the '+' signs. But the number being checked would be negative, so the subtraction will work fine.

Jon
"Do or do not. There is no try!" - Yoda
Edgar Zapata
Esteemed Contributor

Re: file deletion script depending on dates

Jon,
Thank you much.

I am testing it.
But this is for sure what I wanted.

I didn't know how to use the tokens in a FOR loop.

Tnks.
Edgar Zapata
Esteemed Contributor

Re: file deletion script depending on dates

I know there must be a way to do it a lot better.
I didn't know how to use the tokens or delims. This is very useful.

What I finally did was this:

@echo off
for /f "tokens=1,2 delims=/" %%i in ('date /t') do (
set _d=%%i
set _m=%%j
)

if %_d% EQU 5 goto 26a31:
if %_d% EQU 6 goto 26a31:
if %_d% EQU 7 goto 26a31:
if %_d% EQU 8 goto 26a31:
if %_d% EQU 9 goto 1a5:
if %_d% EQU 10 goto 1a5:
if %_d% EQU 11 goto 1a5:
if %_d% EQU 12 goto 1a5:
if %_d% EQU 13 goto 1a5:
if %_d% EQU 14 goto 6a10:
if %_d% EQU 15 goto 6a10:
if %_d% EQU 16 goto 6a10:
if %_d% EQU 17 goto 6a10:
if %_d% EQU 18 goto 6a10:
if %_d% EQU 19 goto 11a15:
if %_d% EQU 20 goto 11a15:
if %_d% EQU 21 goto 11a15:
if %_d% EQU 22 goto 11a15:
if %_d% EQU 23 goto 16a20:
if %_d% EQU 24 goto 16a20:
if %_d% EQU 25 goto 16a20:
if %_d% EQU 26 goto 16a20:
if %_d% EQU 27 goto 16a20:
if %_d% EQU 28 goto 21a25:
if %_d% EQU 29 goto 21a25:
if %_d% EQU 30 goto 21a25:
if %_d% EQU 31 goto 21a25:
exit


:26a31
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????26.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????27.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????28.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????29.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????30.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????31.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????26.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????27.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????28.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????29.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????30.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????31.log
exit

:1a5
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????01.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????02.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????03.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????04.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????05.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????01.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????02.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????03.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????04.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????05.log
exit

:6a10
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????06.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????07.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????08.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????09.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????10.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????06.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????07.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????08.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????09.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????10.log
exit

:11a15
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????11.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????12.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????13.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????14.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????15.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????11.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????12.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????13.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????14.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????15.log
exit

:16a20
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????16.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????17.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????18.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????19.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????20.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????16.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????17.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????18.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????19.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????20.log
exit

:21a25
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????21.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????22.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????23.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????24.log
del \\%computername%\e$\LogFiles\SMTPSVC1\ex????25.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????21.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????22.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????23.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????24.log
del \\%computername%\e$\LogFiles\SMTPSVC2\ex????25.log
exit


At least, I got it working.
I still would like to improve it.
If anyone could shed some light as to how to, I would appreciate it.

Tnks.
Edgar.