1.

Solve : trouble writing ! to a file?

Answer»

I am working on writing this batch file which will create a cshtml file for each item in a file (emailList.txt). I have it working except for one little issue. When I try to write and lines with >> destination.cshtml)  the ! will not write to the file. I'm sure I am missing something simple here, but I'm stuck. Any assistance would be appreciated.


Code: [Select]echo off
setlocal ENABLEDELAYEDEXPANSION
set emailList=%userprofile%\desktop\cshtmlPages\emailList.txt
set pageID=100001

:: this loop writes a separate web page for each email address found in the emailList
for /F %%z in (%emailList%) do (



echo ^<!DOCTYPE html^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml

echo ^<html LANG="en"^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo ^<head^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo ^<meta charset="utf-8" /^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo ^<title^>Test SITE^</title^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo ^</head^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo ^<body^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo { >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo //this is used to create the variables to be written to the database. >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo var pageName = "!pageID!"; >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo var month = DateTime.Now.Month; >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo var day = DateTime.Now.Day; >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo var year = DateTime.Now.Year; >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo var hours = DateTime.Now.Hour; >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo var minutes = DateTime.Now.Minute; >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo } >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml


echo ^<!-- display the variables for testing purposes >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml


echo ^</body^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml
echo ^</html^> >> !userprofile!\desktop\cshtmlPages\test!pageID!.cshtml





set /a pageID+=1

)


exit
I see that you are escaping the < by doing this ^<

So couldn't you just change it to: echo ^<^!DOCTYPE html^>I thought that was the case originally so I tried it and got the same results. It did not write the ^ or ! to the file. It seems that the ! is not actually a control character though. If you open a cmd prompt and type echo ! it will print ! to the screen. It has something to do with the combination of
echo ^<^^!DOCTYPE html^>thanks! I don't know why I didn't think of that.  Haha I don't know why you should have to. Batch is a pretty convoluted language. Maybe you should try AutoIt or VBS for FUTURE projects.I figured this would just be a quick write up and I'd be done with it. Instead I spend two hours looking for a solution on Google and couldn't find anything related to it. Quote from: Dan O on January 13, 2012, 02:51:54 PM

Instead I spend two hours looking for a solution on Google and couldn't find anything related to it.

What were you typing?

1. Google Groups
2. alt.msdos.batch.nt (Usenet archive)
3. Search box
4. Type: echo exclamation mark to file
5. Second hit.. thread titled "Echoing exclamation mark with delayed expansion enabled"
6. Second reply... "Prefix it with ^^ (two escape characters)" (Apr 17 2003, 1:27 pm)

30 secs approx

or even without Google Groups...

1. Google
2. Type: Echoing exclamation mark cmd
3. First hit: How can I escape an exclamation mark ! in cmd scripts?

http://stackoverflow.com/questions/3288552/how-can-i-escape-an-exclamation-mark-in-cmd-scripts

4. First answer: echo I want to go out with a bang^^!

("bang" is what older programmers call the ! character)

approx 20 seconds






Dan O - try saving yourself a lot of typing and produce a cleaner script listing, repeated output Paths\Filenames are not required on each Echo command LINE.

For.......... ) do (
echo etc...
echo etc...

etc....
)>Path\Filename

Good luck.I am not quite sure why you are using exclamtions on the user profile variable. You could use the percent signs and be just fine.
Is my eye sight gone funny? I don't ever see you using the loop variable in your output.
I would have just made that base directory a variable as well.
And since you are reading and writing to that same directory you could have just used the Pushd command to set the working directory. Then you wouldn't have had to keep typing out the path as well. Quote from: Squashman on January 13, 2012, 10:39:46 PM
I am not quite sure why you are using exclamtions on the user profile variable. You could use the percent signs and be just fine.
Is my eye sight gone funny? I don't ever see you using the loop variable in your output.
I would have just made that base directory a variable as well.
And since you are reading and writing to that same directory you could have just used the Pushd command to set the working directory. Then you wouldn't have had to keep typing out the path as well.

I imagine he just thinks you have to use exclamation marks in a loop...


And since you are not using the loop variable which means you are not using any data from your emaillist.txt file you could have just used a FOR /L loop instead and then you could get rid of the counter variable and then you don't need delayed expansion at all. Quote from: Squashman on January 14, 2012, 10:13:04 AM
And since you are not using the loop variable which means you are not using any data from your emaillist.txt file you could have just used a FOR /L loop instead and then you could get rid of the counter variable and then you don't need delayed expansion at all.

The effect of for /F %%z in (%emailList%) do ( is to iterate the loop once for each line in maillist.txt; but as you say %%z (which holds the text of each line) makes no further appearance in the loop code. Either he has missed out some lines, that he does not wish the world to see, from his actual code, or he has only a hazy notion of batch syntax because e.g. he has been copying code from web sites without understanding it.

[edit] looking back through his earlier posts I see that in June 2011 he published a piece of code in which a variable was not changing in a loop. He was advised to use delayed expansion and the ! notation and seems to have not quite grasped the circumstances in which these things are needed, and has decided to throw them in because they fixed up his earlier effort.

To recap what I have written many times:

In a batch or cmd script running in a Windows NT family command environment, where you have a PARENTHETICAL structure (examples below), you need to use delayed expansion for any variables which are both set and expanded inside the parentheses. In the examples below, var1 was set before the parentheses and therefore does not need delayed expansion, and can have % signs. var2 and time are both created and expanded inside the parentheses and need the ! symbol, and prior enabling of delayed expansion. Note that once the closing parenthesis is passed, var2 is accessible with ordinary % signs.


set var1=hello

for blah blah blah (
     set var2=%%X
     echo !time!
     echo %var1%
     echo !var2!
     etc
     )
   
     echo %var1%
     echo %var2%

if a==b (
    set var2=whatever
    echo !time!
    echo %var1%
    echo !var2!
    )

    echo %var1%
    echo %var2%

command1 && (
    set var2=whatever
    echo !time!
    echo %var1%
    echo !var2!
    )

    echo %var1%
    echo %var2%








Discussion

No Comment Found