Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete files older than X hours or minutes?

Tags:

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
like image 699
Vicky Malhotra Avatar asked May 14 '16 11:05

Vicky Malhotra


1 Answers

@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.

like image 164
Rishav Avatar answered Sep 28 '22 02:09

Rishav