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
trying to delete files in one directory older than today
Moderator: DosItHelp
-
- Posts: 1
- Joined: 06 Jun 2006 09:02
delete files not from today
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
elFn
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
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
elFn
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
Try this:
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?
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?