Vicky Malhotra
Vicky Malhotra

Reputation: 360

How to delete files older than X hours or minutes?

How to delete files older than X hours or minutes?

I have a batch file that delete files in a directory which is older than 1 day, but I need for minutes and hours.

REM Remove files older than 1 day
forfiles /p "C:\Users\Username\Downloads" /s /m *.* /c "cmd /c Del @path" /d -1

Upvotes: 6

Views: 32597

Answers (2)

npocmaka
npocmaka

Reputation: 57252

The tools/answers posted by Rishav should help you though they are not so heavily tested (the line posted by him should work though).To bulletproof your query you might need some fre more command line switches. Here's an example with the WSH version of filetimefilter:

call filetimefilterjs.bat "C:\somedir" -hh -10 -direction after  -filetime created

this will show the files newer than 10 hours in c:\somedir (it is not recursive).Hope this will help.

Upvotes: 1

Rishav
Rishav

Reputation: 4068

@echo off

for /f "tokens=* delims=" %%# in ('FileDateFilterJS.bat  "." -hh -5') do (
    echo deleting  "%%~f#"
    echo del /q /f "%%~f#"
)

pause

Replace "." with your Working directory Here is the FileDateFilterJS.bat given by npocmaka

https://github.com/npocmaka/batch.scripts/blob/master/hybrids/.net/FileTimeFilerJS.bat

IF this does not work give the below code a shot although the above code works for me the below code will work for number of days and not hours.

forfiles -p "C:\what\ever" -s -m *.* /D -<number of days> /C "cmd /c del @path"

Batch file to delete files older than N days

Note that if you want files OLDER than 10 days, you need to specify -d "-10". -ve means "older than", +ve means "newer than". You can also specify DDMMYY or -DDMMYY format as the parameter to -d.

If you don't have forfiles installed on your machine, copy it from any Windows Server 2003 to your Windows XP machine at %WinDir%\system32. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.

Batch file to delete files older than N hours or minutes

This might help too.

Hope I helped.

Upvotes: 4

Related Questions