我遇到了一个问题,想知道如何用变量填充文件,然后运行循环来打印一系列行。使用变量调用第二批次时出现问题
下面是代码:
批次1:
@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:
@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
预期的结果将有点像:
|-same as string| | read form old.txt|
wordA 1111 wordb wordc word worde worf
wordA 1111 wordb wordg worh wordi worj
这里是处理:
如果我使用它们分开他们都工作得很好,但是当我试图让他们一起工作这是行不通的。与worda
设置为set string=worda
的批处理2工作就像一个魅力,所以我知道它是正确的,但是当我从批1传递变量时,它不会在output.txt文件中打印任何内容。
其他的解决方案是调用同一个批处理文件中的2个循环,但我一直无法弄清楚。
任何帮助或指导将不胜感激。
Jonathan。
另一个[延迟扩展](http://stackoverflow.com/a/30284028/2152082)问题。 – Stephan
为什么用一个参数调用第二批文件,然后在第二批文件中不使用该参数? – Squashman
Squashman,那是因为我做了很多改变才使它工作,我可能已经遗漏了很多东西。这就是为什么我粘贴这两个bat文件,以获得如何解决这个问题的想法。 –