View unanswered posts | View active topics It is currently 21 Dec 2014 18:37



Post new topic Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next
Double quotes in For loop input [SOLVED] 
Author Message

Joined: 15 Feb 2012 14:02
Posts: 163
Post Double quotes in For loop input [SOLVED]
Code:
@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:
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.



18 Jun 2012 12:16
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Notes
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.


18 Jun 2012 12:16
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post SOLVED
Scott R from stackoverflow solved the question:
http://stackoverflow.com/questions/1114 ... 8#11151638

Here is shortened version of Scott R's code:

Code:
    @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


22 Jun 2012 01:12
Profile
Expert

Joined: 16 May 2011 08:21
Posts: 1275
Location: Flanders_(Belgium)
Post Re: SOLVED
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:
set a=he"lo
set a=!a:"=""!
set a
set a=!a:""="!
set a


22 Jun 2012 08:43
Profile WWW

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: Double quotes in For loop input [SOLVED]
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.


22 Jun 2012 14:29
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 679
Location: Germany
Post Re: Double quotes in For loop input [SOLVED]
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:
@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


22 Jun 2012 15:37
Profile
Expert

Joined: 16 May 2011 08:21
Posts: 1275
Location: Flanders_(Belgium)
Post Re: Double quotes in For loop input [SOLVED]
'
You right jeb, completely missed that the OP used delims '<>' :lol:
Code:
>for /f "tokens=1-3 delims=<>" %a in ( " quotes""<are>" "here " ) do echo.%a,%b,%c
>echo. quotes"",are," "here
> quotes"",are," "here


22 Jun 2012 15:56
Profile WWW

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: Double quotes in For loop input [SOLVED]
jeb wrote:
This code would work also without replacing any quotes.

Code:
@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:
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:
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:
         <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


24 Jun 2012 14:28
Profile
Expert

Joined: 16 May 2011 08:21
Posts: 1275
Location: Flanders_(Belgium)
Post Re: Double quotes in For loop input [SOLVED]
'
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:
<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:
@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:
$=
<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.


24 Jun 2012 17:03
Profile WWW

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: Double quotes in For loop input [SOLVED]
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.


25 Jun 2012 00:18
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: Double quotes in For loop input [SOLVED]
Why I cannot remove quotes from the token which is named $?

Code:
echo %%$
set $=%%~$


The dolar token has these values:
Code:
"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 !$!


28 Jun 2012 07:59
Profile

Joined: 10 Feb 2012 02:20
Posts: 4501
Post Re: Double quotes in For loop input [SOLVED]
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:
@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.


28 Jun 2012 08:10
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: Double quotes in For loop input [SOLVED]
Thanks. I will use
Code:
set $=%%~?
instead $


28 Jun 2012 08:20
Profile

Joined: 10 Feb 2012 02:20
Posts: 4501
Post Re: Double quotes in For loop input [SOLVED]
No. Use a, b or c


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


28 Jun 2012 08:24
Profile

Joined: 15 Feb 2012 14:02
Posts: 163
Post Re: Double quotes in For loop input [SOLVED]
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.



28 Jun 2012 13:40
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 43 posts ]  Go to page 1, 2, 3  Next


Who is online

Users browsing this forum: Google [Bot], ShadowThief, Yahoo [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.