如何使用forfiles远程删除x天之前的子文件夹 [英] How to remotely delete subfolders older than x days using forfiles

查看:567
本文介绍了如何使用forfiles远程删除x天之前的子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要远程删除7天以上的子文件夹,能够找到这样的语法,但是它的作用是循环遍历子文件夹并删除7天以上的文件。知道如何删除7天以上的xFOLDER中的子文件夹吗?

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


推荐答案

您已经准备就绪,只需几件事更改:

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


  • 没有 / S for forfiles ,因为您仅对 xFOLDER 的直接子目录感兴趣;

  • 搜索掩码必须为更改为更改为 *

  • 跳过所有枚举的文件,需要检查(使用 if ); forfiles 提供变量 @isdir ,该变量指示当前项是否为文件( FALSE )或目录( TRUE );

  • del 删除文件,因此要删除目录,需要 rmdir ;开关 / S 允许删除甚至非空目录,开关 / Q 防止任何提示是否删除;

  • 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

这篇关于如何使用forfiles远程删除x天之前的子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆