How to remotely delete subfolders older than x days using forfiles

瘦欲@ 提交于 2019-12-12 02:22:26

问题


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 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


来源:https://stackoverflow.com/questions/33537632/how-to-remotely-delete-subfolders-older-than-x-days-using-forfiles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!