Computing.Net > Forums > Programming > Insert carriage return at column?

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 at column?

Reply to Message Icon

Name: Linuszz
Date: November 13, 2008 at 15:09:54 Pacific
OS: 200 Server
CPU/Ram: 4 GB
Comment:

Hello. I'm trying to write a bat script to modify a text file by inserting a carriage return at the 80th character of any line. e.g. Some lines in my text file exceed 80 characters and if they do, I need that line wrapped. Any suggestions? Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: November 13, 2008 at 17:56:02 Pacific
Reply:

I think you'll want a CRLF pair. [0d0a hex]


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

M2


0

Response Number 2
Name: klint
Date: November 14, 2008 at 02:49:58 Pacific
Reply:

Does it have to be a batch script? It would be much, much easier if you use another language, suck as Awk or VBScript. Also, do you want to break on word boundaries, or just at the 80 column (thus splitting up words into two)?


0

Response Number 3
Name: IVO
Date: November 14, 2008 at 05:54:33 Pacific
Reply:

I got your message and here a working proposal, even if you would think about what klint posted. I break the line after the 79th character otherwise an extra space is displayed. By the way there is no CTRL /F command, you have to insert CR/LF control characters, a job that is tricky in batch.

WARNING: the script works if the text file does not contain ! chars.

@echo off & setlocal EnableDelayedExpansion > MyFile.new
for /F "tokens=1* delims=]" %%j in ('find /N /V "" ^< MyFile.txt') do (
set lineEx=
set line=%%k
if defined line (
if not "!line:~79,1!"=="" (
set lineEx=!line:~79!
set line=!line:~0,79!
)
echo.!line!>> MyFile.new
if defined lineEx echo.!lineEx!>> Myfile.new
) else (
echo.>> MyFile.new
)
)
:: [End_Of_Batch]


0

Response Number 4
Name: Linuszz
Date: November 14, 2008 at 06:05:19 Pacific
Reply:

First off, thanks for any help!

I guess it isn't restricted to a batch script. The files are generated daily (less than 5 files per day) then a separate program is FTP'ing them to a 3rd party. I need something to step in and add the word wrapping before the FTP sends them out. It must enter the wrapping at exactly the 80th column. No concern for individual word integrity.

It is running on an win2000 server and the automation tool that is doing the FTP can easily execute a batch script. I'm not sure how I'd execute Awk or VBScript (e.g. do I need a runtime environment or anything?), but I'm open to ideas.

I started thinking about a batch script from this post which seems very close to my needs (#13548). I've just been struggling to adjust it correctly... (http://www.computing.net/answers/programming/insert-carriage-return-w-batch/13548.html)


0

Response Number 5
Name: Linuszz
Date: November 14, 2008 at 06:33:40 Pacific
Reply:

IVO, thanks! works great. FYI, I modified it slightly to allow me to dynamically input the file name to be modified (below). Thanks again!

@echo off & setlocal EnableDelayedExpansion > %*.new
for /F "tokens=1* delims=]" %%j in ('find /N /V "" ^< %*') do (
set lineEx=
set line=%%k
if defined line (
if not "!line:~79,1!"=="" (
set lineEx=!line:~79!
set line=!line:~0,79!
)
echo.!line!>> %*.new
if defined lineEx echo.!lineEx!>> %*.new
) else (
echo.>> %*.new
)
)
:: [End_Of_Batch]


0

Related Posts

See More



Response Number 6
Name: klint
Date: November 14, 2008 at 06:34:24 Pacific
Reply:

Ivo, your batch script looks good. But it only works for lines up to 160 characters long. Those that are 300 characters long will be split into a line of 80 characters and one of 220 characters. Instead, it should be split into four lines, 80, 80, 80 and 60.


0

Response Number 7
Name: Linuszz
Date: November 14, 2008 at 06:49:46 Pacific
Reply:

Klint, good point. In this case however it isn't a concern. The lines won't ever get that long. 107 chars is going to be the max length. Thanks again everyone.


0

Response Number 8
Name: IVO
Date: November 14, 2008 at 06:50:01 Pacific
Reply:

To klint,

you are right as always, indeed I posted the script as I have already coded it, but I doubted it was effective. It requires more work to be robust and viable for all seasons. I was giving up when the OP said he accepted VBS or awk, but it seems this raw code did the job. If not, better to try using VBS. Often people is really looking for solutions more immediate than we suppose.


0

Response Number 9
Name: IVO
Date: November 14, 2008 at 06:58:57 Pacific
Reply:

To linuszz,

if your filenames have embedded blanks, better you replace in the script %* with "%*" and "%*.new", i.e embraced by double quotes.


0

Response Number 10
Name: Mechanix2Go
Date: November 29, 2008 at 08:41:54 Pacific
Reply:

Hi IVO,

For real long lines you could process a file multiple times?


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

M2


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 at column?

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

Insert carriage return w/ batch www.computing.net/answers/programming/insert-carriage-return-w-batch/13548.html

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