如何在德尔故障停止批处理脚本 [英] How to stop batch script on del failure
问题描述
我们有一个批处理脚本删除文件(删除
)和目录( RD
)。有谁知道如何停止的脚本(失败)如果执行这些DELETE语句的失败?如果一个文件/目录由Windows锁定他们可能会失败。谢谢你。
We have a batch script that removes files (del
) and directories (rd
). Does anyone know how to halt (fail) execution of the script if any of these delete statements fail? They could fail if a file/directory is locked by Windows. Thanks.
更新
我使用的语句:
德尔:德尔* * / S / Q
RD:在FOR / D %% G(*)DO RD / S / Q %%摹
推荐答案
有关删除的文件,你可以先尝试仁
(重命名)的文件。仁
将如果文件被锁定ERRORLEVEL设置为1。加引号的文件名。
For deleting the files, you can first try to ren
(rename) the file.
ren
will set ERRORLEVEL to 1 if the file is locked. Add quotes around filename.
@echo OFF
:: Delete all files, but exit if a file is locked.
for %%F in (*.*) do (
@echo Deleting %%F
ren "%%F" tmp 2> nul
if ERRORLEVEL 1 (
@echo Cannot delete %%F, it is locked.
exit /b 1
)
del tmp
)
我怀疑你可能能够做同样的事情的目录,但我似乎无法弄清楚如何获得锁定,所以我可以测试的目录。以下可能工作:
I suspect you may be able to do the same thing for directories, but I can't seem to figure out how to get a directory locked so I can test. The following may work:
:: Remove all directories, but exit if one is locked.
FOR /D %%G in (*) DO (
@echo Removing %%G
ren "%%G" tmpdir 2> nul
if ERRORLEVEL 1 (
@echo Cannot remove %%G, it is locked
exit /b 1
)
RD /s /q tmpdir
)
这篇关于如何在德尔故障停止批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!