2008年10月30日 星期四 10时08分
当然批处理程序中没有break关键字,那又如何实现呢?先看看例子:
- @echo off
- set AreaPortFile=Ports_A
- set AnchorOnLandPortFile=OL%AreaPortFile%_Anchor
- set MergeOnLandPortFile=MG%AreaPortFile%
- if exist %MergeOnLandPortFile% del %MergeOnLandPortFile%
- rem set find=0 // 方案二
- for /F "eol=; tokens=1,2,3,4 delims=|" %%a in (%AreaPortFile%) do (
- if "%%d"=="?" (
- rem set find = 0
- rem for /F "eol=; tokens=1,2 delims=|" %%i in (%AnchorOnLandPortFile%) do (
- rem if "%%i"=="A%%a" (
- rem echo %%a^|%%b^|%%j>>%MergeOnLandPortFile%
- rem set find=1
- rem
- rem goto _break // 方案一
- rem )
- rem )
- rem if "%find%"=="0" echo %%a^|%%b^|%%c^|%%d>>%MergeOnLandPortFile%
- rem :_break
- rem
- rem 方案三
- call CorrectBy "%%a" "%%b" "%%c" %%d
- ) else (
- echo %%a^|%%b^|%%c>>%MergeOnLandPortFile%
- )
- )
实现的功能是,根据AnchorOnLandPortFile修正AreaPortFile的内容(需要修正的行末尾是"?"),输出到MergeOnLandPortFile。
方案一、在for外层放置一个标签:_break,然后在for里面goto _break,实现。但是不行,在for里面(不管哪层)一旦用goto就会结束循环。
方案二、采用一个变量标识,其实这种方法并没有break,遍历了所有,只是标记一下是否找到。还是不行,为啥呢?这和之前发过的文章中提到过,在for中set一个变量,这个变量的值不会改变,一直是最后一个值,上面的find==1。
方案三、其实这个问题和当想在for中set变量时的处理方法一致,就是用call调用另外的程序,如:
- for %%z in (%1) do set a=%%~z
- for %%z in (%2) do set b=%%~z
- for %%z in (%3) do set c=%%~z
- set d=%4
- echo %a% %b% %c% %d%
- for /F "eol=; tokens=1,2 delims=|" %%i in (%AnchorOnLandPortFile%) do (
- if "%%i"=="A%%a" (
- echo %a%^|%b%^|%%j>>%MergeOnLandPortFile%
- goto _break
- )
- )
- echo %a%^|%b%^|%c%^|%d%>>%MergeOnLandPortFile%
- :_break
OK,方案三成功了。
在CorrectBy传参数时遇到一个问题,就是参数中含有特殊符号:",",这个时候需要将参数用双引号括起来,但是取参数时需要去掉双引号,我的处理方法是:
- for %%z in (%3) do set c=%%~z
应该还有其他方法吧!?