Editting text with strange characters with a batch file

July 23, 2012 at 13:46:49
Specs: Windows 7


I have a folder full of hundreds of .src files that I need to add some text to in a specific spot. I need to add 3 lines of code after the file says M10
For example
.....
.....
M10
.....
.....

needs to be

.....
M10
M19(" Optional Stop: Verify Acceptable Label!")
M1
M19(" ")
......


If someone could give me a sample of a very well commented (I'm a rookie) batch file that could do this, that would be great! I am using Windows 7.


See More: Editting text with strange characters with a batch file

Report •

#1
July 23, 2012 at 18:07:18
@echo off & setlocal
set a1=M19(" Optional Stop: Verify Acceptable Label!")
set a2=M1
set a3=M19(" ")

for %%b in (*.src) do (
>temp echo.
for /f "usebackq tokens=* eol=" %%a in ("%%b") do (
>>temp echo %%a
if %%a equ M10 call :out
)
more temp
echo move /y temp "%%b"
)
goto :eof
:out
>>temp echo %a1%
>>temp echo %a2%
>>temp echo %a3%
:end

not tested yet, probably has errors! Try before you Fly


Report •

#2
July 24, 2012 at 00:36:23
WHAT strange chars?


====================================
Life is too important to be taken seriously.

M2


Report •

#3
July 24, 2012 at 05:44:05
I've got to add text that has (), "", and a !. I tried doing this myself but it didn't see the parenthesis as text and ended up closing the do command prematurely. I tried the batch file code that nbrane posted but it didn't work. I'll try and see if I can find what the problem is. Thank you so much for your help.

Report •

Related Solutions

#4
July 24, 2012 at 07:54:14
Put the preceding lines into pre.txt
the rest into post.txt
then:

copy/b pre.txt+newlines.txt+post.txt newfile


====================================
Life is too important to be taken seriously.

M2


Report •

#5
July 24, 2012 at 14:57:43
So I've got it to work for the most part but I am having a few minor probelms. In the lines

set a1=M19(" Optional Stop: Verify Acceptable Label!")

I had to write it as such to get it to work. If not, the batch file thinks that the parenthesis are for the for loop when it echos them.

set a1="M19(" Optional Stop: Verify Acceptable Label!")"

The quotation marks then show up in the new file but I don't want them there. Is there any way to set the variable to that whole entire string with so the parenthesis doesn't throw it off? I hope this makes sence.

Another slight problem is that the for loop is ignoring lines in the file that start with a semi-colon so they get left out of the new file. I think this might be a similar problem. The variable isn't reading it as text. Any suggestions would be great! Thank you so much for your help!!!


Report •

#6
July 24, 2012 at 19:33:58
Hello. I've edited my script to handle the "no-no" characters, i think. Not sure how it ever worked, given that i dbl-quoted the filename ("%%b") but did not use the "usbackq" qualifier... (I will never cease to bitch/whine/complain of MS' stupidity allowing spaces, or commas for that matter, into filenames, just to make them "look pretty". ALL this crap, just to have "pretty" filenames. wrong with good old underscore?) ok, end rant, sorry. I think M2go solution works around ALL your issues. Put your 4-line file into "newlines" (assuming they're constant), do a "pre-trigger" composition into pre.txt and a "post-trigger" composition into post.txt.You still might have special-char issues if these files have ()"! in them.
The script I modified uses "call" to exit (temporarily) the for-loop, thus neutralizing the () problem, and uses "eol=" qualifier to negate the semicolon (at least, it worked in my tests on winXP,. Funny, I don't remember encountering that before: eol defaulting to ;). I "marginally" tested my version, seemed to work...

Report •

#7
July 25, 2012 at 10:37:51
I got it to work. Thanks to the both of you so much! I couldn't have done it without you.

Report •

#8
July 26, 2012 at 10:08:00
nbrane: I will never cease to bitch/whine/complain of MS' stupidity allowing spaces, or commas for that matter, into filenames, just to make them "look pretty". ALL this crap, just to have "pretty" filenames. wrong with good old underscore?
I was like you once. Then I mellowed out, and it didn't bother me anymore. Then I read how POSIX systems have it worse, and I was amused.

How To Ask Questions The Smart Way


Report •

#9
July 26, 2012 at 18:37:51
Lol! I guess it gives me something to do. Kind of like complaining about the heat or the rain... Now I know I'm getting old!

Report •

#10
July 27, 2012 at 03:40:09
nbrane,

You don't know what old is.
When our school got win95 and I saw Progra~1 I knew there was 40 miles of bad road ahead.


====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question