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 RemovedFolder Exist and was not Removed: echo\Folder not RemovedFolder 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 RemovedFolder New Exist and is not Empty and was Not Removed: echo\Folder not Empty and Not RemovedFolder 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
)
)
默认情况下,rd 只会删除非空文件夹。如果一个文件夹有子文件夹,它也可以被认为是非空的。
通过暗示/s,您说:删除文件夹内的所有文件和文件夹,然后删除该文件夹。
如果你只是这样做:
它将尝试删除新文件夹。如果它找到文件和/或文件夹,它将显示消息
The folder is not empty.
/q 不会删除该消息。为了静默尝试删除文件夹但不显示错误,请使用以下命令:
该命令说:尝试以静默方式删除文件夹。屏幕上显示的任何错误都应该不存在(又名,未显示)。
如果您只删除空文件夹,而不是整个脚本,您可以
rd
在没有/s
开关(和q
)的情况下简单地使用。所以你的命令将是:这只会在目录存在且为空的情况下删除目录。否则它将返回错误消息。
PS。文件夹名称不区分大小写就可以了
rd "new\"
。聚苯乙烯。尽管在这种情况下您不需要它,但
""
如果您使用带有空格的文件夹(如"new 01"
.购买力平价。您也不需要
\
文件夹名称末尾的反斜杠 ( ),因为rd
仅适用于文件夹。但它有助于使用其他命令区分文件和文件夹。例如exist "foldername\"
将应用于文件夹exist "name"
将应用于文件和/或文件夹。您的命令
rd /q new
或仅rd new
适用于删除空文件夹。对于包含文件和/或子文件夹的文件夹,此命令不起作用。
Obs.: 1要删除包含子文件夹/递归的文件夹,请添加
/s
:1.文件夹存在且为空:
2.文件夹包含文件或子文件夹(是否为空):
3.文件夹包含文件或子文件夹(是否为空):
4.当您返回true/false时,您不需要使用
:label
来处理重定向操作,您可以将这些操作放在一行或单个块中,并具有更准确的消息显示:if exist
Obs.: 2使用“文件夹”测试文件夹的存在并避免将其与同名(且没有扩展名)的文件混淆。
对于您的具体情况,您可以使用:
删除文件夹且仅当它为空且没有空子文件夹时
一些进一步的阅读:
[√] RMDir | 研发
[√] If else 条件..
[√]条件执行
||
和&&
[√]理解批处理文件中的start、2>nul、cmd等符号