批处理脚本自动删除空文件夹 - 需要帮助添加例外 [英] Batch script to automatically delete empty folders - need help adding exceptions

查看:329
本文介绍了批处理脚本自动删除空文件夹 - 需要帮助添加例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的批处理的经验是相当有限的,但我还是设法编写如下脚本删除我的目标文件夹的所有子文件夹中的空

My batch experience is rather limited but I did manage to write the following script to delete all empty subfolders of my target folder.

set "Target=C:\Target" 
for /f "delims=" %%i in ('dir "%Target%" /A:D /B /S ^| sort /r') do rd "%%i" 2>NUL >NUL

我的问题是:结果
1.我希望能够保持子文件夹的第一层完好无损,只删除空文件夹在下面的图层。结果
2.如果有可能,我也想完全基于他们的名字来跳过某些文件夹。

My problems are:
1. I would like to be able to keep the first layer of subfolders intact and only delete empty folders in the following layers.
2. If it is possible I would also like to skip certain folders completely based on their name.

是否有可能做到这一点还是我需要写剧本的所有子文件夹我想清理?

Is it possible to do that or would i need to write the script for all of the subfolders i want to clean up?

推荐答案

在code以下文件夹中的以下文件夹结构进行了测试 C:\\目标

The code below was tested on following folder structure in folder C:\Target


  • Folder1中

    • 文件夹删除

    • 文件夹保持

    • FolderToKeepB

    • Folder1
      • Folder To Delete
      • Folder To Keep A
      • FolderToKeepB

      • 文件夹保持

      • 不为空文件夹

        • 文件中Folder.txt

        以下运行是批处理文件后生成的文件夹结构

        The resulting folder structure after running the batch file below was


        • Folder1中

          • 文件夹保持

          • FolderToKeepB

          • Folder1
            • Folder To Keep A
            • FolderToKeepB

            • 文件夹保持

            • 不为空文件夹

              • 文件中Folder.txt

              就在一个文件夹中的文件夹删除已被删除,因为这一个人不应该总是保持它确实是空的。

              Just the single folder Folder To Delete was deleted as this one should not be always kept and it was indeed empty.

              @echo off
              rem For each subfolder in C:\Target do ...
              for /D %%D in ("C:\Target\*") do (
                  rem For each subfolder in found subfolder of C:\Target do ...
                  for /D %%S in ("%%~D\*") do call :DeleteFolder "%%~S"
              )
              rem This goto :EOF results in exiting the batch file.
              goto :EOF
              
              rem Subroutine for easier comparing the name of the current
              rem subfolder with the name of the subfolders to always keep.
              rem The goto :EOF commands below just exit the subroutine.
              :DeleteFolder
              if "%~nx1"=="Folder To Keep A" goto :EOF
              if "%~nx1"=="FolderToKeepB" goto :EOF
              
              rem Delete this subfolder which fails if it is not empty.
              rem The error message is suppressed by redirecting output stream
              rem stderr (standard error) with handle 2 to the NUL device.
              rd "%~1" 2>nul
              goto :EOF
              

              有关所使用的命令的详细信息读取帮助输出印入控制台窗口中运行

              For more details on the used commands read help output printed into a console window on running

              • For command call: call /? or help call
              • For command for: for /? or help for
              • For command goto: goto /? or help goto
              • For command rmdir (rd): rd /? or help rd or rmdir /? or help rmdir
              • Using command redirection operators

              这篇关于批处理脚本自动删除空文件夹 - 需要帮助添加例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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