| 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. 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. 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% |
|