2016-09-22 50 views
1

我遇到了一个问题,想知道如何用变量填充文件,然后运行循环来打印一系列行。使用变量调用第二批次时出现问题

下面是代码:

批次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。

+1

另一个[延迟扩展](http://stackoverflow.com/a/30284028/2152082)问题。 – Stephan

+0

为什么用一个参数调用第二批文件,然后在第二批文件中不使用该参数? – Squashman

+0

Squashman,那是因为我做了很多改变才使它工作,我可能已经遗漏了很多东西。这就是为什么我粘贴这两个bat文件,以获得如何解决这个问题的想法。 –

回答

0

在BATCH1,第二批改变

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string% 

for /f "delims=" %%x in (list_of_variables.txt) do call dp2.bat %%x 

奇怪的是,你正在使用delayedexpansion,但不是在第一。 delayedexpansion允许访问变量值更改的循环内的变量的运行时值。 %var%装置和!var!“当线遇到的var原始值”是指“的var值作为它改变通过的命令的操作”

在第二批,改变每个%%1%%q(或%% 任何字母,与案例一致。)%1意味着the value of the first parameter provided to the routine并试图将其作为元变量容易发生灾难,并被认为是批处理中的不良做法。

然后可以的string值设置为使用

set "string=%1" 

注意,如果传递的字符串包含空格或其他分隔符,那么你应该报所提供的(由BATCH1)的第一个参数的值参数传递"%%x"和dequote上分配set "string=%~1"字符串(注意~

,但与你的代码,因为它代表,string将由第一批设立环境和第二批会看到string,因为它是由batch1设置的; set string=%string%是多余的。

您的问题是,我相信,试图使用%%1作为变量。

+0

可能需要在FOR变量中加引号。 'call dp2.bat“%% x”'。 – Squashman

+0

谢谢Magoo,随着你的改变,第二批一遍又一遍地复制文件的前10行,而不是搜索字符串并复制10行。 –

+0

你应该在'for'循环之前初始化变量'flag'。如果它在batch2运行前的环境中设置为1,则它将具有值“1”开始,因此将吐出前10行。初始化设置变量count也是一个想法。如果它未定义,'set/a'将假定值为零。就个人而言,对于像'flag'这样的开/关操作,我将它设置为* nothing *('set“flag =”')或* something *(除*之外的任何*)。然后,您可以使用'if defined标志......或者“如果没有定义标志......”来解释它的状态。 – Magoo

0

在你的第一个批处理文件,该行:

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string% 

被解析作为一个整体在执行之前。 %string%当时仍然是空的。 你必须使用delayed expansion

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat !string! 
+0

谢谢斯蒂芬,我做了改变,但仍然无法正常工作。由于某种原因,批次2 for循环不起作用。 –

+0

bat1给bat2一个参数。你可以在bat2中用'%1'(=第一个参数)引用它。顺便说一句。不要将数字用作'for'变量('%% 1')。虽然它起作用,但它很混乱,因为'%1'意味着“第一个参数”。 – Stephan

+0

谢谢史蒂芬,我做了修改并使用了字母。 –

相关问题