Board logo

标题: 关于强制程序退出 [打印本页]

作者: qdcr     时间: 2006-7-7 15:14    标题: 关于强制程序退出

@echo off for /f %%i in (fileList.txt) do call :copyFile "%%i" pause :copyFile copy "%~1" E:\ if errorlevel 1 echo Copy File Failed. && goto error if errorlevel 0 echo Copy File "%~1" succeed. goto :EOF :error goto :EOF fileList.txt的内容为: E:\test\1.bmp E:\test\22.bmp(此文件不存在) E:\test\3.bmp 希望程序在遇到错误后能中止运行,而上面的代码是遇到错误后,打印错误信息并继续执行。如果在error后面用exit的话,则是关闭dos窗口,显得不太友好. 请教的问题: 如果出错,如何打印出错误信息后,返回到命令行窗口。

作者: fastslz     时间: 2006-7-7 15:39
借助TEE,详见下列帖子 www.cn-dos.net/forum ...

作者: bagpipe     时间: 2006-7-7 15:51
哦,这样啊,把:ERROR下面的GOTO :EOF更换成 EXIT /B试试............嘎嘎.........

作者: qdcr     时间: 2006-7-7 16:07
呵呵,谢谢fastslz,除了tee之外没有什么其他的解决方法了么?

作者: qdcr     时间: 2006-7-7 16:11
to bagpipe: 试过这个方法,虽然最终能返回命令行,但是for循环仍然继续执行,没有中止。

作者: fastslz     时间: 2006-7-7 16:19
我不理解goto :EOF什么意思,所以.....不好说
  1. @echo off
  2. for /f %%i in (fileList.txt) do call :copyFile "%%i"
  3. pause
  4. :copyFile
  5. copy "%~1" E:\
  6. if errorlevel 1 echo Copy File Failed. && goto error
  7. if errorlevel 0 echo Copy File "%~1" succeed.
  8. goto end
  9. :error
  10. rem 显示错误信息
  11. goto end
  12. :end
DOS一跟葱:发表于 2006-07-07 16:16 [ Last edited by fastslz on 2006-7-7 at 16:24 ]

作者: qdcr     时间: 2006-7-7 16:36
to fastslz: 谢谢你的回复。 是我在上面的帖子中没说清楚,这里澄清一下。 当出现copy出错时,希望不再执行后面的动作,也就是退出for 循环,一直没有找到好的方法.

作者: fastslz     时间: 2006-7-7 18:25
据我所知for肯定是循环,就不能单独列出来后COPY吗?bagpipe高手都没法我更不行。或者把你的要求和要达到的目的说一下,看你的批处理越看越没头绪,毕竟每个人的思路都不一样。 另外fileList.txt的内容是固定的吗?

作者: qdcr     时间: 2006-7-7 19:39
fileList.txt中的内容不是固定的,我所要实现的功能是批处理拷贝某文件中记录的一系列文件,过程中有一个文件拷贝出错,文件拷贝过程将中止,程序退出。 谢谢 [ Last edited by qdcr on 2006-7-7 at 19:42 ]

作者: kcdsw     时间: 2006-7-7 20:01
你干吗还在 error的标号后 执行goto :eof 这本身就被包含在for的循环中了 你当然跳不出来
@echo off
for /f %%i in (fileList.txt) do call :copyFile "%%i"

:endfor
pause >nul
exit

:copyFile
copy "%~1" E:\
if errorlevel 0 (echo Copy File "%~1" succeed.) else (echo Copy FileFailed.)&& goto endfor
goto :EOF
这样就跳出下一个循环了 你可以在endfor后继续执行其他命令了 [ Last edited by kcdsw on 2006-7-7 at 20:08 ]

作者: fastslz     时间: 2006-7-7 21:51
10楼kcdsw的改成if errorlevel 1 从for循环中跳出来了,强......
@echo off
for /f %%i in (fileList.txt) do call :copyFile "%%i"

:endfor
pause >nul
exit

:copyFile
copy "%~1" E:\
if errorlevel 1 (echo Copy File "%~1" succeed.) else (echo Copy FileFailed.)&& goto endfor
goto :EOF

作者: qdcr     时间: 2006-7-10 13:19
这样的退出是关闭整个cmd窗口了,不太友好,希望能够返回cmd窗口而不是关闭。

作者: electronixtar     时间: 2006-7-10 16:55
要返回就运行cmd

作者: bagpipe     时间: 2006-7-10 17:17
真TMD不好伺候,,,EST我们就别管了

作者: voiL     时间: 2006-7-27 04:32
Originally posted by qdcr at 2006-7-10 13:19: 这样的退出是关闭整个cmd窗口了,不太友好,希望能够返回cmd窗口而不是关闭。
要想调用BAT文件本身,在开头先SET批处理自身的路径
@echo off
cls
set mypath=%0
注:cls有助于清除上次运行的信息,当然,如果你是有意让它显示的话可以删除掉. 然后在最后可以在error后面加入
%mypath% & exit
来实现. 再注:%mypath%后面的exit一定要加上,不然很可能会导致程序死循环.

作者: willsort     时间: 2006-7-29 15:46
Re qdcr: 你的问题可以通过在 :error 设置中断变量来解决,如[1]; 也可以考虑放弃 call :sub 的设计思路,而使用for 的语句块,如[2]; 另外,你的代码结构存在一些问题,在 pause 语句下应该有一句goto :eof,否则:copyFile会在for正常结束后再次被执行,导致程序流程的错误。 [1]
@echo off & setlocal EnableDelayedExpansion
for /f %%i in (fileList.txt) do if "!break!"=="" call :copyFile "%%i"
pause
goto :eof

:copyFile
copy "%~1" E:\
if errorlevel 1 goto Error
echo Copy File "%~1" succeed.
goto :EOF

:error
echo Copy File Failed. Program is break.
set break=true
goto :EOF
[2]
@echo off
for /f %%i in (fileList.txt) do (
    copy "%%i" E:\ >nul
    if errorlevel 1 echo Copy File Failed. Program is break. & pause & goto :eof
    echo Copy File "%%i" succeed.
)
pause

作者: junchen2     时间: 2007-8-15 08:49
又学到东西了