Please help with if statement and variable / string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
booga73
Posts: 108
Joined: 30 Nov 2011 16:16

Please help with if statement and variable / string

#1 Post by booga73 » 10 Dec 2011 16:48

I have batch code that is the following:
--------------------------------

@echo ^<table border="1"^> >> c:\temp1\irtdiag1.txt

set "str1=Customer Name: "
set "str2=Street: "
set "str3=Favorite Dish: "
set "str4=Favorite Story: "
set "str5=Total Basket: "


setlocal enabledelayedexpansion
set "count=0"

for /f "tokens=*" %%a in (c:\temp1\123tmp\info1.txt) do (

If "count=1" str1=%%s
If "count=2" str2=%%s
If "count=3" str3=%%s
If "count=4" str4=%%s
If "count=5" str5=%%s


@echo ^<tr^>^<td^> %%s %%a^</td^>^</tr^> >>c:\temp1\diag1.txt
set /a "count+=1" )

@echo ^</table^> >> c:\temp1\diag1.txt

--------------------------------

I get an error: str1=%s was unexpected at this time.

Do I have the if statement constructed wrong?
Should the if statement be inside or outside of the for loop?

I need help passing a string constant to a variable.

The purpose of this code is when the for loop starts running, the code will 1 by 1 check each count and at each count take the string that is found inside info1.txt file and pair that string value from info1.txt with each count verified and write the data to text file, diag1.txt.




For instance, if the count is at 1 then the string constant "str1" (which should be Customer Name: ) should be passed to %%s string variable so that my echo statement will start building the html table / cell with:

Customer Name: (string found from within info1.txt)

or easier stated if string 1 of info1.txt is "Bob's Corn Oil is Corny."

Customer Name: Bob's Corn Oil is Corny.

(if perhaps your name is actually Bob or Rob or Robert, didn't mean to offend, I was simply injecting some hummer in my support of this batch script.)



the ultimate display of information I want to get is a html table cell to display information.


Customer Name: Bob
Street: 123 River St.
Favorite Dish: Mac'n Cheese
Favorite Story: Peter Pan
Total Basket: 14.10


best regards, Booga73

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

Re: Please help with if statement and variable / string

#2 Post by Ed Dyreen » 10 Dec 2011 17:50

'
Hi, tried to get the desired logic but have to leave now, here my initial attempt, hope it helps in the meantime :|

Code: Select all

@echo off &setlocal enableDelayedExpansion

> "irtdiag1.txt" type nul
>>"irtdiag1.txt" (
   echo.!"!<table border="1">!"! &for %%? in (
      "Customer Name:", "Street:", "Favorite Dish:", "Favorite Story:", "Total Basket:"
   ) do    echo.!"!%%~? value!"!
)

> "ou.txt" type nul
>>"ou.txt" (
   for /f "usebackq tokens=*" %%? in (
      "irtdiag1.txt"
   ) do (
      set "$newdata=%%~?" &set /a "$=0" &for %%! in (
         "Customer Name:", "Street:", "Favorite Dish:", "Favorite Story:", "Total Basket:"
      ) do    if !$! equ 0 set "$newdata=!$newdata:%%~!=!" &if /i ["!$newdata!"] neq ["%%~?"] (
         ::
         set "$newdata=<tr><td> %%~! newvalue </td></tr>"
         set "$=1"
      )
      echo.!"!!$newdata!!"!
   )
)
>>"ou.txt" echo.!"!</table>!"!

pause
exit
"irtdiag1.txt"

Code: Select all

<table border="1">
Customer Name: value
Street: value
Favorite Dish: value
Favorite Story: value
Total Basket: value
"ou.txt"

Code: Select all

<table border="1">
<tr><td> Customer Name: newvalue </td></tr>
<tr><td> Street: newvalue </td></tr>
<tr><td> Favorite Dish: newvalue </td></tr>
<tr><td> Favorite Story: newvalue </td></tr>
<tr><td> Total Basket: newvalue </td></tr>
</table>
I almost had it right :x

Post Reply