Computing.Net > Forums > Programming > Insert carriage return w/ batch

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Insert carriage return w/ batch

Reply to Message Icon

Name: ebrian
Date: November 2, 2005 at 05:28:46 Pacific
OS: XP
CPU/Ram: 1GB
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: wizard-fred
Date: November 2, 2005 at 07:39:37 Pacific
Reply:

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.


0

Response Number 2
Name: ebrian
Date: November 2, 2005 at 07:50:31 Pacific
Reply:

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


0

Response Number 3
Name: djas
Date: November 2, 2005 at 09:01:23 Pacific
Reply:

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


0

Response Number 4
Name: IVO
Date: November 2, 2005 at 12:36:36 Pacific
Reply:

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.


0

Response Number 5
Name: ebrian
Date: November 2, 2005 at 15:43:00 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: IVO
Date: November 3, 2005 at 02:24:13 Pacific
Reply:

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.




0

Response Number 7
Name: ebrian
Date: November 3, 2005 at 03:42:16 Pacific
Reply:

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


0

Response Number 8
Name: IVO
Date: November 3, 2005 at 07:55:38 Pacific
Reply:

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


0

Response Number 9
Name: ebrian
Date: November 3, 2005 at 09:50:33 Pacific
Reply:

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.


0

Response Number 10
Name: Mechanix2Go
Date: November 3, 2005 at 09:54:48 Pacific
Reply:

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


0

Response Number 11
Name: IVO
Date: November 3, 2005 at 12:07:37 Pacific
Reply:

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.


0

Response Number 12
Name: ebrian
Date: November 3, 2005 at 14:18:03 Pacific
Reply:

Now it makes sense.

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


0

Response Number 13
Name: FishMonger
Date: November 4, 2005 at 14:08:40 Pacific
Reply:

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

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


0

Response Number 14
Name: Jiriki
Date: January 6, 2006 at 07:09:04 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


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

remove carriage return at file end www.computing.net/answers/programming/remove-carriage-return-at-file-end/17101.html