Double quotes in For loop input [SOLVED]

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Double quotes in For loop input [SOLVED]

#1 Post by doscode » 18 Jun 2012 12:16

Code: Select all

@echo off 
Setlocal EnableDelayedExpansion
SET proxy_3=hide_2.htm         

FOR %%Z IN (hide_2.htm) DO (
FOR /F "tokens=1-20 delims=<>" %%A IN ('grep -B 1411 -E "</table>" %%Z ^| grep -E ^"^(display^|^^\d\d{1,3}^|country^|^<td^>HTTP^|rightborder^).*$^" ') DO (
echo A:%%A + B:%%B + C:%%C + D:%%D + %%E + %%F + %%G + %%H + %%I + %%J + %%K + %%L
FOR %%? in ( "%%~A", "%%~B", "%%~C", "%%~D", "%%~E", "%%~F", "%%~G", "%%~H", "%%~I", "%%~J") DO (
SET $=%%~?
echo $:!$!
)
pause
)
)

http://codepaste.net/iaf4zr

This is the source, See lines 581-585:
http://codepaste.net/11bqxd
This is the most problematic part L.581-585: *

This is the output:

Code: Select all

A:          + B:td + C:span + D:span + 41 + /span + span style="display: none;"
+ 111 + /span + div +  +
$:
$:td
$:span
$:span
$:41
$:/span
$:span style="display:
$:none
$:
$:111
$:/span
$:div
Press any key to continue...
A: style="display: none;" + B:190 + C:/div + D:span class="" style="" + . + /spa
n + span + 197 + /span + span +  +
$: style="display:
$:none
$:
$:190
$:/div
$:span class="" style=""
$:.
$:/span
$:span
$:197
$:/span
$:span
Press any key to continue...
A: style="display: none;" + B:24 + C:/span + D:span + /span + . + span style="di
splay:  +  +  +  +  +
$: style="display:
$:none
$:
$:24
$:/span
$:span
$:/span
$:.
$:span style="display:
$: "" "" "
Press any key to continue...
A:inline;" + B:132 + C:/span + D:span style="display: none;" + 39 + /span + . +
span  +  +  +  +
$:inline;"" "132" "/span" "span
$:style
$:display: none;"" "39" "/span" "." "span
$: "" "

Where the regarded/related part to lines 581-585 is:
A:inline;" + B:132 + C:/span + D:span style="display: none;" + 39 + /span + . +
span + + + +
$:inline;"" "132" "/span" "span
$:style
$:display: none;"" "39" "/span" "." "span
$: "" "

So the token B in 2nd loop is 132, no quotes. It look OK. But in the 3rd loop, it changes to ... style.

Whereas 1st token in 2nd loop is inline;", the 3rd loop shows: inline;"" "132" "/span" "span

Can you explain me how this is possible? I would like to see there 132 when the 2nd member is received.
Last edited by doscode on 22 Jun 2012 01:16, edited 4 times in total.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Notes

#2 Post by doscode » 18 Jun 2012 12:16

This is the most problematic part L.581-585: *
* - http://codepaste.net/e1t61n

Blue parts of the code are debugging part of the echo command.

The dollar - $: marks the value of $ variable, which should be the derived column/token from the second loop without quotes. Here I look for number values, without quotes. This fails in the last case.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

SOLVED

#3 Post by doscode » 22 Jun 2012 01:12

Scott R from stackoverflow solved the question:
http://stackoverflow.com/questions/1114 ... 8#11151638

Here is shortened version of Scott R's code:

Code: Select all

    @echo off 
    Setlocal EnableDelayedExpansion
   
    FOR /F "tokens=1 delims=" %%A IN (html_2.txt) DO (
        set a=%%A
        set a=!a:"=\q!
        echo a=!a!
        FOR /F "tokens=1-26 delims=<>" %%A in ( "!a!" ) DO (
            FOR %%Z in ( %%A, %%B, %%C, %%D, %%E, %%F, %%G, %%H, %%I, %%J) DO (
            set z=%%Z &echo !z:\q="!
            )
        )
        pause
    )


set a=!a:"=\q!
To strip out the double quotes

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: SOLVED

#4 Post by Ed Dyreen » 22 Jun 2012 08:43

doscode wrote:set a=!a:"=\q!
To strip out the double quotes
What do you think will happen if the source contains '\q' ?

Try

Code: Select all

set a=he"lo
set a=!a:"=""!
set a
set a=!a:""="!
set a

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Double quotes in For loop input [SOLVED]

#5 Post by doscode » 22 Jun 2012 14:29

The code does not contain \q. For me, the point is that I have found where was the problem. Better solution for me is to remove double quotes because I don't need them in the script for detection of numbers.

jeb
Expert
Posts: 1041
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Double quotes in For loop input [SOLVED]

#6 Post by jeb » 22 Jun 2012 15:37

jeb - from SO: wrote:@Skip R - Sorry, but your code is nonsense! You could remove the set a=!a:"=\q! and it will work also (and better!). Also the exclamation mark handling isn't correct.


As I said before, it doesn't solve your problem by replacing any quotes.

This code would work also without replacing any quotes.

Code: Select all

@echo off
Setlocal EnableDelayedExpansion

FOR /F "tokens=1 delims=" %%A IN (html_2.txt) DO (
    set a=%%A
    echo a=!a!
    FOR /F "tokens=1-26 delims=<>" %%a in ( "!a!" ) DO (
        set a=%%a& echo $:!a!
        set b=%%b& echo $:!b!
        set c=%%c& echo $:!c!
        set d=%%d& echo $:!d!
        set e=%%e& echo $:!e!
        set f=%%f& echo $:!f!
        set g=%%g& echo $:!g!
        set h=%%h& echo $:!h!
        rem repeat thru to z
        )
)


jeb

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Double quotes in For loop input [SOLVED]

#7 Post by Ed Dyreen » 22 Jun 2012 15:56

'
You right jeb, completely missed that the OP used delims '<>' :lol:

Code: Select all

>for /f "tokens=1-3 delims=<>" %a in ( " quotes""<are>" "here " ) do echo.%a,%b,%c
>echo. quotes"",are," "here
> quotes"",are," "here

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Double quotes in For loop input [SOLVED]

#8 Post by doscode » 24 Jun 2012 14:28

jeb wrote:This code would work also without replacing any quotes.

Code: Select all

@echo off
Setlocal EnableDelayedExpansion

FOR /F "tokens=1 delims=" %%A IN (html_2.txt) DO (
    set a=%%A
    echo a=!a!
    FOR /F "tokens=1-26 delims=<>" %%a in ( "!a!" ) DO (
        set a=%%a& echo $:!a!
        set b=%%b& echo $:!b!
        set c=%%c& echo $:!c!
        set d=%%d& echo $:!d!
        set e=%%e& echo $:!e!
        set f=%%f& echo $:!f!
        set g=%%g& echo $:!g!
        set h=%%h& echo $:!h!
        rem repeat thru to z
        )
)


This is my code:

Code: Select all

set a=%%A&set b=%%B&set c=%%C&set d=%%D&set e=%%E&set f=%%F
set g=%%G&set h=%%H&set i=%%I&set j=%%J&set k=%%K&set l=%%L
set m=%%M&set n=%%N&set o=%%O
if NOT "%%A" == "" set a=!a:"=!
if NOT "%%B" == "" set b=!b:"=!
if NOT "%%C" == "" set c=!c:"=!
if NOT "%%D" == "" set d=!d:"=!
if NOT "%%E" == "" set e=!e:"=!
if NOT "%%F" == "" set f=!f:"=!
if NOT "%%G" == "" set g=!g:"=!
if NOT "%%H" == "" set h=!h:"=!
if NOT "%%I" == "" set i=!i:"=!
if NOT "%%J" == "" set j=!j:"=!
if NOT "%%K" == "" set k=!k:"=!
if NOT "%%L" == "" set l=!l:"=!
if NOT "%%M" == "" set m=!m:"=!
if NOT "%%N" == "" set n=!n:"=!
if NOT "%%O" == "" set o=!o:"=!

And it works. If I would remove the part replacing quotes, it generates mistakes in IP (mistakes during parsing).

I have tested your code few days ago, and this was the result:

Code: Select all

a=         <td><span><span>41</span><span style="display: none;">111</span><div
$:
$:td
$:span
$:span
$:41
$:/span
$:span style="display: none;"
$:111
a=style="display: none;">190</div><span class="" style="">.</span><span>197</spa
n><span
$:style="display: none;"
$:190
$:/div
$:span class="" style=""
$:.
$:/span
$:span
$:197
a=style="display: none;">24</span><span></span>.<span style="display:
$:style="display: none;"
$:24
$:/span
$:span
$:/span
$:.
$:span style="display:
$:
a=inline;">132</span><span style="display: none;">39</span>.<span
$:inline;"
$:132
$:/span
$:span style="display: none;"
$:39
$:/span
$:.
$:span
a=style="display: inline;">186</span></span></td>
$:style="display: inline;"
$:186
$:/span
$:/span
$:/td
$:
$:
$:
a=line=inline;">132</span><span style="display: none;">39</span>.<span
$:line=inline;"
$:132
$:/span
$:span style="display: none;"
$:39
$:/span
$:.
$:span
Press any key to continue...

You see the double quotes are still there. The contents of the source file to test:

Code: Select all

         <td><span><span>41</span><span style="display: none;">111</span><div
style="display: none;">190</div><span class="" style="">.</span><span>197</span><span
style="display: none;">24</span><span></span>.<span style="display:
inline;">132</span><span style="display: none;">39</span>.<span
style="display: inline;">186</span></span></td>   
line=inline;">132</span><span style="display: none;">39</span>.<span

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Double quotes in For loop input [SOLVED]

#9 Post by Ed Dyreen » 24 Jun 2012 17:03

'
First of all, what is the meaning of '<span></span>' in your HTML ?, that just don't makes sense.
On top of that, it isn't valid HTML, I had to rewrite it and remove some parts to get it validated !

'html_2.txt'

Code: Select all

<td><span>
<span>41</span>
<span style="display: none;">111</span>
<span class="" style="">.</span>
<span>197</span>
<span style="display: none;">24</span>.
<span style="display: inline;">132</span><span style="display: none;">39</span>.
<span style="display: inline;">186</span></span></td>
<span style="display: none;">39</span>.
I have no idea what you mean with detection of numbers but, this code produces an output that is easy to work with, provided the html is valid. This simple example is limited to 8k but can be enhanced to tackle a 64mb HTML file.

Code: Select all

@echo off &setlocal enableDelayedExpansion &set $lf=^


set "$=!$lf!" &(

   for /f "usebackq delims=" %%? in ( "html_2.txt" ) do set "$=!$!%%?"

) &   echo.$=!$!_ &for %%? in (

   "!$lf!"

) do    set "$=!$:>=>%%~?!" &set "$=!$:<=%%~?<!"

echo.
echo.$=!$!_

pause
exit

Code: Select all

$=
<td><span><span>41</span><span style="display: none;">111</span><span class="" s
tyle="">.</span><span>197</span><span style="display: none;">24</span>.<span sty
le="display: inline;">132</span><span style="display: none;">39</span>.<span sty
le="display: inline;">186</span></span></td><span style="display: none;">39</spa
n>._

$=

<td>

<span>

<span>
41
</span>

<span style="display: none;">
111
</span>

<span class="" style="">
.
</span>

<span>
197
</span>

<span style="display: none;">
24
</span>
.
<span style="display: inline;">
132
</span>

<span style="display: none;">
39
</span>
.
<span style="display: inline;">
186
</span>

</span>

</td>

<span style="display: none;">
39
</span>
._
Druk op een toets om door te gaan. . .
PS: Placing CSS content into its own file has several advantages. For one, the CSS content is completely separated from the HTML this way, which emphasizes the separation of content from presentation. Also, if the style sheet is in a separate file, it can be used by many HTML documents.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Double quotes in For loop input [SOLVED]

#10 Post by doscode » 25 Jun 2012 00:18

Ed Dyreen wrote:'
First of all, what is the meaning of '<span></span>' in your HTML ?, that just don't makes sense.
On top of that, it isn't valid HTML, I had to rewrite it and remove some parts to get it validated !

The html code is not of mine! I use the parsing script of web page downloaded from net. It gives sense to me! Creator of the html just wanted to confuse programmers, which want to create automatized scripts for detection of IP address. For this reason it needs not to be valid. There is higher goel - to confuse robots.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Double quotes in For loop input [SOLVED]

#11 Post by doscode » 28 Jun 2012 07:59

Why I cannot remove quotes from the token which is named $?

Code: Select all

echo %%$
set $=%%~$


The dolar token has these values:

Code: Select all

"td"
"span"
"span class=82"
"41"
"/span"
"."
"span style=display: inline"
"35"
"/span"
"span style=display:none"
"142"
"/span"
"div style=display:none"
"118"
"/div"
"="
"span"
"47"
"/span"
"span style=display:none"
"190"
"/span"
"span"
"/span"
"."
"8080"
"/td"


But when I replace it, I have %~$ in !$!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Double quotes in For loop input [SOLVED]

#12 Post by foxidrive » 28 Jun 2012 08:10

You seem to have found a good reason why alpha characters should be used in a for loop variable. The other reason is that alpha characters are documented and newbies think "WTF!?" when they see %%? etc.

Code: Select all

@echo off
set var="test"
for /f "delims=" %%a in (%var%) do (
echo %%~a
set $=%%~a
)
echo %$%


The reason why it fails can be found in the last page of FOR /? help.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Double quotes in For loop input [SOLVED]

#13 Post by doscode » 28 Jun 2012 08:20

Thanks. I will use

Code: Select all

set $=%%~?
instead $

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Double quotes in For loop input [SOLVED]

#14 Post by foxidrive » 28 Jun 2012 08:24

No. Use a, b or c


Using ? is ill advised. There is no advantage to using ? only drawbacks.

doscode
Posts: 175
Joined: 15 Feb 2012 14:02

Re: Double quotes in For loop input [SOLVED]

#15 Post by doscode » 28 Jun 2012 13:40

foxidrive wrote:No. Use a, b or c


Using ? is ill advised. There is no advantage to using ? only drawbacks.


I have all letters reserved for tokens A-Z.

Maybe I could use more letters [a-zA-Z] but I don't know if is it possible? I mean If I type
for %%a IN (..) DO .. can I access %%Z in 28th token?
Last edited by doscode on 28 Jun 2012 14:10, edited 1 time in total.

Post Reply