问题
I need to remotely delete sub folders older than 7 days, was able to find a syntax like this, but what it does is it loops through the subfolders and deletes the files in it older than 7 days. Any idea how can I delete the subfolders in xFOLDER older than 7 days as well?
PushD "\\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\" & ("forfiles.exe" /s /m "." /d -7 /c "cmd /c del @file") & PopD
回答1:
You are almost there, just a few things need to be changed:
- no
/S
forforfiles
, as you are interested in the immediate subdirectories ofxFOLDER
only; - the search mask needs to be changed from
.
to*
; - to skip all enumerated files, a check is required (using
if
);forfiles
delivers a variable@isdir
which indicates whether the current item is a file (FALSE
) or a directory (TRUE
); del
deletes files, so to delete directories,rmdir
is required; switch/S
allows to delete even non-empty directories, switch/Q
prevents any prompts whether or not to delete;
So this modified code should do what you want (written as multiple lines for legibility reasons):
PushD "\\IP ADDRESS\FOLDERA\FOLDERB\FOLDERC\FOLDERD\xFOLDER\" ^
& ("forfiles.exe" /P "*" /D -7 /C "cmd /C if @isdir==TRUE rmdir /S /Q @path") ^
& PopD
来源:https://stackoverflow.com/questions/33537632/how-to-remotely-delete-subfolders-older-than-x-days-using-forfiles