Help on Grouping Commands in a Block

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
simplyjaded
Posts: 2
Joined: 28 Feb 2008 14:32

Help on Grouping Commands in a Block

#1 Post by simplyjaded » 28 Feb 2008 15:02

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.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 28 Feb 2008 17:22

simplyjaded,

Try with quotes:

Code: Select all

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

DOS IT HELP? :wink:

simplyjaded
Posts: 2
Joined: 28 Feb 2008 14:32

Post subject: Help on Grouping Commands in a Block

#3 Post by simplyjaded » 29 Feb 2008 08:54

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.

Post Reply