0

I see this - Batch file to delete files older than N days

But how can I delete files older than N hours or minutes?

Thanks.

Community
  • 1
  • 1
houngtze
  • 3
  • 1
  • 1
  • 7
  • Please do not read only the most voted answer, read also the other answers. Several other answers support also smaller time deltas like [my answer](http://stackoverflow.com/a/28395552/3074564) written yesterday which works with seconds. – Mofi Feb 09 '15 at 16:04
  • [See this answer](http://stackoverflow.com/a/27297874/1683264) for other ideas. – rojo Feb 09 '15 at 17:31

5 Answers5

5

Neither of the solutions here worked for me. They are very innovative solutions, and the first one (.net/FileTimeFilerJS.bat [sic]) actually worked once for me. When I tried to call it on another folder, however, it simply crashed. I tried the second one (jscript/FileTimeFilterJs.bat), but it did not successfully delete all files older than one hour.

My solution requires an executable, but I found it was the best solution for me.

I downloaded FindUtils from GnuWin32, and extracted 3 files (you have to download the two zips below): libintl3.dll, libiconv2.dll, and find.exe (combined they are about 1MB in size). I rename find.exe to gnu_find.exe just to avoid the Windows FIND command.

Then, you can run the beautiful GNU command (below command is for all zip files older than 120 minutes in folder specified by environment variable %fldr%):

gnu_find %fldr% -name *[.:]zip -type f -mmin +120 -delete

Instead of *.zip, I use *[.:]zip, because Windows 7+ will expand the *.zip before find can use it.

Find Utils bin

Find Utils deps

Dan
  • 1,823
  • 16
  • 18
4

Its challenging to do this (independent of time date settings) even with WSH\Jscript\VBscript as WSH does not return date object when you request the date/time properties of a file ,but a string that depends on time settings (wmi could be used but this will hit the performance). I suppose you have installed .net framework which make the task far more easier. Here's a hybrid tool that should work in your case. You can save it with whatever name you want. Here's how to use it (its called FileDateFilterJS.bat in this example):

@echo off

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

pause

Where the hours are 5 - -hh -5 and directory is current - "." .You can remove the echo before del command.

This script took me far more time than I've expected (despite I've researched the topic and has some parts ready) and is no so heavy tested so probably is still not buggy-free.And I suppose the help message and options could be improved.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 2
    `FSO.GetFile.DatePropertyOfSomeSort` seems to handle `Date()` object math in stride for me. See [this answer](http://stackoverflow.com/a/27297874/1683264) for an example. I almost hate to point this out, as you seem to have put a substantial amount of time and effort into your JSC hybrid. `o°/` – rojo Feb 09 '15 at 17:36
  • 1
    Yep, confirmed. `WSH.Echo(typeof file.DateLastModified)` echoes `date`. Well, have a +1 anyway. – rojo Feb 09 '15 at 17:46
  • @houngtze If npocmaka's answer was helpful, please consider marking it as accepted. [See this page](http://meta.stackexchange.com/q/5234/275822) for an explanation of why this is important. – rojo Feb 09 '15 at 18:14
  • 1
    @rojo - this is strange! despite it is a date object the method `getTime()` is not applicable! – npocmaka Feb 09 '15 at 20:01
  • 1
    @rojo and this `var n_date=new Date();WScript.Echo(typeof n_date);` prints `object` - pretty interesting indeed – npocmaka Feb 09 '15 at 20:08
  • 1
    Tested `Date()` on jsfiddle. `typeof new Date()` returns `object` as well. Maybe the Windows date object is converted automatically to a JS Date object similar to the way an integer can be converted to a boolean, or the way `typeof ("1" * 1) === 'number')`. Anyway, `.getTime()` seems to be redundant, as you can subtract Windows date object from JS date object and end up with an epoch value. – rojo Feb 09 '15 at 20:13
  • 1
    Know what else you can do? `var timestamp = new Date(fso.getFile(loc).DateLastModified);` Then you can use [JS date methods](http://www.w3schools.com/jsref/jsref_obj_date.asp) like `.getTime()` and `.getMonth()` and `setMinutes()`. Too bad the FSO properties are read-only though. – rojo Feb 09 '15 at 20:29
  • @rojo - good catch.Works pretty (even with no standard time settings) well and makes possible wsh solution. – npocmaka Feb 09 '15 at 20:39
  • @rojo - can you answer this question -http://stackoverflow.com/questions/28393865/how-to-get-folder-age-the-number-of-days-from-creation-or-modification-date - as your method will work best :-) – npocmaka Feb 09 '15 at 20:57
1
@echo off
cd /d "your file path"
:: delete files 3 hours ago. If you want other condition, refer https://www.w3schools.com/asp/func_dateadd.asp
echo wscript.echo dateadd("h",-3,now())>GetOldDate.vbs
for /f "tokens=1,2 delims= " %%i in ('cscript /nologo GetOldDate.vbs') do (
  set d=%%i
  set t=%%j
)
echo "%d%"
echo "%t%"
for /f "tokens=1,2,3 delims=/" %%a in ("%d%") do (
  set y=%%a
  set m=%%b
  set d=%%c
)
for /f "tokens=1,2,3 delims=:" %%a in ("%t%") do (
  set h=%%a
  set mm=%%b
  set s=%%c
)

if %m% LSS 10 set m=0%m%
if %d% LSS 10 set d=0%d%
if %h% LSS 10 set h=0%h%
if %mm% LSS 10 set mm=0%mm%

set OldDate=%y%/%m%/%d% %h%:%mm%
echo "%OldDate%"
del GetOldDate.vbs
for %%a in (*) do ( 
  if "%%~ta" lss "%OldDate%" (
    echo %%a
    echo "%%~ta"
    del %%a
  )
)
xiansweety
  • 113
  • 1
  • 4
1

Deleting files:

@echo off

echo >%temp%\temp.vbs  for each File In CreateObject("Scripting.FileSystemObject").GetFolder("[path_file]").Files
echo >>%temp%\temp.vbs If DateDiff("n",File.DateCreated,Now) ^> 30 Then File.Deletee
echo >>%temp%\temp.vbs next

cscript /nologo %temp%\temp.vbs 

Exit /b

Deleting folders:

@echo off

echo >%temp%\temp.vbs  for each Folder In CreateObject("Scripting.FileSystemObject").GetFolder("[Path_folder]").subFolders
echo >>%temp%\temp.vbs If DateDiff("n",Folder.DateCreated,Now) ^> 30 Then Folder.Delete
echo >>%temp%\temp.vbs next

cscript /nologo %temp%\temp.vbs 

Exit /b

To vary the time difference, we need to substitute in DateDiff("n",[Folder or File].DateCreated,Now) ^> 30, the condition (in this case 30 minutes).

.DateCreated can be changed by: .DateLastAccessed or .DateLastModified.

Time difference: "yyyy" Year, "m" Month, "d" Day, "h" Hour, "n" Minute, "s" Seconds.

["Path_file o Path_folder"] has to be sustituded for the target file or folder.

SNR
  • 712
  • 1
  • 8
  • 22
0

Here's another script that relies only on WSH/JSCRIPT and almost the same interface (and does not require .net installed).To show files older than 5 hours use:

 FileTimeFilterJS.bat "." -hh -5 -filetime modified
npocmaka
  • 55,367
  • 18
  • 148
  • 187