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

delete files older than 7 days

158 views
Skip to first unread message

laurie

unread,
Apr 11, 2004, 1:18:00 PM4/11/04
to
Hi.

I am looking for a simple bat script to delete files in a directory older than, say, 7 days. Its for automatic mail log file
deleting.
Cant seem to find anything suitable on the net.
Can anyone help?

Thansk

Laurie

laurie

unread,
Apr 11, 2004, 1:33:42 PM4/11/04
to
Oh, and it's Windows 2000 Server.

Laurie


"laurie" <law...@hotmail.com> wrote in message news:d3fec.678$P%3.342@newsfe1-win...

Timo Salmi

unread,
Apr 11, 2004, 2:38:13 PM4/11/04
to
In article <Xhfec.684$P%3.286@newsfe1-win>, laurie <law...@hotmail.com> wrote:
> I am looking for a simple bat script to delete files in a directory older than, say, 7 days. Its for automatic mail log file
> deleting.
> Oh, and it's Windows 2000 Server.

In that case news:alt.msdos.batch.nt is your thing.

There e.g. the reverse of
8} How do I find all the files made at of after YYYYMMDD HHMM?

94268 Mar 31 2004 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

Here in alt.msdos.batch which is for MS-DOS+Win../95/98/Me the task
is much more tricky.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful batch files and tricks ftp://garbo.uwasa.fi/pc/link/tsbat.zip

foxidrive

unread,
Apr 11, 2004, 10:00:06 PM4/11/04
to
On Sun, 11 Apr 2004 18:33:42 +0100, laurie wrote:

> Oh, and it's Windows 2000 Server.

> I am looking for a simple bat script to delete files in a directory older
> than, say, 7 days. Its for automatic mail log file deleting.

Forfiles can be found at
ftp://ftp.microsoft.com/ResKit/y2kfix/x86/forfiles.exe

@echo off
if not "%3"=="" goto start
echo.Purpose: to delete specified files that are n days or older
echo. in the folder specified on the command line.
echo. (if -s is used it will include all subdirectories as well)
echo.
echo. NOTE 1: Currently it only prints the del command to the screen so
echo. remove the word ECHO from set p2= to activate it, after testing.
echo.
echo. Syntax: %0 "Drv:\Folder Name" "filespec.ext" number_of_days [-s]
echo.
echo.Example: %0 "C:\Program Files\Data" "*.zip" 8 -s
echo.
echo. will delete *.zip files 8 days and older
echo. from the "C:\Program Files\Data" folder
echo. and recurse through the DATA Folders subdirectories too.
echo.
echo. NOTE 2: The "Drive:\Folder Name" and "filespec.ext" should be
echo. "enclosed in DOUBLE QUOTES"
echo.
echo. Compatible with Win9x/ME/NT/W2K/XP
echo.
echo. Forfiles can be found at
echo. ftp://ftp.microsoft.com/ResKit/y2kfix/x86/forfiles.exe
goto end
:start
set p1=%comspec% /C if not @ISDIR
set p2=ECHO del 0x22@PATH\@FILE0x22
::
:: the following 3 lines will write a log file (remove if not needed)
echo Writing log file, please wait...
if exist c:\log.txt del c:\log.txt
FORFILES -p%1 -m%2 -d-%3 %4 -c"%p1%==TRUE echo %p2% >>c:\log.txt"
::
echo Deleting, please wait...
FORFILES -p%1 -m%2 -d-%3 %4 -c"%p1%==TRUE %p2%"
echo Done!
for %%a in (p1 p2) do set %%a=
:end

Richard Bonner

unread,
Apr 11, 2004, 10:27:01 PM4/11/04
to
laurie wrote:

> I am looking for a simple bat script to delete files in a directory
: older than, say, 7 days.
>

> Laurie

*** Try XXCOPY. I think the syntax is:

XXCOPY drive:\directory\*.* /DB8 /RS


You may want to add other switches to tailor that. An XXCOPY link is in
my "DOS Websites" directory at:

http://www.chebucto.ns.ca/~ak621/DOS/Websites.html


You might also find the final version of BU.bat useful. It's at:

www.chebucto.ns.ca/~ak621/DOS/Bat-Adv2.html


Richard Bonner
http://www.chebucto.ns.ca/~ak621/DOS/

Timo Salmi

unread,
Apr 11, 2004, 10:18:18 PM4/11/04
to

I have used other solutions on the XP-line, but have occasionally
tried forfiles on Win95. There is one thing that baffles me about
that program. My time zone is +2 (in winter). On my 95 forfiles
identifies the number of days (by one) incorrectly at midnight +2
hours! Therefore, I've avoided using it when I have other options.

Joe Batch

unread,
Apr 12, 2004, 12:16:48 AM4/12/04
to

> I am looking for a simple bat script to delete files in a directory
> older than, say, 7 days. Its for automatic mail log file deleting.

Here is a simple method that uses pkzip.exe, but requires a user
supplied date.

:: delbydat.bat (delete by date)
@echo off
if "%2"=="" echo. parm1 is date
if "%2"=="" echo. parm 2 is filespec for targeted files
if "%2"=="" goto end
if not exist %2 echo. must be valid filespec
if not exist %2 goto end
echo. Files %2 dated before %1 will be deleted...
choice.com/cyn/tn,4 " are you sure "
if errorlevel 2 goto end
pkzip -T%1 -m-r %temp%\delete.me %2
del %temp%\delete.me
:end

Dr John Stockton

unread,
Apr 12, 2004, 7:49:55 AM4/12/04
to
JRS: In article <Xhfec.684$P%3.286@newsfe1-win>, seen in
news:alt.msdos.batch, laurie <law...@hotmail.com> posted at Sun, 11 Apr
2004 18:33:42 :

>Oh, and it's Windows 2000 Server.

Unwise to post in this group, then; it is for DOS..Win98/ME.

>I am looking for a simple bat script to delete files in a directory older than,
>say, 7 days.

You are saying that the directory must be older than 7 days, but
probably mean the files must be older than 7 days.

> Its for automatic mail log file
>deleting.
>Cant seem to find anything suitable on the net.

Improved searching technique required, then.

In any DOS-like batch system,

HUNT * b#-7 "del" u ; omit u for test

will delete before the beginning of the day 7 ago;

NOWMINUS d7 Eweekago
HUNT * b%weekago% "del" u ; omit u for test

will delete before this time seven days ago, in on-topic systems.


HUNT * D b#-7 "del " + "\*.*" u ; omit u for test

will delete all unprotected files from all aged directories.

HUNT, NOWMINUS via sig line 3.

However, WSH might provide a better solution for use in Win2K.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

Dr John Stockton

unread,
Apr 12, 2004, 8:00:52 AM4/12/04
to
JRS: In article <c5cu9a$i...@poiju.uwasa.fi>, seen in
news:alt.msdos.batch, Timo Salmi <t...@UWasa.Fi> posted at Mon, 12 Apr
2004 05:18:18 :

>foxidrive <mi...@think.melbpc.org.au> wrote:
>> Forfiles can be found at
>> ftp://ftp.microsoft.com/ResKit/y2kfix/x86/forfiles.exe
>
>I have used other solutions on the XP-line, but have occasionally
>tried forfiles on Win95. There is one thing that baffles me about
>that program. My time zone is +2 (in winter). On my 95 forfiles
>identifies the number of days (by one) incorrectly at midnight +2
>hours! Therefore, I've avoided using it when I have other options.

// go to bed earlier, then !! <G>

Presumably it is using UTC/GMT dating; the GMT date changes in Finland
at local 0200h in winter and 0300h in summer.

The traditional DOS date-stamp is implicitly local time, knowing nothing
of zone; but the stamps added in Windows are in units of 100 ns from
1601-01-01 00:00:00 GMT (I don't know whether 95 might have implemented
that differently).

Perhaps forfiles makes the opposite error in the evening for those in
the New World?

0 new messages