Page 1 of 1

Help on Grouping Commands in a Block

Posted: 28 Feb 2008 15:02
by simplyjaded
Hi,

I'm a newbie in writing batch files. Anyways I've got this code here which really confuses me...

@ECHO off
set "text2=about another."
echo %text2%
set text1=mharz

for /f "usebackq tokens=*" %%a in ("list.txt") do (
if %%a==%text2% (

echo %text2%
) else (
echo %%a
)

)
echo done.
goto :EOF

When I run the code above, this is the output:

about another. :: this is from echo %text2%
else was unexpected at this time. :: now, I don't understand this error here... Can anyone explain please???

Another weird thing is, when I change this line:
if %%a==%text2% (
to:
if %%a==%text1% (

it works perfectly fine!! Whats wrong with using text2???
text2 does look ok when I tried to output its value...

Do you know any workaround on this problem...?? I need text2 variable to have 2 or 3 words read as a single line.. Please help..

Thanks.

Posted: 28 Feb 2008 17:22
by DosItHelp
simplyjaded,

Try with quotes:

Code: Select all

...
if "%%a"=="%text2%" (
...

DOS IT HELP? :wink:

Post subject: Help on Grouping Commands in a Block

Posted: 29 Feb 2008 08:54
by simplyjaded
Yes, it works fine. thank you. :D

I've got another problem though...
The lines below seems to concatenate multiple words in a single line:

set "text2=about another."
echo %text2%


But what if I've this value to be put to text2.
<Data ID="999" Block="Z" Section="Z-9"/>

Doing this will prompt an error bec of <, >:
set "text2=<Data ID="999" Block="Z" Section="Z-9"/>"

Now, I modified the code above to this:
set "text2=^<Data ID="999" Block="Z" Section="Z-9"/^>"
echo %text2%


It works as expected when i echoed the value of text2.
My problem is, when I use it in a for loop like the code below

for /f "usebackq tokens=*" %%a in ("Trial.xml") do (
echo %%a >> Check.txt
echo %text2% >> Check.txt
if "%%a"=="%text2%" (
echo %text1% >> Output.txt
echo %%a >>Output.txt
) else (
echo grr %%a >>Output.txt
)
)

The value of "%text2%" includes the ^ so it can never return true.

Here's an excerpt of the output:

C:\Batchfiles\Trials>(
echo <Data ID="999" Block="Z" Section="Z-9"/> 1>>Check.txt
echo <Data ID="999" Block="Z" Section="Z-9"/> 1>>Check.txt
if "<Data ID="999" Block="Z" Section="Z-9"/> " == [b]"^<Data ID="999" Block="Z"
Section="Z-9"/^>" [/b
](
echo Data ID="003" Block="A" Section="A-3"/ 1>>Output.txt
echo <Data ID="999" Block="Z" Section="Z-9"/> 1>>Output.txt
) else (echo grr <Data ID="999" Block="Z" Section="Z-9"/> 1>>Output.txt )
)

I don't understand why the ^ is being included during comparison when in echoing the value, its not.

Any suggestion please...??? thanks.