0

I'm making a file that first uses PowerShell to decode a JSON file, and then uses Batch to read out the results. I want to put them into variables. Is there a way to define the name of a variable using the text from another variable, like this?

SET name=test
SET %name%=helloworld
ECHO %test%

And if it's not possible, are there some sort of associative arrays in Batch?

Here's my JSON file:

{ "test" : "helloworld" }

Thanks in advance!

  • 3
    Why don't you try it and see what happens? Batch doesn't really do arrays, but if you search here at SO you'll find lots of examples of how to kind of fake them. I stick with Powershell for what you're trying to do. – jwdonahue May 11 '20 at 18:20
  • I think you're looking for [`Set-Variable`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-variable?view=powershell-7) – Robert Dyjas May 11 '20 at 19:10
  • what are you trying to achieve? using BAT/CMD seems backwards since powershell is able to do almost everything that you can do with BAT/CMD ... and do it much more clearly. – Lee_Dailey May 11 '20 at 19:25
  • 1
    There are no specific array variables in batch, but if you separate values in a variable with a delimiter like `;`, you can use a for loop through the variable and it would go through each value separated by the `;` or other delimiter. – Nico Nekoru May 11 '20 at 21:32
  • @jwdonahue I tried it already, but it didn't work. I want to make just a small batch script to redirect to an internet page (which I wrote in the Batch file itself) if internet is working. If not, it should redirect the user to a local address which is declared in a JSON file. –  May 12 '20 at 06:56
  • There's many approaches to Variable manipulation you can use, from Arrays and lists to Nested variables. [See here](https://stackoverflow.com/a/59680701/12343998) – T3RR0R May 12 '20 at 07:54

1 Answers1

0

This is my full code:

@ECHO OFF
SETLOCAL
PING www.google.com -n 1 -w 1000 >NUL
powershell -command " Get-Content -Path \montemanager\data.json | ConvertFrom-JSON | Out-File -FilePath \montemanager\output.txt "
FOR /F "tokens=1,3 delims=-----" %%A IN (\montemanager\test.txt) DO (
   SET %%A = %%B
)
IF ERRORLEVEL 1 (
    REM Redirect to local page
) ELSE (
    START "" http://google.com
)