Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a string between two quotes in a batch file?

I have a string in a batch file, of the structure

[[status]]:{"01bcd123-1234-5678-0000-abcdefghijkl": "11"}

I need to get just the 01bcd123-1234-5678-0000-abcdefghijkl out of it, but trying to use " as a delimiter doesn't turn out well. \ and ^ don't seem to escape it properly.

set i=1

set "x!i!=%x:"=" & set /A i+=1 & set "x!i!=%"

Is what I have with x being the whole string, attempting to parse it into x1, x2 etc with " as the delimiter.

What is a proper way to split this string, using " as the delimiter?

Edit: Powershell tag is because I am running the script as part of a larger orchestration in Powershell and could export the functionality of the batch script into it if necessary.

like image 713
King Dedede Avatar asked Aug 31 '25 03:08

King Dedede


1 Answers

Here are two approaches. The first one doesn't mess with the for syntax format, but it's risky - too much dependence on the string (the quotes are actually stripped by %%~). The second one is an ugly non-intuitive syntax, but actually delimits by quotes:

set "string=[[status]]:{"01bcd123-1234-5678-0000-abcdefghijkl": "11"}"
for /f "tokens=2 delims=:{" %%a in ("%string%") do @echo %%~a
for /f tokens^=2delims^=^" %%a in ("%string%") do @echo %%a
like image 189
Stephan Avatar answered Sep 02 '25 18:09

Stephan