简体   繁体   中英

Variable in variable in batch file

For school I need to write some script to execute some software (SCIA Engineer). For now I already wrote 3 scripts with python that create numorous xml files. Those files are the input for the software (SCIA Engineer). During the process, the paths of the files including their names are saved to a txt file. I would like to use that txt file with the names to execute the software (SCIA) and calculate all created xml files. 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.

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 . 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. 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. 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. I just try to delete the ".xml" and insert "Results.xml" at the end. 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. 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. Is there a way to fix that? enter image description here Thanks in advance, 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

Stephan's DELAYEDEXPANSION link

Note that executing @echo off at the start of your batch would render each of the @ unnecessary. @echo off turns off command-echoing. @thiscommandline turns off echoing of thiscommandline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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