How to remotely delete subfolders older than x day

2019-09-08 16:55发布

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条回答
手持菜刀,她持情操
2楼-- · 2019-09-08 17:48

You are almost there, just a few things need to be changed:

  • no /S for forfiles, as you are interested in the immediate subdirectories of xFOLDER 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
查看更多
登录 后发表回答