2

I'm using the following command to delete files older than X days (%numb% is the amount of days):

forfiles -p "%temp%" -s -m *.* -d %numb% -c "cmd /c del @path"

Still, when I execute it it just prompts me with all features of forfiles and how it should be used.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Deniz Zoeteman
  • 9,691
  • 26
  • 70
  • 97
  • If you don't wont to use any 3rd party software, look at this http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days/1322886#1322886 – Jay Aug 28 '09 at 12:18
  • Note that contrary to what MS docs say, `/m *.*` in `forfiles` does not match all files. It will only match files whose names have an extension. If you want to match *all* files, you need `/m *`. Or just omit `/m` entirely, since `/m *` is the default. – AnT stands with Russia Jan 16 '18 at 02:12

3 Answers3

4

You may want to change your "DEL @path" to "echo @path" until you get the kinks worked out...

On my XPsp3 machine, I noticed that a hyphen before the number of days (%numb%) was important. So, this:

forfiles -p "%temp%" -s -m *.* -d 30 -c "cmd /c echo @path"

yeilded this: ERROR: No files found with the specified search criteria.

but this:

forfiles -p "%temp%" -s -m *.* -d -30 -c "cmd /c echo @path"

gave me a directory listing

RobW
  • 424
  • 2
  • 5
1

One guess, since you haven't given much information, is if you're not on 2003/Vista, then you should not have a space after each parameter:

forfiles -p"%temp%" -s -m*.* -d%numb% -c"cmd /c del @path"

It's also possible your substitution parameters are not working as expected. I find it helpful often to prefix the command with "echo" to see what's actually being processed:

echo forfiles -p "%temp%" -s -m *.* -d %numb% -c "cmd /c del @path"
lavinio
  • 23,931
  • 5
  • 55
  • 71
0

Natively, you can use vbscript. this example, num days is 30

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
numdays=30
today=Now
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders
        Go eFolder
    Next
    For Each strFile In objDIR.Files
        If DateDiff("d",strFile.DateLastModified,today) >= numdays Then
           WScript.Echo "file found that is 1 month old or more: " & strFile
           'objFSO.DeleteFile(strFile) 'uncomment to use
        End If 
    Next 
  End If  
End Sub
ghostdog74
  • 327,991
  • 56
  • 259
  • 343