jwstairs
asked on
CMD File to delete .txt files that are at least 7 days old.
Hi,
I need some help with a .cmd file that. I haven't been able to find much useful info on this subject. My .cmd file knowledge is limited to set,start, if,... What I need is a .cmd file that will delete all .txt files in a given directory that are at least seven days old. Can anyone help me with this?
The idea is something like the following...
for (each file in directory x)
{
if (file type is .txt)
{
if (file older than 7 days)
delete the file;
}
}
Thanks,
Jeff
I need some help with a .cmd file that. I haven't been able to find much useful info on this subject. My .cmd file knowledge is limited to set,start, if,... What I need is a .cmd file that will delete all .txt files in a given directory that are at least seven days old. Can anyone help me with this?
The idea is something like the following...
for (each file in directory x)
{
if (file type is .txt)
{
if (file older than 7 days)
delete the file;
}
}
Thanks,
Jeff
ASKER
Thanks for the info.
I am running Windows NT 4.0. service pack 6. To make the script reusable I would like to be able to pass the number of days and file type as parameters. I would rather use native OS commands from a cmd file if that is possible.
Regards,
Jeff
I am running Windows NT 4.0. service pack 6. To make the script reusable I would like to be able to pass the number of days and file type as parameters. I would rather use native OS commands from a cmd file if that is possible.
Regards,
Jeff
Solution will follow after the coffee...
;-)
I need to solve the number of days for the month the file were created.
;-)
I need to solve the number of days for the month the file were created.
Check out the delold utility at http://www.michna.com/software.htm#DelOld
It looks like it will do what you want.
The CMD file command would look like the following.
DELOLD C:\TEMP\*.TMP 7
It looks like it will do what you want.
The CMD file command would look like the following.
DELOLD C:\TEMP\*.TMP 7
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I thought this would probably be the answer. I gave it a try a couple of times myself and ran into many blocks....
Thanks for the info everyone.
Thanks for the info everyone.
Sorry
I had no time to finish the job yesterday.
Here is the solution exactly as you asked : batch file.
There is only one day difference of 1 day for bissextil years.
This work in the same directory as the .txt files for now.
But it works great.
--- zDelTxt.bat ---
REM By Longbow on april 24 2002
@echo off
echo Deleted files >deleted.log
echo.>del.log
rem Check filedate (variables = _fdd _fmm _fyyyy)
for %%b in (*.txt) do call zCheck.bat %%b
del del.log
notepad deleted.log
-------------------
--- zCheck.bat ----
REM By Longbow on april 24 2002
@echo off
dir %1 | find /I "/">c:\del.log
rem Check current date (variables = _dd _mm _yyyy)
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cyyyy=%%c
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cmm=%%b
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cdd=%%a
set _cyyyy=%yyyy:~2,2%
set _yyyy=
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fyyyy=%%c
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fmm=%%b
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fdd=%%a
rem Number of days elapsed
If %_cmm%==01 set _cnd=0
If %_cmm%==02 set _cnd=31
If %_cmm%==03 set _cnd=59
If %_cmm%==04 set _cnd=90
If %_cmm%==05 set _cnd=120
If %_cmm%==06 set _cnd=151
If %_cmm%==07 set _cnd=181
If %_cmm%==08 set _cnd=212
If %_cmm%==09 set _cnd=243
If %_cmm%==10 set _cnd=273
If %_cmm%==11 set _cnd=304
If %_cmm%==12 set _cnd=334
If %_fmm%==01 set _fnd=0
If %_fmm%==02 set _fnd=31
If %_fmm%==03 set _fnd=59
If %_fmm%==04 set _fnd=90
If %_fmm%==05 set _fnd=120
If %_fmm%==06 set _fnd=151
If %_fmm%==07 set _fnd=181
If %_fmm%==08 set _fnd=212
If %_fmm%==09 set _fnd=243
If %_fmm%==10 set _fnd=273
If %_fmm%==11 set _fnd=304
If %_fmm%==12 set _fnd=334
set /A _timeF = %_fyyyy% - 1 * 365 + %_fnd% + %_fdd%
set /A _timeC = %_cyyyy% - 1 * 365 + %_cnd% + %_cdd%
set /A _timeE=_timeC-_timeF
rem If elapsed time is greater than 7 days log and delete the file
echo.>>deleted.log
if %_timeE% GTR 7 goto DELETE
goto END
:DELETE
rem echo file to be deleted : %1
type del.log>>deleted.log
del %1
:END
set _cdd=
set _cmm=
set _cyyyy=
set _fdd=
set _fmm=
set _fyyyy=
set _x=
set _y=
set _cnd=
set _fnd=
set _timeC=
set _timeF=
set _timeE=
-------------------
I had no time to finish the job yesterday.
Here is the solution exactly as you asked : batch file.
There is only one day difference of 1 day for bissextil years.
This work in the same directory as the .txt files for now.
But it works great.
--- zDelTxt.bat ---
REM By Longbow on april 24 2002
@echo off
echo Deleted files >deleted.log
echo.>del.log
rem Check filedate (variables = _fdd _fmm _fyyyy)
for %%b in (*.txt) do call zCheck.bat %%b
del del.log
notepad deleted.log
-------------------
--- zCheck.bat ----
REM By Longbow on april 24 2002
@echo off
dir %1 | find /I "/">c:\del.log
rem Check current date (variables = _dd _mm _yyyy)
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cyyyy=%%c
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cmm=%%b
FOR /f "tokens=2-4 delims=/ " %%a in ('date /t') DO SET _cdd=%%a
set _cyyyy=%yyyy:~2,2%
set _yyyy=
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fyyyy=%%c
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fmm=%%b
FOR /f "tokens=1-4 delims=/ " %%a in ('type del.log') DO SET _fdd=%%a
rem Number of days elapsed
If %_cmm%==01 set _cnd=0
If %_cmm%==02 set _cnd=31
If %_cmm%==03 set _cnd=59
If %_cmm%==04 set _cnd=90
If %_cmm%==05 set _cnd=120
If %_cmm%==06 set _cnd=151
If %_cmm%==07 set _cnd=181
If %_cmm%==08 set _cnd=212
If %_cmm%==09 set _cnd=243
If %_cmm%==10 set _cnd=273
If %_cmm%==11 set _cnd=304
If %_cmm%==12 set _cnd=334
If %_fmm%==01 set _fnd=0
If %_fmm%==02 set _fnd=31
If %_fmm%==03 set _fnd=59
If %_fmm%==04 set _fnd=90
If %_fmm%==05 set _fnd=120
If %_fmm%==06 set _fnd=151
If %_fmm%==07 set _fnd=181
If %_fmm%==08 set _fnd=212
If %_fmm%==09 set _fnd=243
If %_fmm%==10 set _fnd=273
If %_fmm%==11 set _fnd=304
If %_fmm%==12 set _fnd=334
set /A _timeF = %_fyyyy% - 1 * 365 + %_fnd% + %_fdd%
set /A _timeC = %_cyyyy% - 1 * 365 + %_cnd% + %_cdd%
set /A _timeE=_timeC-_timeF
rem If elapsed time is greater than 7 days log and delete the file
echo.>>deleted.log
if %_timeE% GTR 7 goto DELETE
goto END
:DELETE
rem echo file to be deleted : %1
type del.log>>deleted.log
del %1
:END
set _cdd=
set _cmm=
set _cyyyy=
set _fdd=
set _fmm=
set _fyyyy=
set _x=
set _y=
set _cnd=
set _fnd=
set _timeC=
set _timeF=
set _timeE=
-------------------
I want to created automated scheduled script that would delete data older than a specific number of days from the current date from specific directories
Jeff,
What is your OS and version? Is the "seven days" in your example
to be hard coded or will you want to pass a variable number of days
(and/or file attributes) to the script as a replaceable parameter?
Also, would a program made for that purpose work for you?
There are many available that are designed to delete by date.
Depending on the OS there aren't commands to delete by date
but a workaround script calling other commands can be made for it.
Otherwise I can recommend and provide a small utility that is made to
delete files in a specified location older than a given time or date.
It can be called from a cmd or batch file having a set period
or can be passed variable dates and/or file specifications.
Please post back with further details if interested and
I'll supply more info on it or indicate whether
you'd rather try to use commands
your OS already contains.