Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用变量调用第二批时遇到问题_String_Windows_Variables_Batch File_Cmd - Fatal编程技术网

String 使用变量调用第二批时遇到问题

String 使用变量调用第二批时遇到问题,string,windows,variables,batch-file,cmd,String,Windows,Variables,Batch File,Cmd,我遇到了一个问题:如何用变量填充文件,然后运行一个循环来打印一系列行 代码如下: 第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 "

我遇到了一个问题:如何用变量填充文件,然后运行一个循环来打印一系列行

代码如下:

第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
交易如下:

如果我单独使用它们,它们都可以很好地工作,但是当我试图让它们一起工作时,它就不起作用了。批处理2将
worda
设置为
set string=worda
就像一个符咒,因此我知道它是正确的,但当我从批处理1传递变量时,它不会在output.txt文件中打印任何内容

另一个解决方案是在同一批处理文件中调用这两个循环,但我还没有弄清楚

我们将非常感谢您的任何帮助或指导


乔纳森。

在您的第一个批处理文件中,行:

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string%
在执行之前将其作为一个整体进行分析<代码>%string%此时仍为空。 您必须使用:


在第一个批处理文件中,行:

for /f "delims=" %%x in (list_of_variables.txt) do set string=%%x & call dp2.bat %string%
在执行之前将其作为一个整体进行分析<代码>%string%此时仍为空。 您必须使用:

在第1批中,更改

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

奇怪的是,您在第二批中使用了
delayedexpansion
,但在第一批中没有使用
delayedexpansion
允许在变量值发生变化的循环中访问变量的运行时值<代码>%var%表示“遇到行时
var
的原始值”和
!瓦尔表示“通过命令操作而改变的
var
值”

在第二批中,将每个
%%1
更改为
%%q
(或%%any字母,与大小写一致)。
%1
表示
提供给例程的第一个参数的值
,尝试将其用作元变量容易发生灾难,在batchworld中被认为是不好的做法

然后,您可以使用以下命令将
string
的值设置为(由batch1)提供的第一个参数的值

请注意,如果正在传递的字符串包含空格或其他分隔符,则应引用正在传递的参数
“%%x”
,并在赋值时取消引用字符串
set“string=%~1”
(请注意
~

但是,如果您按原样编码,
string
将由第一批在环境中设置,第二批将看到由batch1设置的
string
设置字符串=%string%
是多余的

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

在batch1中,更改

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

奇怪的是,您在第二批中使用了
delayedexpansion
,但在第一批中没有使用
delayedexpansion
允许在变量值发生变化的循环中访问变量的运行时值<代码>%var%
表示“遇到行时
var
的原始值”和
!瓦尔表示“通过命令操作而改变的
var
值”

在第二批中,将每个
%%1
更改为
%%q
(或%%any字母,与大小写一致)。
%1
表示
提供给例程的第一个参数的值
,尝试将其用作元变量容易发生灾难,在batchworld中被认为是不好的做法

然后,您可以使用以下命令将
string
的值设置为(由batch1)提供的第一个参数的值

请注意,如果正在传递的字符串包含空格或其他分隔符,则应引用正在传递的参数
“%%x”
,并在赋值时取消引用字符串
set“string=%~1”
(请注意
~

但是,如果您按原样编码,
string
将由第一批在环境中设置,第二批将看到由batch1设置的
string
设置字符串=%string%
是多余的


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

另一个问题。为什么您调用带有参数的第二批处理文件,然后在第二批处理文件中不使用该参数?Squashman,这是因为我做了许多更改以使其工作,并且可能遗漏了很多内容。这就是为什么我粘贴了两个bat文件来获得解决方法的想法。另一个问题。为什么你用一个参数调用第二批文件,然后在你的第二批文件中不使用该参数?Squashman,那是因为我做了很多更改使其工作,我可能遗漏了很多东西。这就是为什么我粘贴了两个bat文件,以获得关于如何修复此问题的想法。谢谢Stephan,我做了更改,但仍然不起作用。出于某种原因,batch 2 for循环没有启动。bat1为bat2提供了一个参数。您可以使用
%1
(=第一个参数)在bat2中引用它。顺便说一句,不要将数字用作变量(
%%1
)的
。虽然它可以工作,但它令人困惑,因为
%1
表示“第一个参数”。谢谢Stephan,我做了更改并使用了字母。谢谢Stephan,我做了更改,但仍然不起作用。出于某种原因,batch 2 for循环没有启动。bat1为bat2提供了一个参数。您可以使用
%1
(=第一个参数)在bat2中引用它。顺便说一句,不要将数字用作变量(
%%1
)的
。虽然它可以工作,但它令人困惑,因为
%1
表示“第一个参数”。谢谢Stephan,我做了更改并使用了字母。可能必须在FOR变量周围加引号<代码>调用dp2.bat“%%x”
。谢谢你,Magoo,随着你的更改,现在第二批会一遍又一遍地复制文件的前10行,而不是搜索字符串并向下复制10行。你应该在
之前初始化变量
标志
set "string=%1"