File Erase based on date

1502 views
Skip to first unread message

Mashup

unread,
Mar 31, 2000, 3:00:00 AM3/31/00
to
I wish to erase files created daily in a directory - a week after they have
been created - as a scheduled task. Does anyone know of any way of deleteing
files based upon creation date?

Cheers

Chris

Raymond Zeitler

unread,
Mar 31, 2000, 3:00:00 AM3/31/00
to
Because I'm lazy and very busy, I will propose only an outline of the steps.

1. Use XCOPY with these switches /L/D:mm.dd.yyyy and redirect
to Exclude.txt to create a list of files that have been
created within the last week.
2. Invoke XCOPY again with these switches /L/exclude:exclude.txt
and redirect to Delete.txt to create a list of all files
except those that are listed in Exclude.txt
3. Use a FOR command to delete each file listed in delete.txt.

The trick will be to specify or calculate mm.dd.yyyy. Also, XCOPY will
require the source and destination paths.

There are several batch gurus here who can fill in the blanks.

HTGYS

In article <954514510.26010.0...@news.demon.co.uk>,
cgra...@hotmail.com says...

Mashup

unread,
Apr 4, 2000, 3:00:00 AM4/4/00
to
Ray - thanks. Problem is I am stuck specifying the date - like you say. I
would like a way for the batch file to pick up the date from the OS
(Regional settings) - and we schedule once per week. So far I specify:

XCOPY C:\DIRA /L /D:%1 > EXCLUDE.TXT
XCOPY C:\DIRA /L /EXCLUDE:EXCLUDE.TXT

But the command cannot recognize the date as in mm.dd.yyyy etc....


Any other help / ideas appreciated.

Cheers

Chris
Raymond Zeitler wrote in message ...

Phil Robyn

unread,
Apr 4, 2000, 3:00:00 AM4/4/00
to
Mashup wrote:

> Ray - thanks. Problem is I am stuck specifying the date - like you say. I
> would like a way for the batch file to pick up the date from the OS
> (Regional settings) - and we schedule once per week. So far I specify:
>
> XCOPY C:\DIRA /L /D:%1 > EXCLUDE.TXT
> XCOPY C:\DIRA /L /EXCLUDE:EXCLUDE.TXT
>
> But the command cannot recognize the date as in mm.dd.yyyy etc....
>
> Any other help / ideas appreciated.
>
> Cheers
>
> Chris

Here's how to get the date in the format mm.dd.yyyy:

for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set zdate=%%a.%%b.%%c


Raymond Zeitler

unread,
Apr 11, 2000, 3:00:00 AM4/11/00
to
Unfortunately, the date that's required is one week before the current date.

In article <38EA1977...@uclink4.berkeley.edu>,
pro...@uclink4.berkeley.edu says...

Phil Robyn

unread,
Apr 11, 2000, 3:00:00 AM4/11/00
to
Raymond Zeitler wrote:

========begin file c:\cmd\UTIL\calcdate.cmd========
0001. @echo off
0002. if "%1"=="" goto :syntax
0003. if "%1"=="?" goto :syntax
0004. if "%1"=="/?" goto :syntax
0005. if "%1"=="help" goto :syntax
0006. if "%1"=="HELP" goto :syntax
0007. if "%2"=="" goto :syntax
0008. if "%OS%"=="Windows_NT" goto :begin
0009. echo.
0010. echo This procedure requires Windows NT.
0011. :syntax
0012. echo.
0013. echo %0 - calculate a new date based on an input date (Windows NT ONLY)
0014. echo.
0015. echo.
0016. echo parameters: 1 = TODAY or a date in the form YYYYMMDD
0017. echo.
0018. echo 2 = +xxxx or -xxxx, the number of days to add or subtract
0019. echo.
0020. echo 3 (optional) a separator character or short string
0021. echo.
0022. echo If there is a third parameter, the calculated date
0023. echo will be displayed as YYYYsepMMsepDD, e.g., 1999/01/15
0024. echo However, the returned value does not contain the separators.

0025. echo.
0026. echo syntax: %0 TODAY -45 returns the date 45 days ago
0027. echo.
0028. echo %0 19990201 +45 returns the date 45 days after 19990201
0029. echo.
0030. echo.
0031. echo The RESULT of %0 is returned in environment variable %0. To see the
0032. echo result, type the command 'set %0' or specify a third parameter (see above).
0033. echo To invoke this function repeatedly from the command prompt, first enter the command
0034. echo as
0035. echo.
0036. echo c:\cmd^> %0 TODAY +10
0037. echo.
0038. echo and then as
0039. echo.
0040. echo c:\cmd^> %0 %%%0%% +10
0041. echo.
0042. echo.
0043. echo.
0044. echo To invoke %0 and display the result, enter the command as
0045. echo.
0046. echo.
0047. echo c:\cmd^> %0 TODAY +10^&set %0
0048. echo.
0049. echo.
0050. echo or specify a third parameter, e.g.,
0051. echo.
0052. echo.
0053. echo c:\cmd^> %0 TODAY +10 /
0054. echo.
0055. goto :EOF
0056.
0057. :begin
0058. setlocal
0059. set scriptname=%~n0
0060. set indate=%1
0061. set inparm=%2
0062. set sign=%inparm:~0,1%
0063. set num_days=%inparm:~1%
0064.
0065. set MonthTable=1.31 2.28 3.31 4.30 5.31 6.30 7.31 8.31 9.30 10.31 11.30 12.31
0066. set LeapYears=1988 1992 1996 2000 2004 2008 2012 2016 2020
0067.
0068. if "%indate%" EQU "today" set indate=TODAY
0069. if "%indate%" EQU "TODAY" call :GetDate
0070.
0071. set zyy=%indate:~0,4%
0072. set zmm=%indate:~4,2%
0073. set zdd=%indate:~6,2%
0074.
0075. if %zmm:~0,1% EQU 0 set zmm=%zmm:~1%
0076. if %zdd:~0,1% EQU 0 set zdd=%zdd:~1%
0077.
0078. if [%sign%] EQU [+] goto :INCR
0079. if [%sign%] EQU [-] goto :DECR
0080.
0081. echo invalid sign %sign% & goto :syntax
0082.
0083. :GetDate
0084.
0085. for /f "tokens=1,2,3,4* delims=/ " %%i in ('date /t') do set ^
0086. indate=%%l%%j%%k
0087. goto :EOF
0088.
0089. :DaysInMonth
0090.
0091. set tstmm=%zmm%X
0092. if "%tstmm:~1,1%" EQU "X" set zmmoffset=2
0093. if "%tstmm:~1,1%" NEQ "X" set zmmoffset=3
0094.
0095.
0096. call newindex "%MonthTable%" "%zmm%."
0097. set /a ptr = newindex + zmmoffset
0098. call exec set days_in_month=%%MonthTable:~%ptr%,2%%%
0099. if "%zmm%" NEQ "2" goto :EOF
0100. call newindex "%LeapYears%" "%zyy%"
0101. if %newindex% GTR -1 set /a days_in_month+=1
0102. goto :EOF
0103.
0104. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
0105. :INCR
0106.
0107. call :DaysInMonth
0108.
0109. set /a tot_dd = zdd + num_days
0110. if %tot_dd% LEQ %days_in_month% set zdd=%tot_dd%& goto :EXIT
0111.
0112. set j=0
0113. :incrloop
0114. set /a j+=1
0115. if %j% GTR %num_days% goto :EXIT
0116. set /a zdd+=1
0117. if %zdd% LEQ %days_in_month% goto :incrloop
0118.
0119. set /a zmm+=1
0120. if 12 GEQ %zmm% goto :IX2
0121. set /a zyy+=1
0122. set zmm=1
0123. :IX2
0124. set /a zdd = 1
0125. call :DaysInMonth
0126. goto :incrloop
0127.
0128.
0129. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
0130. :DECR
0131.
0132. call :DaysInMonth
0133.
0134. set /a tot_dd = zdd - num_days
0135. if 1 LEQ %tot_dd% set zdd=%tot_dd%&goto :EXIT
0136.
0137. set j=0
0138.
0139. :decrloop
0140.
0141. set /a j+=1
0142. if %j% GTR %num_days% goto :EXIT
0143. set /a zdd-=1
0144. if %zdd% GEQ 1 goto :decrloop
0145. set /a zmm-=1
0146. if %zmm% GEQ 1 goto :DX2
0147. set /a zyy-=1
0148. set zmm=12
0149.
0150. :DX2
0151. call :DaysInMonth
0152. set zdd=%days_in_month%
0153. goto :decrloop
0154.
0155. :EXIT
0156.
0157. set ztst=%zmm%X
0158. if "%ztst:~1,1%" EQU "X" set zmm=0%zmm%
0159. set ztst=%zdd%X
0160. if "%ztst:~1,1%" EQU "X" set zdd=0%zdd%
0161. if "%3" NEQ "" echo.&echo %zyy%%3%zmm%%3%zdd%
0162. endlocal&set %scriptname%=%zyy%%zmm%%zdd%
0163. goto :EOF
0164. :EOF
0165.
========end file c:\cmd\UTIL\calcdate.cmd========

========begin file c:\cmd\UTIL\newindex.cmd========
001. @echo off
002. setlocal
003. set scriptname=%~n0
004. set s1=###%1###
005. set s2=###%2###
006. set s1=%s1: =`%
007. set s2=%s2: =`%
008. set s1=%s1:"###=%
009. set s1=%s1:###"=%
010. set s2=%s2:"###=%
011. set s2=%s2:###"=%
012. call length %s1%
013. set /a s1.size=%length%
014. call length %s2%
015. set /a s2.size=%length%
016. set /a pos=-1
017. :nxtchar
018. set /a pos+=1
019. if %pos% GTR %s1.size% goto :NOTFOUND
020. call exec set s3=%%s1:~%pos%,%s2.size%%%
021. if "%s3%" EQU "%s2%" goto :FOUND
022. goto :nxtchar
023. :FOUND
024. endlocal&set /a %scriptname%=%pos%&goto :EOF
025. :NOTFOUND
026. endlocal&set /a %scriptname%=-1&goto :EOF
027. :EOF
========end file c:\cmd\UTIL\newindex.cmd========

========begin file c:\cmd\UTIL\length.cmd========
001. @echo off
002. setlocal
003. set scriptname=%~n0
004. set zls01=%*
005. set zlen=0
006. :nextchar
007. if not defined zls01 goto :EXIT
008. set /A zlen+=1
009. set zls01=%zls01:~1%
010. goto nextchar
011. :EXIT
012. set /A zlen-=1
013. endlocal & set %scriptname%=%zlen%& goto :EOF
014. :EOF
========end file c:\cmd\UTIL\length.cmd========

========begin file c:\cmd\UTIL\exec.cmd========
001. @echo off
002. ::
003. ::
004. :: this subroutine executes the parameter passed to it
005. ::
006. ::
007. %*
008. goto :EOF
009. :EOF
010.
========end file c:\cmd\UTIL\exec.cmd========

Rick Campbell

unread,
Apr 11, 2000, 3:00:00 AM4/11/00
to
I have to small utilities that allow you to delete files based on thier
date. One is called DELAGE, and the other is called PURGE.

These are both 32 bit console programs and they work very well for me. I
use them in batch scripts to scan a public access share on my network on a
daily basis and delete ANY files older than 2 days.

Rick.


"Raymond Zeitler" <ra...@phonon.com> wrote in message
news:MPG.135d56c36...@news.javanet.com...

Olav Kesper

unread,
Apr 18, 2000, 3:00:00 AM4/18/00
to
Where I can get this programs?

I have to solve the problem that I must delete files that are older then one
month or 30 day's.

Thanks in advice.

Olav


Rick Campbell <rick...@speakeasy.org> wrote in message
news:sf85aos...@corp.supernews.com...

HART

unread,
Apr 23, 2000, 3:00:00 AM4/23/00
to
DATE:
I use a variable in the enviorment.
add varible. date1
and just each day, reset the date manully.
then the rest is done automatically.

you can use it like this: IF EXIST 3.HTM COPY %date1%hk.htm+3.htm
that is just a copy of a line in abatch file to add htm pages into a
single htm file dated today.
for you:
%date1%hk.htm this is the part.

the rest of the file, others have mentioned.
this is just date help.
luck, Haart


On Tue, 4 Apr 2000 14:51:49 +0100, "Mashup" <cgra...@hotmail.com>
wrote:

>Ray - thanks. Problem is I am stuck specifying the date - like you say. I
>would like a way for the batch file to pick up the date from the OS
>(Regional settings) - and we schedule once per week. So far I specify:
>
>XCOPY C:\DIRA /L /D:%1 > EXCLUDE.TXT
>XCOPY C:\DIRA /L /EXCLUDE:EXCLUDE.TXT
>
>But the command cannot recognize the date as in mm.dd.yyyy etc....
>
>
>Any other help / ideas appreciated.
>
>Cheers
>
>Chris

Reply all
Reply to author
Forward
0 new messages