trying to delete files in one directory older than today

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gigglehurts
Posts: 1
Joined: 06 Jun 2006 09:02

trying to delete files in one directory older than today

#1 Post by gigglehurts » 06 Jun 2006 09:14

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

resource58

delete files not from today

#2 Post by resource58 » 06 Jun 2006 12:40

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 07 Jun 2006 19:29

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:

Post Reply