Pipe in for loop breaks double quoted variables
Posted: 14 May 2015 07:13
Situation: Using batchscript to retrieve certain values from a JSON.
I've got the following batchscript:
What does it do?
Upon having entered a npo.nl program-url, the first for-loop strips the url down to the prid: POMS_VPRO_850040. In the 2nd for-loop curl.exe retrieves the JSON...:
...and sends it through a pipe to jq.exe which removes the non-JSON-data parseMetadata( and ) //epc and leaves the single line intact. This is for 2 reasons: 1) with non-JSON-data present jq.exe can't process anything, and 2) for-loops process only 1 line at a time.
The 3rd for-loop retrieves and echoes values for the specified objects without double quotes.
As long as curl.exe and jq.exe are in the same directory as the batchscript, or in the %path%-variable, this is working all fine:
Now I want to call curl.exe and jq.exe from another map. One with spaces in it:
For the 2nd for-loop this causes problems:
While 'ECHO %%B ^| %jq%' does work, it seems '%curl% ^| %jq%' doesn't. So for some reason things go wrong as soon as 2 variables in a pipe are parsed.
Well, no more pipe then:
Now curl.exe and jq.exe each in a for-loop. At first this seems to work fine. The 3 values are echoed, but then things go wrong:
Like I said before; for-loops parse and process only 1 line at a time. The non-JSON-data //epc on the 2nd line causes the for-loop to start over, which goes horribly wrong as you can see. That's the reason for the pipe between curl and jq in the code above. To output 1 single line to process. Sadly that didn't work either...sigh.
Of course using temporary files is a last resort when curl and jq are still in a map with spaces in it, but I prefer to use variables, so I'm trying to solve the pipe-issue. I've tried 'usebackq' in the for-loop using backticks around the command instead of single-quotes for instance, but to no avail.
So far I haven't found a solution. Does anyone have an explanation for this behaviour and how to solve it?
I've got the following batchscript:
Code: Select all
@ECHO off
SET url=http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('curl.exe -s http://e.omroep.nl/metadata/aflevering/%%A ^| jq.exe -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"') DO (
FOR /F "tokens=1-3" %%C IN ('ECHO %%B ^| jq.exe -r "[.tijdsduur,.start,.eind] | \"\(.[0]^) \(.[1]^) \(.[2]^)\""') DO (
ECHO Duration: %%C
ECHO Start: %%D
ECHO End: %%E
)
)
)
PAUSE
What does it do?
Upon having entered a npo.nl program-url, the first for-loop strips the url down to the prid: POMS_VPRO_850040. In the 2nd for-loop curl.exe retrieves the JSON...:
Code: Select all
parseMetadata({"STATUS":"OK","VERSION":"1.11.12","prid":"VPWON_1232766",[.........],"sko_cl":"3284"}})
//epc
The 3rd for-loop retrieves and echoes values for the specified objects without double quotes.
As long as curl.exe and jq.exe are in the same directory as the batchscript, or in the %path%-variable, this is working all fine:
Code: Select all
Duration: 00:05:27
Start: 00:23:13
End: 00:28:40
Now I want to call curl.exe and jq.exe from another map. One with spaces in it:
Code: Select all
@ECHO off
SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"
SET url=http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('%curl% -s http://e.omroep.nl/metadata/aflevering/%%A ^| %jq% -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"') DO (
FOR /F "tokens=1-3" %%C IN ('ECHO %%B ^| %jq% -r "[.tijdsduur,.start,.eind] | \"\(.[0]^) \(.[1]^) \(.[2]^)\""') DO (
ECHO Duration: %%C
ECHO Start: %%D
ECHO End: %%E
)
)
)
PAUSE
Code: Select all
'C:\map' is not recognized as an internal or external command,
operable program or batch file.
Well, no more pipe then:
Code: Select all
@ECHO off
SET curl="C:\map with spaces\curl.exe"
SET jq="C:\map with spaces\jq.exe"
SET url=http://www.npo.nl/buitenhof/03-05-2015/VPWON_1232766/POMS_VPRO_850040
FOR /F "tokens=6 delims=/" %%A IN ("%url%") DO (
FOR /F "delims=" %%B IN ('%curl% -s http://e.omroep.nl/metadata/aflevering/%%A') DO (
FOR /F "delims=" %%C IN ('ECHO %%B ^| %jq% -R -r -s ".[1+index(\"(\"): rindex(\"^)\")]"') DO (
FOR /F "tokens=1-3" %%D IN ('ECHO %%C ^| %jq% -r "[.tijdsduur,.start,.eind] | \"\(.[0]^) \(.[1]^) \(.[2]^)\""') DO (
ECHO Duration: %%D
ECHO Start: %%E
ECHO End: %%F
)
)
)
)
PAUSE
Code: Select all
Tijdsduur: 00:05:27
Start: 00:23:13
Einde: 00:28:40
parse error: Invalid numeric literal at line 1, column 4
parse error: Invalid numeric literal at line 1, column 4
Of course using temporary files is a last resort when curl and jq are still in a map with spaces in it, but I prefer to use variables, so I'm trying to solve the pipe-issue. I've tried 'usebackq' in the for-loop using backticks around the command instead of single-quotes for instance, but to no avail.
So far I haven't found a solution. Does anyone have an explanation for this behaviour and how to solve it?