using forfiles

Sun, 2014-06-08 14:34 by gillypalm · Forum/category:

Hi I am new to using forefile im wondering if anyone can help me
Im trying to delete all the folder and files in Windows/temp which are older than 20 days.
this is what I have done via cmd line

cd c:\windows\system32
Forefiles /p c:\windows\Temp /s /d -20 /c "cmd /c echo @PATH\@FILE & cmd /c del @FILE" >\gill-delete.txt

I wanted it to delete all the files and folders in the windows\temp directory that are older than 20 days and then list the output in a file called gill-delete.

what it doses is either just sit there and not seem to do anything, or if im lucky I get one file deleted and listed and that is it,.

I know im doing something wrong, but what im not sure.. can anyone help please. Also If I want to delete other files and folders in the same command can I just list them straight agetre starting a new line with forefiles again,

sorry but im new to this

thanks in advance gill

Deleting folders is different.

Sun, 2014-06-08 19:45 by admin

This should delete files, but not folders. You have to distinguish between the two, because an attempt to delete a folder with a command that is meant for files may interrupt the process. I guess that is what has happened.

forfiles /p "C:\Windows\Temp" /s /d -20 /c "cmd /c if @isdir==FALSE del @file" >\gill-delete.txt

Folders are more difficult to handle. You would first have to delete all files older than your criterion in all subfolders, then attempt to delete each folder. If done right, this would succeed only for empty folders.

You may also have to look out for files that have been set to read-only and for files and folders to which you have no access rights. The following example deletes everything in there, not just older files and empty folders, so you'd have to modify it, using forfiles:

cacls *.* /T /E /C /P Everyone:F
del *.* /s /f /q
for /d %%d in (*.*) do rmdir "%%d" /s /q

I have no complete, ready-made solution on hand, so you'll have to keep trying.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.