Using for command starts/ends with double quote.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
WiVi71
Posts: 15
Joined: 06 Jan 2020 02:33

Using for command starts/ends with double quote.

#1 Post by WiVi71 » 01 May 2020 16:39

I'm making a code to get some value from website with curl and sed.
The website will include code that starts with name and ends with value with double quote.

Code: Select all

<div style="display:none;">
something="somethig"
categoryNo="[numbers]"
</div>
In this code, I need [numbers].

Here is the code that I made to get [numbers].

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
:: some commands..
:: curl path is !curl_p!
IF "!URL!" == "!URL:PostView.com?=!" IF "!URL!" == "!URL:PostList.com?=!" (
	IF DEFINED URL_logNo (
		ECHO ERROR
		CALL :ERROR
		EXIT /B
	)
	FOR /F %%A IN ('"!cURL_p!\curl.exe" -s https://url.com ^| "!NCA_P!\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p"') DO SET URL_categoryNo=%%A
)
The summarized code is:

Code: Select all

:: this code will excute: "!cURL_p!\curl.exe" -s https://url.com | "!NCA_P!\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p"
IF . EQU . (FOR /F %%A IN ('"!cURL_p!\curl.exe" -s https://url.com ^| "!NCA_P!\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p"') DO SET URL_categoryNo=%%A)
curl will not be in %~dp0, so it must be enclosed in double quotes.
options for sed below should also be enclosed in double quote.

But For /f that starts/ends with double quote will not work as expected.

Code: Select all

IF . EQU . (FOR /F %%A IN ('!cURL_p!\curl.exe -s https://url.com ^| "!NCA_P!\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p"') DO SET URL_categoryNo=%%A)
This code will not work if "curl path" contains special characters like "&"

Code: Select all

IF . EQU . (FOR /F %%A IN ('""!cURL_p!\curl.exe" -s https://url.com | "!NCA_P!\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p""') DO SET URL_categoryNo=%%A)
This code will not work too.

Using CMD /C or moving to curl path is a not bad way, but I don't want to modify this code as much as possible.
Is there a simple way to use For /f that starts and ends with double quotes?

penpen
Expert
Posts: 1824
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Using for command starts/ends with double quote.

#2 Post by penpen » 01 May 2020 17:30

The cause and cure was explained in detail somewhere here in this forum, but my google-fu isn't strong enough (at least tonight).
Maybe someone has bookmarked that or remembers why that goes wrong.
If i remember right my workaround was, to just add an echo command and to not use delayedExpansion in the set-part of the for/f loop, so the following may (or may not) help you:

Code: Select all

IF . EQU . (FOR /F %%A IN ('echo Ok ^| "%cURL_p%\curl.exe" -s https://url.com ^| "%NCA_P%\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p"') DO SET URL_categoryNo=%%A)
penpen

WiVi71
Posts: 15
Joined: 06 Jan 2020 02:33

Re: Using for command starts/ends with double quote.

#3 Post by WiVi71 » 04 May 2020 14:33

penpen wrote:
01 May 2020 17:30
The cause and cure was explained in detail somewhere here in this forum, but my google-fu isn't strong enough (at least tonight).
Maybe someone has bookmarked that or remembers why that goes wrong.
If i remember right my workaround was, to just add an echo command and to not use delayedExpansion in the set-part of the for/f loop, so the following may (or may not) help you:

Code: Select all

IF . EQU . (FOR /F %%A IN ('echo Ok ^| "%cURL_p%\curl.exe" -s https://url.com ^| "%NCA_P%\sed.exe" -n "s/^categoryNo=.\([0-9]\+\).$/\1/p"') DO SET URL_categoryNo=%%A)
penpen
Fortunately, the method worked fine.
Thanks! :D

Post Reply