2011-08-09 16 views
1

ファイルに2番目の行(:: echo1、:: echo2、:: echo3)の文字列があるかどうかを判断するコードがあります。しかし、私がサブルーチンで何らかの理由でコードを実行すると:_verify IFコマンド内のIFコマンドで、変数%chk%を増加させてIFのelseセクションを完全にスキップし、_verifyに戻ります。 of :: echoがファイル内で探していて、同じファイルを再度検索します(すべて3つすべてのファイルを検索する必要があります)。 IFコマンドをIF NOTと逆にして、他のものをフロントとコールに切り替えることを試みました:_redeemは最後に同じエラーが発生しました。 (注:これは完了しました::: echo1を含むファイルに対して_verifyを正しく行いました)。それは%chk%を増加させずに:_verifyにもう一度行くので、:: echo1について各ファイルをチェックするだけです。代わりに、goto:eofに直接行き、次のものに戻ります。もう一度、別のファイルを見つけるために、:: echo1だけを処理します。スクリプトを説明するためのコメントを追加しました。文字列のファイルを処理するWindowsバッチ

set gt1=1 
setLocal EnableDelayedExpansion 
::Identifies all files meeting the criteria (name being tmp*.tmp) and sets cap%gt1% equal the filename. Also checks to see if there are no files left (if the filename doesn't exist (i.e. it's blank because there are none left)). 
:identify 
set chk=1 
if %gt1%==4 goto :restore 
for %%A in (tmp*.tmp) do (set cap%gt1%=%%A) & call :_verify 
if not exist !cap%gt1%! goto :error 
goto :identify 

:_verify 
::Verifies that the specific file it's looking at (set as cap%gt1% in :identify) has the string ::echo1, 2 or 3 as the second line. 
if %chk%==4 call :_reserve & goto :eof 
for /f "skip=1" %%B in (!cap%gt1%!) do if %%B==::echo%chk% (call :_redeem) else (set /a chk=%chk%+1) & (goto :_verify) 
goto :eof 

:_redeem 
::Renames files that are confirmed to have the string to their string name (minus the ::). 
ren !cap%gt1%! echo%chk%.tmp 
set /a gt1=%gt1%+1 
goto :eof 

:_reserve 
::Subroutine used to temporairly discard files that do not meet the requirements so they will not be processed again in :identify during loopback. 
if not exist temp50 mkdir temp50 
move !cap%gt1%! temp50 
goto :eof 

:restore 
::Restores files that were put in :_reserve to their previous location. 
if exist %~dp0\temp50 cd temp50 & for %%C in (tmp*.tmp) do move %%C %~dp0 & cd .. & rmdir temp50 
pause 

:error 
::Error in case it can't find all three files containing the strings. 
echo Unable to find program files. Please reinstall. 
echo. 
pause 
quit 

答えて

1

代わりに、それは後藤に直接行く:あなたがこれを行うには、あなたのコードを書くよう

EOFこれは、ケースです。
あなたは

if %chk%==4 (call :_reserve & goto :eof) 

を書きたいしかし、あなたのコードは、あなたが良く、&セパレーターを使用しないよう括弧で複数の行を使用する必要があります

if %chk%==4 call :_reserve 
goto :eof 

として動作します。

for %%A in (tmp*.tmp) do (
    set cap%gt1%=%%A 
    call :_verify 
) 
.... 
if %chk%==4 (
    call :_reserve 
    goto :eof 
) 
+0

ハハハ、ありがとう、ジェブ。午前4時まで待っていれば返事をすることができました。 :P –

関連する問題