Solved Batch file to delete files older than

May 26, 2012 at 07:10:29
Specs: Windows XP
I need a batch file to run every 5 minutes and delete all files that are older than 5 min in:
C:\vsi103\Progress Software\WebClientApps\VSI\RecTracLAN\temp\


See More: Batch file to delete files older than

Report •

June 16, 2012 at 20:32:38
✔ Best Answer
I figure you've gotten an answer by now, but I was bored so...

@echo off & setlocal enabledelayedexpansion
set x=%0
set timer=300
if defined x goto :first
set z=%time:~0,!s!%
if %z% equ %prev% goto :countsecs
set /a cc+=1
echo %cc%
set prev=%z%
:echo %prev%
if %cc% lss %timer% goto :countsecs

:this does the deletion: any file without "A" archive-bit set is killed.
:alternative: dir /b*.* >\temp then use temp as a list of files to delete.
for /f "tokens=*" %%a in ('attrib *.* ^| sort') do (
set xy=%%a
set ff=!xy:~0,2!
if "!ff!" equ "A " (
echo A is set, file not qualified for delete:!xy!
) else (
set xy=!xy:~11,99!
:disabled this for testing.
:delete !xy!
echo this file to delete:!xy!]

:remove the Archive attribute from all existing files (0 to 300 seconds old)
:these will be deleted on the next 5-minute cycle
attrib -a *.*
:or: dir /b *.* >\temp
set prev=-1
set cc=0
echo 300 second countdown starting at %time%
goto :countsecs

:special handling for the first cycle: don't delete, just mark existing files
set x=%0
:crude attempt to test time format. prone to failure in unconventional environments!
set s=99
set test=%time%
set test2=%test%
set test=%test:.=*%
if %test% equ %test2% (
echo no centiseconds!
) else (
set s=-2
goto :archset
:end batch

I attempted to avoid any time comparisons or time-math, but you still might have issues if your time format is "exotic" (ask M2go about this date/time stuff, Lol!)

Report •
Related Solutions

Ask Question