.bat 处理错误码
2016-01-27 16:34
309 查看
某些命令只会返回error level,而要添加互操作性,.bat文件的返回值,exit code应该最后指定,那么需要,在这个单行的命令后面单个添加处理error level
的语句,最后再做统一的抛出处理,一个robo copy,批量拷贝,很实用的例子,可以作为参考,以下命令通过测试
robo copy命令的说明可以见网址:http://ss64.com/nt/robocopy-exit.html
a sample call in c# with exitcode handling
的语句,最后再做统一的抛出处理,一个robo copy,批量拷贝,很实用的例子,可以作为参考,以下命令通过测试
@echo off ROBOCOPY c:\a c:\d /MIR /LOG:c:\d\log1.txt if %ERRORLEVEL% GEQ 8 echo error encountered errorlevel:%errorlevel% & goto throw ROBOCOPY c:\b c:\c /MIR /LOG:c:\c\log2.txt :throw if %ERRORLEVEL% GEQ 16 echo ***FATAL ERROR*** & goto end if %ERRORLEVEL% EQU 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto end if %ERRORLEVEL% EQU 14 echo FAIL + MISMATCHES + XTRA & goto end if %ERRORLEVEL% EQU 13 echo OKCOPY + FAIL + MISMATCHES & goto end if %ERRORLEVEL% EQU 12 echo FAIL + MISMATCHES& goto end if %ERRORLEVEL% EQU 11 echo OKCOPY + FAIL + XTRA & goto end if %ERRORLEVEL% EQU 10 echo FAIL + XTRA & goto end if %ERRORLEVEL% EQU 9 echo OKCOPY + FAIL & goto end if %ERRORLEVEL% EQU 8 echo FAIL & goto end if %ERRORLEVEL% EQU 7 echo OKCOPY + MISMATCHES + XTRA & goto end if %ERRORLEVEL% EQU 6 echo MISMATCHES + XTRA & goto end if %ERRORLEVEL% EQU 5 echo OKCOPY + MISMATCHES & goto end if %ERRORLEVEL% EQU 4 echo MISMATCHES & goto end if %ERRORLEVEL% EQU 3 echo OKCOPY + XTRA & goto end if %ERRORLEVEL% EQU 2 echo XTRA & goto end if %ERRORLEVEL% EQU 1 echo OKCOPY & goto end if %ERRORLEVEL% EQU 0 echo No Change & goto end :end if %ERRORLEVEL% GEQ 8 exit %ErrorLevel% exit 0
robo copy命令的说明可以见网址:http://ss64.com/nt/robocopy-exit.html
a sample call in c# with exitcode handling
void Main() { Process processer = new Process(); processer.StartInfo.FileName = @"c:\a\testrobocopy.bat"; processer.StartInfo.Arguments = null; processer.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; processer.StartInfo.RedirectStandardOutput = true; processer.StartInfo.RedirectStandardError = true; processer.StartInfo.UseShellExecute = false; processer.StartInfo.CreateNoWindow = true; processer.EnableRaisingEvents = true; processer.OutputDataReceived += new DataReceivedEventHandler(OutputDataReceived); processer.ErrorDataReceived += new DataReceivedEventHandler(ErrorDataReceived); processer.Start(); processer.BeginOutputReadLine(); processer.BeginErrorReadLine(); processer.WaitForExit(); Console.WriteLine (processer.ExitCode); //Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error()); } private void ErrorDataReceived(object sendingProcess, DataReceivedEventArgs outLine) { if (!String.IsNullOrEmpty(outLine.Data)) { Console.WriteLine ("error:"+outLine.Data); } } private void OutputDataReceived(object sendingProcess, DataReceivedEventArgs outLine) { if (!String.IsNullOrEmpty(outLine.Data)) { Console.WriteLine ("output:"+outLine.Data); } } // Define other methods and classes here //call kernel32.dll method to invoke some process to complete our job and check results of the processor created jobs
相关文章推荐
- K3CLOUD Linux+Oracle部署
- kafka本地存储4-LogCleaner
- C#窗口传值及方法调用1--通过静态方法实现
- 执行mount命令时遇到的问题
- tableViewCell的选中状态的还原
- HDOJ 2053 Switch Game
- 从框架到完整项目搭建,实战项目《约个球》(7)- 将项目迁移到android studio,使用gradle搭建android项目(上)
- Centos6.x系统下安装telnet命令及使用
- netty 5 (2)解码器
- mysql压缩包如何使用及PoolableConnectionFactory 和Access denied for user 'testdb'@'localhost'问题的解决
- 数字图像处理 调整对比度的几种方法
- HDOJ 2053 Switch Game
- linux下查看程序占用多少内存
- java 读取文件的常用方式
- ---------快排-----表排-----基数排序(桶排序)-----
- 【Unity】 相机移动脚本
- Linux 环境下搭建DDNS
- kafka本地存储3-LogSegment
- CSS icon图标之纯CSS实现带动画效果的天气图标
- mysql按字段分组并获取每个分组按照某个字段排序的前三条