简体   繁体   English

批处理文件中的变量

[英]Variable in variable in batch file

For school I need to write some script to execute some software (SCIA Engineer).对于学校,我需要编写一些脚本来执行一些软件(SCIA 工程师)。 For now I already wrote 3 scripts with python that create numorous xml files.现在我已经用 python 编写了 3 个脚本,它们创建了大量的 xml 文件。 Those files are the input for the software (SCIA Engineer).这些文件是软件(SCIA 工程师)的输入。 During the process, the paths of the files including their names are saved to a txt file.在此过程中,文件的路径(包括它们的名称)将保存到一个 txt 文件中。 I would like to use that txt file with the names to execute the software (SCIA) and calculate all created xml files.我想使用带有名称的 txt 文件来执行软件 (SCIA) 并计算所有创建的 xml 文件。 At the moment I am able to execute the program and calculate the files one by one by altering the path of the files in the batch file.目前,我可以通过更改批处理文件中文件的路径来执行程序并一个一个地计算文件。 But the goal is to automate it so all files are calculated with one batch file.但目标是使其自动化,以便所有文件都使用一个批处理文件进行计算。

I found a small script to read the txt file and to save every line to a different variable.我找到了一个小脚本来读取 txt 文件并将每一行保存到不同的变量。

set count=0
for /f "tokens=*" %%x in (LijstBestanden.txt) do (
    set /a count+=1
    set var[!count!]=%%x
    echo.Count--!count!--
)

I know the code saves for example the first line of the txt file as var 1 .我知道代码将 txt 文件的第一行保存为 var 1 Now I wanted to use a while loop but I found that it is not possible and it can be done by using an if statement and jumping back to just before the if with goto.现在我想使用 while 循环,但我发现这是不可能的,可以通过使用 if 语句并使用 goto 跳回到 if 之前来完成。 So I tried doing this.所以我试着这样做。

:while
set i=0
if i leq count (
    set /a i+=1
    @SET INPUTXML=%var[i]%
    @SET OUTPUTXML=%var[i]~0,-4%Results.xml

    @Call :Calculate LIN FIle1 %INPUTXML% %OUTPUTXML%
    goto :while
)

So I try to calculate every file but the first problem I get is that %var[i]% isn't correct.所以我尝试计算每个文件,但我遇到的第一个问题是 %var[i]% 不正确。 I try to use the path of the input file and alter it to use it as the path for the output file with %var[i]~0,-4%Results.xml.我尝试使用输入文件的路径并将其更改为将其用作带有 %var[i]~0,-4%Results.xml 的 output 文件的路径。 I just try to delete the ".xml" and insert "Results.xml" at the end.我只是尝试删除“.xml”并在末尾插入“Results.xml”。 I don't know if this is the correct way to do it.我不知道这是否是正确的方法。

Can Anybody help me with that, especially the problem of %var[i]% because I don't know how to get it to work.谁能帮我解决这个问题,尤其是 %var[i]% 的问题,因为我不知道如何让它工作。 There may be other faults in my code, sorry for that, I am not an expert with coding.我的代码中可能还有其他错误,抱歉,我不是编码专家。

Response to the answer: As displayed in cmd argument 3 works fine which is INPUTXML but argument4 does not display the path, only Results.xml.对答案的回应:如 cmd 中所示,参数 3 工作正常,它是 INPUTXML,但参数 4 不显示路径,仅显示 Results.xml。 Is there a way to fix that?有办法解决这个问题吗? enter image description here Thanks in advance, Niels在此输入图片描述提前致谢,Niels

for /L %%i in (1,1,%count%) do (
    @SET INPUTXML=!var[%%i]!
    @SET OUTPUTXML=!var[%%i]:~0,-4!Results.xml

    @Call :Calculate LIN FIle1 !INPUTXML! !OUTPUTXML!
)

You need the contents of the variables var , but need to use delayedexpansion您需要变量var内容,但需要使用delayedexpansion

Stephan's DELAYEDEXPANSION link Stephan 的 DELAYEDEXPANSION 链接

Note that executing @echo off at the start of your batch would render each of the @ unnecessary.请注意,在批处理开始时执行@echo off会使每个@都变得不必要。 @echo off turns off command-echoing. @echo off关闭命令回显。 @thiscommandline turns off echoing of thiscommandline. @thiscommandline关闭此命令行的回显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM