Insert carriage return w/ batch

ebrian November 2, 2005 at 05:28:46 Pacific
XP, 1GB

Is there a way to insert a carriage return (new line character) into a file? I would like to be able to insert a carriage return whenever I find (using finstr) a # character in a file.

For example, if the file contained the string:

"This is line 1 # this should be line 2."

I would like to break it apart and save it into another file as:

"This is line 1 #
this should be line 2."

Any suggestions?


Think your friends can help? Ask them!


#1
+1
wizard-fred November 2, 2005 at 07:39:37 Pacific

There are unix style utilities like awk and sed that can do this easily. Otherwise a good text editor (work processor ) can replace a character with another.



#2
+1
ebrian November 2, 2005 at 07:50:31 Pacific

I realize there are other tools that can facilitate this, but I was hoping to be able to do it with DOS.



#3
+3
djas November 2, 2005 at 09:01:23 Pacific

Well "echo." gives you a blacnk line if thats any use to you but I'm afraid I don't know of any carriage return method.

djas



#4
+1
IVO November 2, 2005 at 12:36:36 Pacific

It is possible to do what you posted with the NT batch language of Windows XP (that has no DOS at all as it is a NT kernel system), but the key point is the following:

Is your file a true text file, i.e. a collection of records each delimited by CR/LF, the "rows", or just a byte stream ended by an EOF symbol?

If each row holds a variable number of strings delimited by #, then the job can be done by batch, otherwise the stream can not be handled by a script languag aimed to process text files.

So please explain and than we can try to help... if possible.



#5
+1
ebrian November 2, 2005 at 15:43:00 Pacific

The text file is a true text file, it contains multiple records all of which are delimited by CR/LF and some of which contain # symbols.



Related Posts

#6
+3
IVO November 3, 2005 at 02:24:13 Pacific

Well, here is your script that works under Win NT/2K/XP only. Pay attention the :SPLIT internal routine is a recursive one, i.e. it calls itself, so if the row holds too many # substrings a stack overflow may occur. Last point, the space lines marked by a CR/LF only are lost. So stated I hope you get what you wish.

@Echo OFF

For /f "tokens=1,* delims=#" %%a in (%*) Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF

:SPLIT
For /f "tokens=1,* delims=#" %%a in (" %*") Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF

The label :EOF does not need to be declared as it is the standard return point for NT sequences.

To play the script type Split File_Name, assuming the batch's name is Split.bat.





#7
+1
ebrian November 3, 2005 at 03:42:16 Pacific

Thanks for your help IVO. The script does split the lines, however it removes the # symbol.



#8
+3
IVO November 3, 2005 at 07:55:38 Pacific

OK, this sligthly modified version saves the separator; I used the character @, but you can choose any other under the condition that is not a symbol typed in the text.

To save the output to a new file type

Split File_Name > File_Out

@Echo Off

For /f "tokens=* delims=" %%a in (%*) Do (
Call :SPLIT %%a)
GoTo :EOF

:SPLIT
Set Line=%*
Set Line=%Line:#=#@%
For /f "tokens=1,* delims=@" %%a in ('Echo %Line%') Do (
Echo.%%a
If not "%%b"=="" Call :SPLIT %%b)
GoTo :EOF



#9
+1
ebrian November 3, 2005 at 09:50:33 Pacific

Thanks IVO, your the master at this stuff. I could follow your first iteration of this, but I don't understand:

Set Line=%*
Set Line=%Line:#=#@%

in your final version.



#10
+1
Mechanix2Go November 3, 2005 at 09:54:48 Pacific

Yep, IVO's the man.

I don't understand any of it but I'm muddling along.


If at first you don't succeed, you're about average.

M2



#11
+4
IVO November 3, 2005 at 12:07:37 Pacific

That's the trick to retain the # separator, i.e. to suffix it with a new exot(er)ic character to use as marker in place of the native one. So each occurence of # is replaced by #@ (@ as the new symbol) and then the split is performed by looking for @, as the separator itself is lost in parsing.

Set Line=%*

assigns to the environment variable Line the string to be parsed and

Set Line=%Line:#=#@%

replaces each occurence of # with #@

Obviously to work the appended character needs to have a special meaning in the text context, i.e. it must not be used elsewhere in the body of the file. That's the reason why I choose a character like @, but other good choices may be % $ £ § ç according to your country.

I hope my explanation helps.



#12
+3
ebrian November 3, 2005 at 14:18:03 Pacific

Now it makes sense.

Thanks for taking the time to explain the code. You're definitely an asset to the forum !!



#13
+2
FishMonger November 4, 2005 at 14:08:40 Pacific

Just for comparison, here's a perl command that accomplishes the same thing more efficiently.

perl -pi.bak -e "s/#/#\n/g;" file.txt



#14
+2
Jiriki January 6, 2006 at 07:09:04 Pacific

Here's a neat trick. Right-click on My Computer | Properties | Advanced tab | Environment Variables button. Under System Variables, click New.

In the Variable name type something like CR. Make sure NumLock is on, in the Variable value field, press and hold the ALT key, now press and release the 1 key on the number pad and then the 0 key on the number pad, now release the ALT key. You should get a Square. Click OK.

Now in your batch file whenever you want a carriage return, use %CR%. If you write to a file, notepad can't interperet this ASCII, but if you open in Write, Word or other word processor, it will be a carriage return. Standard out to console will interpert it as well as Net Send command.



Start New Discussion Reply to Message Icon
« Spaces in ANSI string cal... FTP Server Programming »

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC


Results for: Insert carriage return w/ batch

Insert carriage return at column? www.computing.net/answers/programming/insert-carriage-return-at-column/17254.html

Eval reg entry w/carriage returns www.computing.net/answers/programming/eval-reg-entry-wcarriage-returns/15573.html

insert a carriage return into a txt file www.computing.net/answers/programming/insert-a-carriage-return-into-a-txt-file/21856.html