Automatically responding yes to forfiles

5

I'm looking to automatically delete files older than 7 days old with forfiles.

The code below works when I do it manually and respond yes to deleting the files. How can I incorporate the yes into this?

This is the output:

E:\>forfiles -p "H:\SHARED\Scans" -s -m *.* -d -7 -c "cmd /c del @path"
Could Not Find H:\SHARED\Scans\.DS_Store
H:\SHARED\Scans\XXX\DOC006.XSM\*, Are you sure (Y/N)?

PHLiGHT

Posted 2011-01-05T16:25:24.200

Reputation: 396

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 – 2018-01-16T02:13:26.130

1what happens when you try echo y | before it? – barlop – 2011-01-05T16:30:46.227

Answers

6

You could try adding in a /Q /S, though be aware that this may not in fact do what you really want it to:

/Q Quiet mode, do not ask if ok to delete on global wildcard
/S Delete specified files from all subdirectories

E:\forfiles -p "H:\SHARED\Scans" -s -m . -d -7 -c "cmd /c del /Q /S @path"

You are probably better off either using CSCRIPT (with your choice of VBScript or JScript) or PowerShell. Check out this answer from StackOverflow: https://stackoverflow.com/questions/1575493/how-to-delete-empty-subfolders-with-powershell

Here is some vbscript to accomplish a similar task:

Dim fso, folder, folders
Set fso = CreateObject("Scripting.FileSystemObject")
Set parent = fso.GetFolder("H:\SHARED\Scans")
Set folders = parent.SubFolders

' delete any folder older than 7 days
For Each folder in folders
    If Abs(DateDiff("d",Date, folder.DateCreated)) > 7 Then
        folder.Delete(True) 'force delete
    End If
Next

Goyuix

Posted 2011-01-05T16:25:24.200

Reputation: 6 021

Might want to try H:\SHARED\Scans\ seems the .DS_STORE is getting appended to the current folder @PHLiGHT – Sathyajith Bhat – 2011-01-05T17:33:13.693

0

This example removes all files in the folder "G:\db_bk_copies" older than 3 days without asking the users to confirm deletion (Are you sure (Y/N)?):

forfiles -p "G:\db_bk_copies" -s -m * /D -3 /C "cmd /c del /a-s @path"

Ahmed Genedi

Posted 2011-01-05T16:25:24.200

Reputation: 1

-1

Verify that the files are not Hidden or system files.

If System try:

forfiles -p "H:\SHARED\Scans" -s -m *.* -d -7 -c "cmd /c del /a-s @path"

If Hidden Try:

forfiles -p "H:\SHARED\Scans" -s -m *.* -d -7 -c "cmd /c del /a-h @path"

user616835

Posted 2011-01-05T16:25:24.200

Reputation: 1

Please read the question again carefully. Your answer does not answer the original question. – DavidPostill – 2016-07-13T19:57:10.173