问题
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
/Sforforfiles, as you are interested in the immediate subdirectories ofxFOLDERonly; - the search mask needs to be changed from
.to*; - to skip all enumerated files, a check is required (using
if);forfilesdelivers a variable@isdirwhich indicates whether the current item is a file (FALSE) or a directory (TRUE); deldeletes files, so to delete directories,rmdiris required; switch/Sallows to delete even non-empty directories, switch/Qprevents 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