windows 发生错误时跳过

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1966671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:39:15  来源:igfitidea点击:

Skip when error occurs

windowsbatch-filecmdskip

提问by Deniz Zoeteman

i have the following code in batch (cmd):

我有以下批处理代码(cmd):

for /f "delims=" %%f in ('dir /b /s Example') do (
command
if %errorlevel%==1 (
command
SKIP
)
command
)

EDIT:To make things more clear: for /f...searches for a directory called 'Example' and loops to search for more directories than one. the first commandis a delete command, it deletes all files in the directory. the commandthat happens when an error occurs, is a echocommand which writes some info about the error to a text file. now the hole skipthing; sometimes, the files can't be deleted because of access deniedor this file is in use by.... Normally, what would happen if there weren't a skip thing, it would just stop the command and hang. So, what i want to do, is prevent this from happening. Alternatively, i want to use something like skip, so it can skip the file and continue anyways. So i think this command needs to be piped in the deletecommand.

编辑:为了使事情更清楚: for /f...搜索名为“示例”的目录并循环搜索多个目录。第一个command是删除命令,它删除目录中的所有文件。在command发生错误时出现这种情况,是echo该写的错误到一个文本文件的一些信息的命令。现在是洞的skip事情;有时,由于access denied或无法删除文件this file is in use by...。通常情况下,如果没有跳过的事情会发生什么,它只会停止命令并挂起。所以,我想做的是防止这种情况发生。或者,我想使用类似的东西skip,所以它可以跳过文件并继续。所以我认为这个命令需要在delete命令中进行管道传输。

I hope it's clear now.

我希望现在清楚了。

Thanks in advance.

提前致谢。

采纳答案by atzz

Like this?

像这样?

for /f "delims=" %%f in ('dir /b /s Example') do (
  command
  if not errorlevel 1 (
    command-for-success
  ) else (
    command-for-error
  )
)

回答by fupsduck

Create the two command files and run delex.cmd. The files and directories that are not deleted will be logged to delex.txt. The ones that hang, will have a minimized cmd window open that gets killed after a delay by using ping (thanks to Doc Brown's suggestion).

创建两个命令文件并运行 delex.cmd。未被删除的文件和目录将被记录到 delex.txt。挂起的那些将打开一个最小化的 cmd 窗口,该窗口在使用 ping 延迟后被杀死(感谢 Doc Brown 的建议)。

delex2.cmd
----------
@echo off
del /q %1 
if exist %1 echo %1 not deleted!>>delex.txt 
exit 

delex.cmd
---------
@echo off
if exist delex.txt del delex.txt 
for /f "delims=" %%f in ('dir /s /b example') do start "delextaskkill" /min delex2.cmd "%%f"
ping 127.0.0.1 -n 3 -w 1000> nul
taskkill /fi "Windowtitle eq delextaskkill"> nul

Tested with:

测试:

\example
|   file1
|   file2
|   file3
|   file4
|   file5
|
\---example
        file1
        file2
        file3
        file4
        file5

回答by Doc Brown

When one uses del, and "access denied" or "this file is in use by..." occurs, %errorlevel%is 0, so testing %errolevel%is useless. Perhaps the following simple solution works in your case:

当使用del, 并且出现“拒绝访问”或“此文件正在被...使用”时,%errorlevel%为 0,因此测试%errolevel%无用。也许以下简单的解决方案适用于您的情况:

for /f "delims=" %%f in ('dir /b /s Example') do (
    del %%f
    if exist %%f (
         echo "file not deleted"
    ) else (
         echo "file deleted"
    )
 ) 

回答by bta

Try this batch file:

试试这个批处理文件:

@echo off
REM For each directory named 'Example' ...
for /f "delims=" %%f in ('dir /b /s Example') do (
    REM .. enter the directory and delete all the files found there.
    pushd .
    cd %%f
    del /q *
    for /f "delims=" %%z in ('dir /b') do (
        REM Any files that still exist must have been inaccessable.  Log an error.
        echo Unable to delete file %%z > C:\logfile.txt
    )
    popd
)

Tested with the following directory structure:

使用以下目录结构进行测试:

folder\
|------Example\
|      |-------file1 (write-protected)
|      |-------file2
|      |-------file3
|------deeper\
       |-------Example\
               |-------file4
               |-------file5 (write-protected)
               |-------file6

After running the batch file, only file1and file5were left. An "Access is denied" message was printed for each write-protected file encountered; if that gets annoying you re-direct the output of the batch file like script.bat > NULto hide it.

运行批处理文件后,只剩下file1file5了。对于遇到的每个写保护文件,都会打印一条“访问被拒绝”消息;如果这很烦人,您可以重定向批处理文件的输出,例如script.bat > NUL隐藏它。

回答by Oliver

So after all the existing answers didn't satisfy you and you absolutely need a skip within your batch file, i will make another try:

因此,在所有现有答案都没有让您满意并且您绝对需要在批处理文件中跳过之后,我将再试一次:

@echo off
setlocal

for /f "delims=" %%f in ('dir /b /s batch') do call :DoSomething %%f
goto end

:DoSomething
echo Here i am: %1
if %errorlevel%==1 goto :eof

echo No error occured
goto :eof

:end
endlocal

The trick is, that the for loop calls a sub function within the same file and gives the needed parameter to it. This new call runs in a new context and can only access the variables which are defined after the do call :DoSomethingin the given order their.

诀窍是,for 循环调用同一文件中的子函数并为其提供所需的参数。这个新调用在一个新的上下文中运行,并且只能访问do call :DoSomething在给定顺序之后定义的变量。

So you have to access the variables here with %1, %2, etc. If you want to leave this context you have to make a goto :eofto jump to the end of the file (this marker is predefined in batch mode and should not occur within your file), what leaves the context and returns to the for loop.

所以你必须用%1,%2等访问这里的变量。如果你想离开这个上下文,你必须goto :eof跳到文件的末尾(这个标记是在批处理模式下预定义的,不应该出现在你的文件中),什么离开上下文并返回到for循环。

After running through the whole loop we just jump to the :endmarker, make a little clean up and are finished.

运行完整个循环后,我们只需跳到:end标记处,稍作清理即可完成。

回答by Ameer Deen

The following looks for the file extentions you want recursively under the "dirname" directory tree and executes commandstuff.bat against that file name:

以下内容在“dirname”目录树下递归查找您想要的文件扩展名,并针对该文件名执行commandstuff.bat:

for /r %i in (c:\dirname\*.ext) do commandstuff "%i"

for /r %i in (c:\dirname\*.ext) do commandstuff "%i"

Commandstuff.batlooks like this:

Commandstuff.bat看起来像这样:

@ECHO OFF
del %1
IF (%ERRORLEVEL% == 0) goto END
:ERROR
Echo Error deleting %1

:END
Echo end

@ECHO OFF
del %1
IF (%ERRORLEVEL% == 0) goto END
:ERROR
Echo Error 删除 %1

:END
Echo end

This would run commandstuff.bat for you to delete the files you want. When there is an error it will simply echo the file details and continue processing the next file.

这将为您运行 commandstuff.bat 以删除您想要的文件。当出现错误时,它只会回显文件详细信息并继续处理下一个文件。

回答by Goyuix

Perhaps this is more advanced than can be accomplished using just built-in cmd processing. Why not consider using the Windows Scripting Host (vbscript/jscript) or even PowerShell? Both will likely provide you the level of control you are requesting.

也许这比仅使用内置 cmd 处理所能完成的更高级。为什么不考虑使用 Windows Scripting Host (vbscript/jscript) 甚至 PowerShell?两者都可能为您提供所需的控制级别。

回答by PA.

I believe that this is what you want

我相信这就是你想要的

for /f "delims=" %%f in ('dir /b /s Example') do (
  command
  if not errorlevel 1 (
    command
  ) else (
    command
    goto :eof
  )
)