Page 1 of 1

trying to delete files in one directory older than today

Posted: 06 Jun 2006 09:14
by gigglehurts
Hi - I'd like to write a batch file for windows that would look in one directory and delete the text files that have a modified date older than today. I was looking at a related script on this site, but would it apply to the modfied date?

Thanks,
Stephen

delete files not from today

Posted: 06 Jun 2006 12:40
by resource58
I used this to delete files not from today, where files were of the form FOO20060606-1000.txt. You can change the dir command appropriately

for /f %%i in ('dir /b %SRCDIR% ^| findstr /v /b FOO%TODAY%') do (
del /f %SRCDIR%\%%i
)

Getting %TODAY% from date/t is left as an exercise to the reader. :)
Alternately, something like this

:: %1 n entries to leave
:DelFn
for /f "skip=%1 delims=:" %%a IN ('DIR /O-D /B %SRCDIR%\*.*') do (
del "%SRCDIR%\%%a"
)
goto :eof

will delete n files sorted by date. Play with the dir command to get what you want. Reading the fileattr is another way.

http://resource58.blogspot.com

Posted: 07 Jun 2006 19:29
by DosItHelp
Try this:

Code: Select all

cd "\yourdirectory\here"
for %%a in (*.txt) do echo.%%~ta|findstr /v /b /c:"%date:~4%">NUL&&del "%%a"


The FOR loop - finds all txt files in the directory.
echo.%%~ta - resolves into file date and file time which will be piped into findstr.
%date:~4% - resolves into the current date without the day string.
findstr /v /b /c:"...">NUL only succeeds when %%~ta doesn't start with todays date.
>NUL - suppresses any display output
&& - makes sure that del "%%a" only executes when the findstr command succeeded.

DOS IT HELP? :wink: