4

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

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

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

我希望现在很清楚。

提前致谢。

4

8 回答 8

3

像这样?

for /f "delims=" %%f in ('dir /b /s Example') do (
  command
  if not errorlevel 1 (
    command-for-success
  ) else (
    command-for-error
  )
)
于 2009-12-27T18:42:40.473 回答
2

创建两个命令文件并运行 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

经测试:

\example
|   file1
|   file2
|   file3
|   file4
|   file5
|
\---example
        file1
        file2
        file3
        file4
        file5
于 2010-01-04T03:10:30.410 回答
1

当一个使用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"
    )
 ) 
于 2010-01-04T07:02:25.713 回答
0

我相信这就是你想要的

for /f "delims=" %%f in ('dir /b /s Example') do (
  command
  if not errorlevel 1 (
    command
  ) else (
    command
    goto :eof
  )
)
于 2010-01-03T18:17:01.847 回答
0

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

于 2010-01-04T03:22:41.273 回答
0

试试这个批处理文件:

@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
)

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

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

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

于 2010-01-06T19:20:40.003 回答
0

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

@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

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

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

运行完整个循环后,我们只需跳到:end标记处,稍微清理一下就完成了。

于 2010-01-07T11:55:32.140 回答
0

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

for /r %i in (c:\dirname\*.ext) 执行命令“%i”

Commandstuff.bat看起来像这样:

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

:END
Echo end

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

于 2010-01-07T12:12:26.900 回答