Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Batch-file delete based on how old

5,202 views
Skip to first unread message

Jan Pedersen

unread,
Oct 9, 2001, 10:33:05 AM10/9/01
to
Hello

im trying to find out, how to make a batch file who can delete files, which
are 2 or more days old.
how is that possible?

any help would be accepted

-Jan


Phil Robyn

unread,
Oct 9, 2001, 12:23:08 PM10/9/01
to
Jan Pedersen wrote:

========begin file C:\CMD\UTIL\DELOLD.CMD==================================
0001. @echo off
0002. ::
0003. :: %~n0 - delete files older than (nnn) days - Y2K compliant
0004. ::
0005. :: parameters: %%1 file spec
0006. :: %%2 number of days
0007. :: %%3 action
0008. :: 'list' - list files eligible for deletion only, no delete
0009. :: 'delete' - delete eligible files
0010. ::
0011. :: example: %~n0 c:\cmd\z*.cmd 25 list
0012. ::
0013. :: enter long file names or directory names containing spaces as
0014. ::
0015. :: %~n0 "c:\Program Files\Some directory\*.*" 25 delete
0016. ::
0017. ::
0018. if [%1] NEQ [] if [%1] NEQ [?] if [%1] NEQ [/?] if [%2] NEQ [] goto :begin
0019. call XHELP %~f0
0020. goto :EOF
0021.
0022. :begin
0023. if not exist %1 (
0024. echo.
0025. echo %1 not found.
0026. call XHELP %~f0
0027. goto :EOF
0028. )
0029.
0030. setlocal
0031. set cd=
0032. set days_old=%2
0033. set files_deleted=0
0034. set dir_entries=0
0035. set totalsize=0
0036. set target_dir=%~dp1
0037. call CALCDATE TODAY -%days_old%
0038. set mode=%3
0039. if not defined mode set mode=LIST
0040. set targetdate=%calcdate%
0041. if /i "%mode%" EQU "LIST" call :header
0042. for /f "tokens=* delims=" %%a in (
0043. 'dir /a-d %1 /-C ^| find "/"'
0044. ) do call :getitem "%%a"
0045. echo.
0046. echo eligible for deletion: %files_deleted% files occupying %totalsize% bytes
0047. endlocal&goto :EOF
0048.
0049.
0050. :header
0051.
0052.
0053. echo.
0054. echo The following files are more than %days_old% days old and are thus eligible
0055. echo for deletion:
0056. echo.
0057. echo FILEDATE FILE SIZE FILE NAME
0058. echo ________ ___________ ________________________________________
0059. goto :EOF
0060.
0061. :getitem
0062.
0063. set /a dir_entries+=1
0064. set inrec=%*
0065. if not defined cd set inrec=%inrec:~1%
0066. set inrec=%inrec:&=^^^&%
0067. set inrec=%inrec:"=%
0068. if "%inrec:~39,6%" EQU " bytes" goto :EOF
0069. set filedate=%inrec:~0,8%
0070. set filetime=%inrec:~10,6%
0071. set filesize=%inrec:~29,10%
0072. set filename=%inrec:~39%
0073. if not defined filename goto :EOF
0074. set filename=%filename:&=^^^&%
0075. set filename=%filename: =%
0076. set fileyy=%filedate:~6,2%
0077.
0078. if %fileyy% GEQ 80 set yyyy=19%fileyy%
0079. if %fileyy% LSS 80 set yyyy=20%fileyy%
0080.
0081. set compdate=%yyyy%%filedate:~0,2%%filedate:~3,2%
0082.
0083. if %compdate% GTR %targetdate% goto :EOF
0084.
0085. set /a files_deleted+=1
0086. set /a totalsize = totalsize + filesize
0087. if /i "%mode%" EQU "LIST" goto :list
0088. if /i "%mode%" EQU "DELETE" goto :delete
0089.
0090. :delete
0091. set filename="""%target_dir%%filename%"""
0092. set filename=%filename:&=^&%
0093. set filename=%filename:"""=%
0094. del "%filename%"
0095. goto :EOF
0096.
0097. :list
0098. set dispsize=%filesize%$$$$$$$$$$$$
0099. :lloop
0100. set dispsize= %dispsize%
0101. if "%dispsize:~13,3%" EQU "$$$" goto :padded
0102. goto :lloop
0103. :padded
0104. set dispsize=%dispsize:$=%
0105. set filename=%filename:&=^^^&%
0106. echo %filedate% %dispsize% %target_dir%%filename%
0107. goto :EOF
========end file C:\CMD\UTIL\DELOLD.CMD==================================

========begin file C:\cmd\TEST\XHELP.CMD==================================
001. @echo off
002. ::
003. :: Displays the contents of the 'flowerbox' in a CMD or BAT
004. :: file.
005. ::
006. :: '%~n0' will be displayed as the name of the referenced file
007. ::
008.
009. if {%1} NEQ {} (if {%1} NEQ {?} (if {%1} NEQ {/?}^
010. (if /i {%1} NEQ {HELP} ( goto :begin ))))
011. call XHELP %~f0
012. goto :EOF
013.
014. :begin
015. setlocal
016. set whichfile=%1
017. set fname=%~n1
018. echo :
019. echo : %whichfile%
020. echo :
021. for /f "tokens=* delims=" %%* in (
022. 'findstr /b /c:"::" %whichfile%') do call :display %%*
023. endlocal& goto :EOF
024. :display
025. set line=%*
026. set line=%line:~2%
027. call set line=%%line:^%~n0=%fname%%%
028. echo %line%
029. goto :EOF
030. :EOF
031.
========end file C:\cmd\TEST\XHELP.CMD==================================

========begin file C:\cmd\UTIL\calcdate.cmd==================================
0001. @echo off
0002. ::
0003. :: calculate a new date based on the input date
0004. ::
0005. ::
0006. :: parameters: %%1 = TODAY or date in the form of YYYYMMDD
0007. ::
0008. :: %%2 = +xxxx or -xxxx, the number of days to add or subtract
0009. ::
0010. :: %%3 (optional, any char) if defined, display result on screen
0011. ::
0012. ::
0013. :: syntax: %~n0 TODAY -45 returns the date 45 days ago
0014. ::
0015. :: %~n0 19990201 +45 returns the date 45 days after 19990201
0016. ::
0017. ::
0018. :: The RESULT is returned in environment variable %~n0. To see the
0019. :: result, type the command 'set %~n0' or specify any character as parameter %%3.
0020. ::
0021. if {%1} NEQ {} (if {%1} NEQ {?} (if {%1} NEQ {/?}^
0022. (if /i {%1} NEQ {HELP} (if {%2} NEQ {} ( goto :begin )))))
0023. call XHELP %~f0
0024. goto :EOF
0025. :begin
0026. setlocal
0027. set indate=%1
0028. set inparm=%2
0029. set sign=%inparm:~0,1%
0030. set num_days=%inparm:~1%
0031.
0032. if /i "%indate%" EQU "TODAY" call :GetDate
0033.
0034. set zyy=%indate:~0,4%
0035. set zmm=%indate:~4,2%
0036. set zdd=%indate:~6,2%
0037.
0038. if %zmm:~0,1% EQU 0 set zmm=%zmm:~1%
0039. if %zdd:~0,1% EQU 0 set zdd=%zdd:~1%
0040.
0041.
0042. if [%sign%] EQU [+] goto :FORWARD
0043. if [%sign%] EQU [-] goto :BACKWARD
0044.
0045. echo invalid sign %sign% & goto :syntax
0046.
0047. :GetDate
0048.
0049. for /f "tokens=2-4 delims=/ " %%j in ('date /t') do set indate=%%l%%j%%k
0050. goto :EOF
0051.
0052. :DaysInMonth
0053.
0054. set nmm=%zmm%X
0055. if %nmm:~1,1%==X (
0056. set nmm=0%zmm%
0057. ) else (
0058. set nmm=%zmm%
0059. )
0060. set /a tstmm = 1%nmm% - 100
0061. for /f "tokens=%tstmm%" %%a in (
0062. 'echo 31 28 31 30 31 30 31 31 30 31 30 31'
0063. ) do set days_in_month=%%a
0064.
0065. if "%nmm%" NEQ "02" goto :EOF
0066.
0067. for %%a in (
0068. 1980 1984 1988 1992 1996 2000 2004 2008 2012 2016 2020 2024
0069. 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072
0070. 2076
0071. ) do if %%a==%zyy% set /a days_in_month+=1
0072. goto :EOF
0073.
0074. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
0075. :FORWARD
0076.
0077. call :DaysInMonth
0078.
0079. set /a tot_dd = zdd + num_days
0080. if %tot_dd% LEQ %days_in_month% set zdd=%tot_dd%& goto :EXIT
0081.
0082. set j=0
0083. :incrloop
0084. set /a j+=1
0085. if %j% GTR %num_days% goto :EXIT
0086. set /a zdd+=1
0087. if %zdd% LEQ %days_in_month% goto :incrloop
0088.
0089. set /a zmm+=1
0090. if 12 GEQ %zmm% goto :IX2
0091. set /a zyy+=1
0092. set zmm=1
0093. :IX2
0094. set /a zdd = 1
0095. call :DaysInMonth
0096. goto :incrloop
0097.
0098.
0099. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
0100. :BACKWARD
0101.
0102. call :DaysInMonth
0103.
0104. set /a tot_dd = zdd - num_days
0105. if 1 LEQ %tot_dd% set zdd=%tot_dd%&goto :EXIT
0106.
0107. set j=0
0108.
0109. :decrloop
0110.
0111. set /a j+=1
0112. if %j% GTR %num_days% goto :EXIT
0113. set /a zdd-=1
0114. if %zdd% GEQ 1 goto :decrloop
0115. set /a zmm-=1
0116. if %zmm% GEQ 1 goto :DX2
0117. set /a zyy-=1
0118. set zmm=12
0119.
0120. :DX2
0121. call :DaysInMonth
0122. set zdd=%days_in_month%
0123. goto :decrloop
0124.
0125. :EXIT
0126.
0127. if 10 GTR %zmm% set zmm=0%zmm%
0128. if 10 GTR %zdd% set zdd=0%zdd%
0129. if "%3" NEQ "" echo.&echo %zyy%%3%zmm%%3%zdd%
0130. endlocal&set %~n0=%zyy%%zmm%%zdd%
0131. goto :EOF
0132. :EOF
========end file C:\cmd\UTIL\calcdate.cmd==================================

Phil Robyn
Univ. of California, Berkeley

--

u n z i p m y a d d r e s s t o s e n d e - m a i l


Dr John Stockton

unread,
Oct 9, 2001, 2:22:00 PM10/9/01
to
JRS: In article <3bc2fc92$1...@news.carlbro.dk>, seen in
news:alt.msdos.batch.nt, Jan Pedersen <firest...@hotmail.com> wrote
at Tue, 9 Oct 2001 15:33:05 :-

>im trying to find out, how to make a batch file who can delete files, which
>are 2 or more days old.

The following works in DOS; if it has been tested in NT, I've not seen
it reported.

HUNT * b#-2 "del" u

will delete files from before the start of the day before yesterday.
Test without the 'u', or with 'echo' instead of 'del'. Replace '*' to
be more selective.

To be more exact about 2 days = 172800 seconds, same caveat,

NOWMINUS d2 exx
HUNT * b%xx% "del" u

HUNT, NOWMINUS via sig line 3. If in NT Exx fails, use Pxx.

--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)

Jan Pedersen

unread,
Oct 10, 2001, 5:30:45 AM10/10/01
to
i have a problem, my pc is running european format like this
c:\date /T
on 09-10-2001

on= day of week - in danish.

can you give a hint on what to switch in your batch?

"Phil Robyn" <pro...@uclink.berkzipeley.edu> wrote in message
news:3BC3246B...@uclink.berkzipeley.edu...

Frank

unread,
Oct 10, 2001, 7:19:42 AM10/10/01
to
Jan Pedersen <3bc4...@news.carlbro.dk>...

^ i have a problem, my pc is running european format like this
^ c:\date /T
^ on 09-10-2001

We might be able to figure out a general solution to this international
date problem. Please post the output from the following script.

::BEGIN SCRIPT::::::::::::::::::::::::::
@Echo OFF
Echo.------------------
Echo COMMAND LINE:
Echo.|DATE
Echo.------------------
Echo REGISTRY:
regedit /e %temp%\Date.reg "HKEY_USERS\.DEFAULT\Control
Panel\International\"
Type %temp%\Date.reg
Erase %temp%\Date.reg
Echo.------------------
Goto :EOF
::END SCRIPT::::::::::::::::::::::::::::

I suspect that the DATE command does not get its settings from the
registry.

Frank

Frank

unread,
Oct 10, 2001, 7:26:56 AM10/10/01
to
Frank <01c1517d$731f0860$0125250a@qxdxysrefdospqnx>...

^ ::BEGIN SCRIPT::::::::::::::::::::::::::
^ @Echo OFF
^ Echo.------------------
^ Echo COMMAND LINE:
^ Echo.|DATE
^ Echo.------------------
^ Echo REGISTRY:
^ regedit /e %temp%\Date.reg "HKEY_USERS\.DEFAULT\Control
^ Panel\International\"
^ Type %temp%\Date.reg
^ Erase %temp%\Date.reg
^ Echo.------------------
^ Goto :EOF
^ ::END SCRIPT::::::::::::::::::::::::::::


These two lines:

^ regedit /e %temp%\Date.reg "HKEY_USERS\.DEFAULT\Control
^ Panel\International\"

are actually one line with a space between them. Try this instead:

::BEGIN SCRIPT::::::::::::::::::::::::::
@Echo OFF
Echo.------------------
Echo COMMAND LINE:
Echo.|DATE
Echo.------------------
Echo REGISTRY:

regedit^

Frank

unread,
Oct 10, 2001, 8:37:04 AM10/10/01
to
Frank <01c1517e$77b34110$0125250a@qxdxysrefdospqnx>...

Also the output from this please:
SetLocal
For /F "tokens=1 delims=:" %%a in ('Echo.^|DATE') Do Set DateFmt=%%a
Type %ComSpec%|Find "%DateFmt%"
Goto :EOF

On my system that produces:
Enter the new date: (mm-dd-yy) %0
Enter the new date: (yy-mm-dd) %0
Enter the new date: (dd-mm-yy) %0

Frank

Jan Pedersen

unread,
Oct 10, 2001, 10:00:01 AM10/10/01
to
1st result

------------------
COMMAND LINE:
The current date is: on 10-10-2001


Enter the new date: (dd-mm-yy)

------------------
REGISTRY:
REGEDIT4

[HKEY_USERS\.DEFAULT\Control Panel\International\]
"Locale"="00000409"
"sLanguage"="ENU"
"sCountry"="United States"
"iCountry"="1"
"sList"=","
"iMeasure"="1"
"sDecimal"="."
"sThousand"=","
"iDigits"="2"
"iLZero"="1"
"sCurrency"="$"
"iCurrDigits"="2"
"iCurrency"="0"
"iNegCurr"="0"
"sDate"="/"
"sTime"=":"
"sShortDate"="M/d/yy"
"sLongDate"="dddd, MMMM dd, yyyy"
"iDate"="0"
"iTime"="0"
"iTLZero"="0"
"s1159"="AM"
"s2359"="PM"

[HKEY_USERS\.DEFAULT\Control Panel\International\\Sorting Order]

------------------


result 2:


E:\cmd>SetLocal

E:\cmd>FOR /F "tokens=1 delims=:" %a in ('Echo.|DATE') Do Set DateFmt=%a

E:\cmd>Set DateFmt=The current date is

E:\cmd>Set DateFmt=Enter the new date

E:\cmd>Type C:\WINNT\system32\cmd.exe | Find "Enter the new date"


Enter the new date: (mm-dd-yy) %0
Enter the new date: (yy-mm-dd) %0
Enter the new date: (dd-mm-yy) %0

E:\cmd>Goto :EOF

-jan


Simon Sheppard

unread,
Oct 10, 2001, 1:40:41 PM10/10/01
to
On Wed, 10 Oct 2001 11:19:42 GMT, "Frank"
<skyzl...@fnahutzqplcocqmu.com> wrote:

>Jan Pedersen <3bc4...@news.carlbro.dk>...
>
>^ i have a problem, my pc is running european format like this
>^ c:\date /T
>^ on 09-10-2001
>
>We might be able to figure out a general solution to this international
>date problem.
>

You'll find a couple of approaches to this here

http://www.ss64.demon.co.uk/ntsyntax/ss64_NT.zip

:: This batch file - based on a previous post by Michael Jerkovic
will return the date (including 4 digit year) into environment vars

:: Works on any NT machine independent of regional date settings

@ECHO off
SETLOCAL
IF [%1]==[] goto s_start

ECHO GETDATE.cmd
ECHO Returns the date independent of regional settings
ECHO Creates the environment variables %v_year% %v_month% %v_day%
ECHO.
ECHO SYNTAX
ECHO GETDATE
ECHO.
ECHO.
GOTO :eof

:s_start

FOR /f "tokens=2-4 skip=1 delims=(-)" %%G IN ('echo.^|date') DO (
FOR /f "tokens=2 delims= " %%A IN ('date /t') DO (
SET v_first=%%G
SET v_second=%%H
SET v_third=%%I
SET v_all=%%A
)
)

SET %v_first%=%v_all:~0,2%
SET %v_second%=%v_all:~3,2%
SET %v_third%=%v_all:~6,4%

ECHO Today is Year: [%yy%] Month: [%mm%] Day: [%dd%]

ENDLOCAL & SET v_year=%yy%& SET v_month=%mm%& SET v_day=%dd%
-
Simon Sheppard
Web: http://www.ss64.com
email: Simon@ "

Frank

unread,
Oct 10, 2001, 2:14:46 PM10/10/01
to
Jan Pedersen <3bc4464d$1...@news.carlbro.dk>...

^ COMMAND LINE:
^ The current date is: on 10-10-2001
^ Enter the new date: (dd-mm-yy)

I picked a real bad day to do this. Well, I suppose 10-10-10 would have
been worse. I'll assume until tomorrow that the output (10-10-2001) matches
the input format (dd-mm-yy). Not a good assumption since yy does not match
2001.

I'm surprised that English is used in the prompt and Danish is used in the
date. Is it that way with all built in command line utilities? Now that I
think about it, I think it's only time, currency and number information
that has local language customization.

^ REGISTRY:
^ [HKEY_USERS\.DEFAULT\Control Panel\International\]
^ "sShortDate"="M/d/yy"

That doesn't match the input format for DATE either. Month and day are
reversed and "M" and "d" are the month and day without leading zeros. Also,
yy matches the input format of DATE but not its output format.


^ E:\cmd>Type C:\WINNT\system32\cmd.exe | Find "Enter the new date"
^ Enter the new date: (mm-dd-yy) %0
^ Enter the new date: (yy-mm-dd) %0
^ Enter the new date: (dd-mm-yy) %0

I'll have to assume that those formats are the best that we can get. Here's
what I've come up with so far:


::START OF SCRIPT:::::::::::::::::::::::::::::::::::::

::@Goto :RunDemo Uncomment to run a demonstration.

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:GetDateFormat VariableName
:: Sets the specified variable to the system's date format.
:: Written by Frank P. Westlake, 2001.10.10
::
:: Usage: Call :GetDateFormat VariableName
::
SetLocal
For /F "tokens=5" %%a in ('Echo.^|DATE') Do Set DateFmt=%%a
For /F "tokens=6" %%a in ('Echo.^|DATE^|Find "current"') Do Set DateStr=%%a
Set DateFmt=%DateFmt:~1%
Set DateFmt=%DateFmt:)=%
Call Set DateFmt=%%DateFmt:-=^%DateStr:~2,1%%%&::Thanks Phil Robyn!
If NOT "%DateStr:~8,1%"=="" Set DateFmt=%DateFmt:yy=ccyy%
EndLocal&Set %1=%DateFmt%
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: Delete the remainder of this script if demo is not required.
:RunDemo
@Echo OFF
SetLocal ENABLEEXTENSIONS

Echo.We would have obtained a date by some means. Let's use
Echo.31 July 2010:
Echo.
Echo Set M=07
Echo Set D=31
Echo Set C=20
Echo Set Y=10

Set M=07
Set D=31
Set C=20
Set Y=10
Echo.
Echo Set the date format into the variable %%Today%%:
Echo.
Echo Call :GetDateFormat Today
Call :GetDateFormat Today
Echo.
Echo.The format is:
Echo.
Echo %Today%
Echo.
Echo.Now replace the format tokens with the contents of:
Echo.%%M%%, %%D%%, %%C%%, %%Y%%
Echo.
Echo. Call Set Today=%%%%Today:mm=^^%%M%%%%%%
Echo. Call Set Today=%%%%Today:dd=^^%%D%%%%%%
Echo. Call Set Today=%%%%Today:cc=^^%%C%%%%%%
Echo. Call Set Today=%%%%Today:yy=^^%%Y%%%%%%

Call Set Today=%%Today:mm=^%M%%%
Call Set Today=%%Today:dd=^%D%%%
Call Set Today=%%Today:cc=^%C%%%
Call Set Today=%%Today:yy=^%Y%%%

Echo.
Echo.Now display the result:
Echo.
Echo Echo.%%Today%%
Echo.
Echo.%Today%
Goto :EOF
::END OF SCRIPT:::::::::::::::::::::::::::::::::::::::

Jan Pedersen

unread,
Oct 11, 2001, 5:58:24 AM10/11/01
to
This getdate.cmd works well.

any easy way to delete files "TODAY-2" ?

got some examples but they dont work here cause of my regional settings, and
it is rather hard for me to adopt as i am a nonadvanced batch programmer.

Think it could be done in only one cmd file.

btw. thanks for the answers so far, i sure appreciate it!

-jan

Frank

unread,
Oct 11, 2001, 6:57:48 AM10/11/01
to
Jan Pedersen <3bc5...@news.carlbro.dk>...

^ any easy way to delete files "TODAY-2" ?

If you have the resource kit use FORFILES. If you don't have the resource
kit then I suggest you modify Phil's script to meet your needs.

Frank

Jan Pedersen

unread,
Oct 11, 2001, 9:14:59 AM10/11/01
to
hehe Dammit man you roxx

this util can do what i want! why the hell didnt i know about it:)

whoaahhhh thanks man and to ya all for helping me.

-jan

"Frank" <okwhy...@rrncwwjrfbgrzpqb.com> wrote in message
news:01c15243$8e6ae0a0$0125250a@rrncwwjrfbgrzpqb...

Al Dunbar

unread,
Oct 24, 2001, 1:12:34 AM10/24/01
to

"Frank" <qdjln...@mkwzalzpdzhzlfuk.com> wrote in message
news:01c151b7$6f0220c0$0125250a@mkwzalzpdzhzlfuk...

> Jan Pedersen <3bc4464d$1...@news.carlbro.dk>...
>
> ^ COMMAND LINE:
> ^ The current date is: on 10-10-2001
> ^ Enter the new date: (dd-mm-yy)
>
> I picked a real bad day to do this. Well, I suppose 10-10-10 would have
> been worse. I'll assume until tomorrow that the output (10-10-2001)
matches
> the input format (dd-mm-yy). Not a good assumption since yy does not match
> 2001.

Arg! Who would have thought that this long after the dreaded y2k we would
still be facing date ambiguities even though there is now a non ambiguous
standard: yyyy-mm-dd. Hey, it even sorts easily in chronological order!

/Al

Frank

unread,
Oct 24, 2001, 7:40:49 AM10/24/01
to
Al Dunbar <ttcjjqk...@corp.supernews.com>...

^ "Frank" news:01c151b7$6f0220c0$0125250a@mkwzalzpdzhzlfuk...
^ Arg! Who would have thought that this long after the dreaded y2k we would
^ still be facing date ambiguities even though there is now a non ambiguous
^ standard: yyyy-mm-dd. Hey, it even sorts easily in chronological order!

Find something useful to contribute Al.

Frank

0 new messages