【问题标题】:How to remove all folders of name x within a directory using cmd/batch file如何使用 cmd/批处理文件删除目录中名称为 x 的所有文件夹
【发布时间】:2012-05-01 03:08:24
【问题描述】:

我有一个名为 x 的文件夹,其中包含许多子文件夹和文件。我想删除 x 中存在的名为 y 的文件夹及其所有子文件夹。必须删除的所述文件夹可能包含也可能不包含任何文件。我相信我可以使用 cmd 或某种批处理文件来做到这一点,但我是一个命令行新手,真的可以使用一些帮助。

一个简单的事情是 rd 文件夹的名称,它有效,但我相信有比单独删除每个文件夹更好的方法.. 就像遍历所有文件夹的循环一样。

谢谢

编辑:澄清一下,我在 x 中有 y(需要删除的文件夹),它可以在 x 的任何子文件夹中,并且可以处于任何深度。我也在看答案,我可能需要一些时间才能接受任何答案。请多多包涵:)

【问题讨论】:

    标签: batch-file cmd


    【解决方案1】:

    这是另一个解决方案,用于描述脚本的每个部分:

    @Echo OFF
    REM Important that Delayed Expansion is Enabled
    setlocal enabledelayedexpansion
    REM This sets what folder the batch is looking for and the root in which it starts the search:
    set /p foldername=Please enter the foldername you want to delete: 
    set /p root=Please enter the root directory (ex: C:\TestFolder)
    REM Checks each directory in the given root
    FOR /R %root% %%A IN (.) DO (
        if '%%A'=='' goto end   
        REM Correctly parses info for executing the loop and RM functions
        set dir="%%A"
        set dir=!dir:.=!
        set directory=%%A
        set directory=!directory::=!
        set directory=!directory:\=;!   
        REM Checks each directory
        for /f "tokens=* delims=;" %%P in ("!directory!") do call :loop %%P
    )
    REM After each directory is checked the batch will allow you to see folders deleted.
    :end
    pause
    endlocal
    exit
    REM This loop checks each folder inside the directory for the specified folder name. This allows you to check multiple nested directories.
    :loop
    if '%1'=='' goto endloop
    if '%1'=='%foldername%' (
        rd /S /Q !dir!
        echo !dir! was deleted.
    )
    SHIFT
    goto :loop
    :endloop
    

    如果您不想被提示,您可以从初始变量前面取出/p,然后在= 后面输入它们的值:

    set foldername=
    set root=
    

    您还可以删除循环部分中的echo 和末尾部分中的pause,以便批处理以静默方式运行。

    它可能有点复杂,但代码可以应用于很多其他用途。

    我测试了它在C:\Test 中寻找相同文件夹名称qwerty 的多个实例:

    C:\Test\qwerty
    C:\Test\qwerty\subfolder
    C:\Test\test\qwerty
    C:\Test\test\test\qwerty
    

    剩下的就是:

    C:\Test\
    C:\Test\test\
    C:\Test\test\test\
    

    【讨论】:

      【解决方案2】:
      FOR /D /R %%X IN (fileprefix*) DO RD /S /Q "%%X"
      

      小心使用它...

      对于 RD 命令:

      /S      Removes all directories and files in the specified directory
              in addition to the directory itself.  Used to remove a directory
              tree.
      /Q      Quiet mode, do not ask if ok to remove a directory tree with /S
      

      FOR 命令用于循环遍历文件或变量的列表,选项很容易记住,仅目录递归。

      【讨论】:

      • 你能解释一下它到底是做什么的吗?什么是文件前缀?还有那些标志……现在就试试吧。
      • @Achshar:基本上,用 Y 文件夹的名称代替 fileprefix (… IN (Y*) DO …)。此外,将 X 文件夹的路径放在 /R%%X 之间(Jermaine 忽略了 /R 开关需要一个参数,这是一个要遍历的根文件夹)。还有一件事,您还需要添加一个条件以确保您删除的是Y 文件夹,而不是与Y* 掩码匹配的Ysomething。这样的事情可能会做:… DO IF "%%~nxX" == "Y" RD ….
      • 您可以通过在RD 之前添加echo 来测试命令的试运行。
      【解决方案3】:

      此类主题的一个共同问题是,如果目标文件夹在多个级别上存在实例,大多数方法都会导致错误,因为当删除高级文件夹时,它下面的所有文件夹都会消失。例如:

      C:\X\Y\subfolder
      C:\X\Y\subfolder\one\Y
      C:\X\Y\subfolder\two\Y
      C:\X\Y\subfolder\three\Y
      C:\X\test
      C:\X\test\test
      

      上一个示例生成一个包含 4 个名为 Y 的文件夹的列表,这些文件夹将被删除,但在第一个文件夹被删除后,其余三个名称不再存在,在尝试删除它们时会导致错误消息。我知道在你的情况下这是一种可能性。

      为了解决这个问题,文件夹必须按自下而上的顺序删除,即最里面的文件夹必须先删除,最顶层的文件夹必须最后删除。实现这一点的方法是通过递归子程序

      @echo off
      rem Enter into top level folder, process it and go back to original folder
      pushd x
      call :processFolder
      popd
      goto :EOF
      
      :processFolder
      rem For each folder in this level
      for /D %%a in (*) do (
         rem Enter into it, process it and go back to original
         cd %%a
         call :processFolder
         cd ..
         rem If is the target folder, delete it
         if /I "%%a" == "y" (
            rd /S /Q "%%a"
         )
      )
      exit /B
      

      虽然在这种特殊情况下,由其他方法引起的问题只是多个错误消息,但在其他情况下,这种处理顺序是基本的。

      【讨论】:

        【解决方案4】:

        使用以下内容制作 .bat:

        del /q "C:\Users\XXX\AppData\Local\Temp\*"
        
        FOR /D %%p IN ("C:\Users\XXX\AppData\Local\Temp\*.*") DO rmdir "%%p" /s /q
        

        【讨论】:

          猜你喜欢
          • 2011-10-13
          • 1970-01-01
          • 1970-01-01
          • 2021-12-07
          • 2021-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多