I'm looking to use a variable which gets defined inside a for loop and use it to call a predefined variable.
For example: To start the program "Slack" I need to call the variable %Slack%.
The batch file will take your input, so If I input "Slack", the variable !filename[%%i]! will be assigned "Slack". Once that is done, I wish to call %Slack% using !filename[%%i]!. As per logic: %!filename[%%i]!%
Using %!filename[%%i]!% will make the batch file crash and close.
Setting set file=!filename[%%i]! and running it with %file% will return nothing.
Any smart trick?
@echo off
setlocal EnableDelayedExpansion
:: Predefined variables
for /f "delims== tokens=1,2" %%G in (D:\programs.txt) do set %%G=%%H
set /P "filenames=Enter programs to install: "
:: Puts input into array
set n=0
for %%a in (%filenames%) do (
set /A n+=1
set "filename[!n!]=%%~a"
)
:: Run variables imported from programs.txt
for /L %%i in (1,1,%n%) do (
:: This is where I want !filename[%%i]! to be called as %!filename[%%i]!%
)
pause
endlocal
program.txt example
Slack=C:\Users\username\AppData\Local\slack\slack.exe
Text=C:\textfile.txt
Program=C:\program.bat & C:\program_license.txt
D:\programs.txt.@echo offand check all the command echoes. Anyway,%!variable!%makes no sense unless you have got a variable that is called!variable!(including the!, which is not the case here); why is!variable!not good enough for you?!variable!only returnsSlack. With this string return I want to somehow use it to call a%Slack%variable (Defined from programs.txt at line 6).CALL echo %%!filename[%%i]!%%orCALL START "" %%!filename[%%i]!%%Forloop could look like this:For /F "UseBackDelims=" %%A In ("D:\programs.txt")Do Set "%%A"