AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1582977
Accepted
bat_cmd
bat_cmd
Asked: 2020-09-05 02:54:17 +0800 CST2020-09-05 02:54:17 +0800 CST 2020-09-05 02:54:17 +0800 CST

从 Windows 的批处理文件中删除特定文件夹(如果它为空)?

  • 772

我想删除一个名为“NEW”的文件夹,但前提是该文件夹为空。

这是我尝试过的:

if exist "NEW\*" goto finished
if not exist "NEW\*" goto remove_new

:remove_new
rd /s /q "NEW\"
goto finished

:finished
exit

以上似乎是考虑到 NEW 文件夹存在所以它没有删除它?

windows batch
  • 3 3 个回答
  • 1463 Views

3 个回答

  • Voted
  1. Best Answer
    LPChip
    2020-09-05T03:31:47+08:002020-09-05T03:31:47+08:00

    默认情况下,rd 只会删除非空文件夹。如果一个文件夹有子文件夹,它也可以被认为是非空的。

    通过暗示/s,您说:删除文件夹内的所有文件和文件夹,然后删除该文件夹。

    如果你只是这样做:

    rd /q new
    

    它将尝试删除新文件夹。如果它找到文件和/或文件夹,它将显示消息The folder is not empty.

    /q 不会删除该消息。为了静默尝试删除文件夹但不显示错误,请使用以下命令:

    rd /q new 2>nul
    

    该命令说:尝试以静默方式删除文件夹。屏幕上显示的任何错误都应该不存在(又名,未显示)。

    • 1
  2. Albin
    2020-09-05T03:32:06+08:002020-09-05T03:32:06+08:00

    如果您只删除空文件夹,而不是整个脚本,您可以rd在没有/s开关(和q)的情况下简单地使用。所以你的命令将是:

    rd "NEW\"
    

    这只会在目录存在且为空的情况下删除目录。否则它将返回错误消息。

    PS。文件夹名称不区分大小写就可以了rd "new\"。

    聚苯乙烯。尽管在这种情况下您不需要它,但""如果您使用带有空格的文件夹(如"new 01".

    购买力平价。您也不需要\文件夹名称末尾的反斜杠 ( ),因为rd仅适用于文件夹。但它有助于使用其他命令区分文件和文件夹。例如exist "foldername\"将应用于文件夹exist "name"将应用于文件和/或文件夹。

    • 0
  3. Io-oI
    2020-09-05T06:46:11+08:002020-09-05T06:46:11+08:00

    您的命令rd /q new或仅rd new适用于删除空文件夹。

    对于包含文件和/或子文件夹的文件夹,此命令不起作用。

    Obs.: 1要删除包含子文件夹/递归的文件夹,请添加/s:


    1.文件夹存在且为空:

    rd /q new   ⁄⁄ it work
    
    rem :: or... 
    rd new     ⁄⁄ it work

    2.文件夹包含文件或子文件夹(是否为空):

    rd /q new   ⁄⁄ does not work
    
    rem :: or... 
    rd new      ⁄⁄ does not work

    3.文件夹包含文件或子文件夹(是否为空):

    rd /q /s new   ⁄⁄ it work
    
    rem :: or... 
    rd /s new     ⁄⁄ it work

    4.当您返回true/false时,您不需要使用:label来处理重定向操作,您可以将这些操作放在一行或单个块中,并具有更准确的消息显示:if exist

    • 一条线:
    2>nul rmdir /q /s new\. && echo\Folder Removed || if exist "New\." (Echo\Folder not Removed)else Echo\Folder not exist
    • 在一个块中:
    @echo off 
    
    2>nul rd /q /s new\. && ( 
         echo\Folder Removed 
    )||( 
         if exist "New\." (
              echo\Folder not Removed
            ) else echo\Folder new not Exist 
        )

    • 所有可能的输出/案例:
    Folder Exist and was Removed: echo\Folder Removed
    
    Folder Exist and was not Removed: echo\Folder not Removed 
    
    Folder not Exist and was Not Removed: echo\Folder not Exist 

    Obs.: 2使用“文件夹”测试文件夹的存在并避免将其与同名(且没有扩展名)的文件混淆。


    2>nul ⇆ omit error/redirect
      rmdir /q new ⇆  remove folder command
        && ⇆ operator take action for retun 0 == do  echo\Folder Removed
          || ⇆ operator take action for retun 0 retun non 0 == do ( ... 
            if True Exist new ⇆ (
                 Folder Exists and has not been Removed⇆ if condition True 
                ) else/False Folder does not exist and because of that it was not removed ⇆ if condition False             
    

    对于您的具体情况,您可以使用:

    • 在一行中:
    2>nul rd /q new\. && echo\Folder New Empty Removed || if exist "New\." (echo\Folder not Empty and Not Removed) else echo\Folder not Exist

    • 在一个块中:
    @echo off 
    
    2>nul rd /q new\. && ( 
         echo\Folder New Empty Removed 
    )||( 
         if exist "New\." (
              echo\Folder not Empty and Not Removed
            ) else echo\Folder not Exist 
        )
    • 所有可能的输出/案例:
    Folder New Empty Exist and was Removed: echo\Folder Empty Removed
    
    Folder New Exist and is not Empty and was Not Removed: echo\Folder not Empty and Not Removed 
    
    Folder New not Exist and was Not Removed: echo\Folder not Exist 

    删除文件夹且仅当它为空且没有空子文件夹时

    @echo off 
    
    2>nul dir /b "new\."|findstr . >nul && (
         echo\Folder New non Empty and not was Removed
    )||( 
         2>nul rd /q "new\." && (
             echo\Folder new Empty Removed 
        )||(
             echo\Folder new Not Exist
           )
        )

    • 一些进一步的阅读:

      [√] RMDir | 研发

      [√] If else 条件..

      [√]条件执行||和&&

      [√]理解批处理文件中的start、2>nul、cmd等符号

    • 0

相关问题

  • 批量重命名图像文件集

  • Python 的“pass”参数的批处理等价物是什么?

  • 在 Windows 上与 Docker 守护进程通信

  • 资源管理器侧面板中的桌面外壳快捷方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve