Home > Blockchain >  Batch parse command output for :read-attribute(name=header-value)
Batch parse command output for :read-attribute(name=header-value)

Time:03-31

I want to execute a command in my batch script and need to read a specific part of the output (parse the output). The output looks like this:

{
    "outcome" => "success",
    "result" => "http://localhost:3000"
}

The information I want to extract from it and store in a variable or output it in a first step is http://localhost:3000

I've tried to use the standard parse-loop for this.

FOR /F delims=^=^> \" skip=2 tokens=2 %%G IN ('myCommand') DO @ECHO %G

I'm trying to skip the first two lines, and then split at => ", to get the information. But it is not working. How can I extract http://localhost:3000

CodePudding user response:

@ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q71689574.txt"

FOR /f "usebackqtokens=1,2delims==> " %%a IN ("%filename1%") DO (
IF "%%~a"=="result" SET "result=%%~b"
)
SET result
GOTO :EOF

I used a file named q71689574.txt containing your data for my testing.

The usebackq option is only required because I chose to add quotes around the source filename.

CodePudding user response:

You didn't escape all of the relevant characters in the for options (Spaces and equal-signs have to be escaped too).

May I suggest a slightly different solution?:

FOR /F tokens^=4delims^=^" %%G IN ('%mycommand%^|find "result"') DO @ECHO %%G
  • Related