Please help: Batch files to delete files older than "X" minutes from a specific directory

J

janefield2002

Hi All,

Sorry - I know very little about batch files.

I have files continually created in a directory - C:\ABC that I want
deleted periodically once they are older than a particular number of
hours. I wonder if anyone can help me with a batch file that will
delete files older than a specific number of minutes, by taking the
minutes argument from command line. So it would be run on commandline
as follows... like "clean C:\ABC X_MINUTES". (actually through a
scheduler so it happens regularly)

Thanks in advance!
 
T

Todd Vargo

Hi All,

Sorry - I know very little about batch files.

I have files continually created in a directory - C:\ABC that I want
deleted periodically once they are older than a particular number of
hours. I wonder if anyone can help me with a batch file that will
delete files older than a specific number of minutes, by taking the
minutes argument from command line. So it would be run on commandline
as follows... like "clean C:\ABC X_MINUTES". (actually through a
scheduler so it happens regularly)

Thanks in advance!

Oh my, another 'spoon feed me' Google poster. This question has been asked
many times over. Please use the Google search engine you are posting from to
locate one of the many existing posts that cover this. Your subject line
already has all of the necessary keywords needed to locate them.

delete files older than X minutes
 
T

Todd Vargo

Todd Vargo said:
Oh my, another 'spoon feed me' Google poster. This question has been asked
many times over. Please use the Google search engine you are posting from to
locate one of the many existing posts that cover this. Your subject line
already has all of the necessary keywords needed to locate them.

delete files older than X minutes

Sorry, on second thought, since you intend to use scheduler, the following
simple batch will do what you want without need to examine the date/time
each file was created. Obviously, you don't want to save this batch in the
c:\abc folder.

@echo off
if exist last.bat call last.bat
type nul>last.bat
for %%? in (c:\abc\*.*) do echo if exist "%%?" del "%%?" >>last.bat
 
T

Timo Salmi

Sorry - I know very little about batch files.
http://lipas.uwasa.fi/~ts/http/http2.html#batch

I have files continually created in a directory - C:\ABC that I want
deleted periodically once they are older than a particular number of
hours. I wonder if anyone can help me with a batch file that will
delete files older than a specific number of minutes, by taking the
minutes argument from command line. So it would be run on commandline
as follows... like "clean C:\ABC X_MINUTES".

The essence of the task is to find out the timestamp X_MINUTES ago.
Then (not covered below) one compares the files' datestamps with that
value.

Please note. Followups narrowed down to alt.msdos.batch.nt only.

Also I'll assume XP. One always should specify one's OS when asking!

DRAFT:
147} What was the time 100 minutes ago?

@echo off & setlocal enableextensions
::
rem The 24h time format is assumed
rem as always is sensible in an international se
::
:: Get the current time: hour and minutes
for /f "tokens=1,2 delims=:" %%a in ('time /t') do (
set hhNow_=%%a
set mmNow_=%%b
)
:: Remove any leading zeros to avoid the octal trap
for /f "tokens=* delims=0" %%a in ('echo %hhNow_%') do set hhNow_=%%a
for /f "tokens=* delims=0" %%b in ('echo %mmNow_%') do set mmNow_=%%b
::
:: Current time in minutes since midnight
set /a mmSinceMidnight=60*%hhNow_%+%mmNow_%
::
:: Display
echo hhNow_ %hhNow_%
echo mmNow_ %mmNow_%
echo mmSinceMidnight %mmSinceMidnight%
::
:: How many minutes to calculate backwards?
set back_=0
set rollover=
if not "%~1"=="" set back_=%1
::
:: How many minutes since midnight was back minutes ago?
set /a backSinceMidnight=%mmsinceMidnight%-%back_%
if %backSinceMidnight% LSS 0 (
set /a backSinceMidnight=%backSinceMidnight%+24*60
set rollover=true
)
:: Display
echo backSinceMidnight %backSinceMidnight%
::
:: Convert the result to hours and minutes
set /a hhBack_=%backSinceMidnight%/60
set /a mmBack_=%backSinceMidnight%-60*%hhBack_%
:: Display
echo hhBack_ %hhBack_%
echo mmBack_ %mmBack_%
if defined rollover echo Yesterday
::
endlocal & goto :EOF

The output e.g.
C:\_D\TEST>cmdfaq 100
hhNow_ 9
mmNow_ 33
mmSinceMidnight 573
backSinceMidnight 473
hhBack_ 7
mmBack_ 53

All the best, Timo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top