ファイルに変数を設定し、一連の行を印刷するためのループを実行する方法がわかりません。変数で2番目のバッチを呼び出す際に問題が発生しました
ここはコードです:
バッチ1:
@echo off
cls
:: This batch is supposed to get the variable %string% and look in a different file (old.txt) and copy a block of 10 lines below the matching string.
setlocal enabledelayedexpansion
set string=%string%
for /f "tokens=*" %%1 in (OLD.txt) do (
if !flag! equ 1 (
echo !string! %%1 >> output.txt
set /a count+=1
if !count! gtr 10 goto endit
)
if /i "%%1" equ "!string!" (set flag=1)
)
echo "%string%" not found check spellings and input file.
exit /b
:endit
type output.txt
意図した結果は以下となります。
@echo off
:: This batch read a file and copy all lines containing that word into a new
file in an ordered list. (This works just fine)
findstr /C:"wordA" OLD.txt >> list_of_variables.txt
for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string%
結果は、この
wordA 1111 wordb
wordA 1112 wordb
wordA 1113 wordb
wordA 555 wordb
バッチ2のようなものですのようなものになる:ここでは
|-same as string| | read form old.txt|
wordA 1111 wordb wordc word worde worf
wordA 1111 wordb wordg worh wordi worj
は契約です:
私はそれらを使用する場合は、別途、それらの両方が正常に動作しますが、私は彼らが一緒に動作させるためにしようとすると、それは動作しません。 worda
のバッチ2はset string=worda
と同じように動作しますので、正しいですが、バッチ1の変数を渡すと、output.txtファイルに何も印刷されません。
他の解決策は、同じバッチファイル内の2つのループを呼び出すことですが、私はそれを理解することができませんでした。
ご迷惑をおかけして申し訳ございません。
ジョナサン。あなたの最初のバッチファイルで
別の[遅延拡張](http://stackoverflow.com/a/30284028/2152082)の問題。 – Stephan
なぜ2番目のバッチファイルで引数を指定し、2番目のバッチファイルでその引数を使用しないのですか? – Squashman
Squashman、それは私がそれを働かせるために多くの変更を加えたので、私は多くのものを捨てたかもしれないからです。そのため、これを修正する方法のアイデアを得るために、両方のバットファイルを貼り付けました。 –