Split lines in .txt at 80 characters

Microsoft Windows xp professional w/serv...
February 22, 2010 at 07:16:40
Specs: Windows XP
Hi.
I have a batch file which writes flat files of 320 bytes/characters, and then merge them together.
The next step I need to do is a line break at 80 bytes/characters, but I can't figure out how. Anyone know how to do this?

Thanks!


See More: Split lines in .txt at 80 characters

Report •


#1
February 22, 2010 at 09:15:55
not tested:
@echo off & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (file) do (
set out=%%a
for /L %%b in (0 80 320) do
set xx=%%b
call :aa
)
)
echo examining output file:
more ofile
goto :eof

:aa
>> ofile echo !out:~%xx%,80!


Report •

#2
February 22, 2010 at 12:26:07
Hi.
Should I pass filename as argument, or replace "file" with file name? I tested both, and they both came out as
D:\software\xmlconvert>test.bat
The syntax of the command is incorrect.

Any clues? :)


Report •

#3
February 22, 2010 at 13:44:58
nbrane: "tokens=*"
"usebackq delims=" would be better.

hjornevik: Should I pass filename as argument, or replace "file" with file name?
The latter.
Any clues? :)
Leave echo on, and let's see the output leading to the error.


Report •

Related Solutions

#4
February 22, 2010 at 14:47:54
sorry, that's what happens when i try to work in the morning.
(and sans coffee!). I left off a parenthesis!

@echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("file") do (
set out=%%a
:: the last par. was left off in original
for /L %%b in (0 80 320) do (
set xx=%%b
call :aa
)
)

my apologies.


Report •

#5
February 23, 2010 at 00:58:43
nbrane; thank you so much! Brilliant script, and if I had any skills myself, I probably could've found that missing parentheses, but I don't... :)
Also thanks to razor2.3 :)

I need to request a few more mods and improvements before I can use it fully though, and I hope someone can help me out.

The script seems to add "Echo is off" to the output after each 320 byte, except the last.
The last 320 byte record get "Echo off" in the middle of it (the record has a lot of spaces), and also some strange symbols (and a couple of lines) at the end.

Does this make any sense?


Report •

#6
February 23, 2010 at 02:19:28
There's also a unix-like command, don't know if I got it from MSRK (Microsoft Resource Kit), but behold the ease of use:

D:\Temp>fmt -w 6 test.txt
This
is a
test

D:\Temp>type test.txt
This is a test

D:\Temp>


Report •

#7
February 23, 2010 at 04:41:23
tvc:
looks nice, but unfortunatly I'm at office computer with restricted rights, so I'm not able to install/use MSRK... :/

Report •

#8
February 23, 2010 at 07:58:26
sorry again... i should have tested
change 320 to 240 in the for /L loop, should fix my logic error.
the end-of-file thing is more problematic:
is the last line 320 bytes? or shorter?
if it's 320 long, what kind of content? can you post:
1) the content of last line
2) the output of the script (last line only)
it might be a matter of a truncated last line.

Report •

#9
February 23, 2010 at 09:00:16
Hi again (or should I start with "thanks again"). You got rid of the "Echo is off" for the first records, so now only the end of file is bad... :))

This is the last line (with an extra character/square, not sure where that comes from):

AH200TBIU0217000001TRANSKO8L123456789H04BETFOR9912345678900           0006      0222                   00007163                                                                                                                                                             163                         SOFTWARE------16BANK--08


And this is the same line after script:

AH200TBIU0217000001TRANSKO8L123456789H04BETFOR9912345678900           0006      
0222                   00007163                                                 
ECHO er av.
                            163                         SOFTWARE------16BANK--08
ECHO er av.

ECHO er av.
ECHO er av.
ECHO er av.
ECHO er av.
ECHO er av.
.

All lines are of equal length, but now I notice an additional square which I didn't before...


Report •

#10
February 23, 2010 at 09:10:44
Ok, so I tested, and found that the new line with the square cause most of the trouble.

If I remove the square manually, and run it again, I get:

AH200TBIU0217000001TRANSKO8L123456789H04BETFOR9912345678900           0006      
0222                   00007163                                                 
<b>ECHO er av.</b>
                            163                         SOFTWARE------16BANK--08
                            163                         SOFTWARE------16BANK--08
.

This is a bit weird also, as there is still one Echo-message remaining, and also the last 80 byte appear two times!! :)

I'm really thankfull you're helping me out, cause this is making me tired! :)


Report •

#11
February 23, 2010 at 09:26:24
You know what? Since nbrane doesn't seem to be around, I might as well give some answer.
Replace: >> ofile echo !out:~%xx%,80! with
>> ofile echo.!out:~%xx%,80!


Report •

#12
February 23, 2010 at 09:32:29
add the dot after echo in sub :aa, as shown:

:aa
>> ofile echo.!out:~%xx%,80!

(i thought i fixed that in prev. post, but i overlooked it)
you seem to have a 1-byte eof (chr. 26) on a line by itself at end of file.
the dot (after echo) will prevent the garbagey stuff, but you'll still get the "square" (chr.26) foll. by 3 blank lines unless we add some testing. this might work (i know this is getting kind of klunky, but maybe):
for /f "usebackq delims=" %%a in ("file") do (
set out=%%a
:: add these two lines
set test=!out:~1!
if "!test!" equ "" goto :eof
:: ... (rest of script)


Report •

#13
February 23, 2010 at 09:34:55
Hi Razor.
I was reading a post where you and nbrane talked about that part before, so I tried a couple of variants:

>> 3_output1\out80.dat echo.!out:~%xx%,80! gives

out:~0,80
out:~80,80
out:~160,80
out:~240,80
out:~0,80
...

>> 3_output1\out80.dat echo.!out:~%xx%,80! gives
an extra space in the beginning of each line, as well as a couple of blank lines at the end...

Thanks though :)


Report •

#14
February 23, 2010 at 09:44:07
>> 3_output1\out80.dat echo.!out:~%xx%,80! gives
>> 3_output1\out80.dat echo.!out:~%xx%,80! gives
I find this hilarious, but I can't reproduce the bug you're seeing. Also, both lines are the same. Finally, it looks like your data isn't ASCII, with EoL's denoted by '\n\r'. You might want to try a better scripting language for text parsing.

Report •

#15
February 23, 2010 at 09:50:28
I'm sorry, I was suppose to exchange the . with a space for last line:
>> 3_output1\out80.dat echo.!out:~%xx%,80! gives...
>> 3_output1\out80.dat echo !out:~%xx%,80! gives...

This is frustrating... I'm going to use this to generate some flat test data files for mainframe testing.
The data will be stored in xml, and converted to 320 byte lines in flatfile, which I need to break up to 80 bytes for the mainframe to read it... Puh... :)


Report •

#16
February 23, 2010 at 09:59:19
@echo off
setlocal enabledelayedexpansion
for /f "usebackq delims=" %%a in ("file") do (
set out=%%a
set test=!out:~1!
if "!test!" equ "" goto :eof
for /L %%b in (0 80 240) do (
set xx=%%b
call :aa
)
)
goto :eof
:aa
>> ofile echo.!out:~%xx%,80!

but i have no clue where:
ECHO er av.
is coming from!



Report •

#17
February 23, 2010 at 10:09:57
I found the bug which created the char23.

There is only one "Echo er av" message now, and it appears at the one single line which has only spaces. Does this make the line appear blank or something? It has 80 characters, but all blank...
If this doesn't make any new clues come to (either of your) mind(s) immediatly, don't bother any more :)) I will go through your other posts and replace the last "Echo er av" with spaces.

Thanks for all your effort though, I'm going home from work now... If I only were allowed to have Ruby or something here :)


Report •

#18
February 23, 2010 at 10:17:04
The spaces are the trouble. If I replace them with 1's, the file comes out perfect:
AH200TBIU0217000001TRANSKO8L123456789H04BETFOR9912345678900           0006      
0222                   000071631111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111111111163                         SOFTWARE------16BANK--08
.

Unfortunatly, they have to be spaces :)


Report •

#19
February 23, 2010 at 11:05:40
You know what? I'm just going to post a VBScript and call this done.
Const inFileName  = "infile"
Const outFileName = "3_output1\out80.dat"

Set fso = CreateObject("Scripting.FileSystemObject")
inText = fso.OpenTextFile(inFileName).ReadAll
With fso.OpenTextFile(outFileName, 2, True)
  For i = 1 To 241 Step 80
    .WriteLine Mid(inText, i, 80)
  Next 'i
End With

And remember, every language is better than batch. Even VBScript.

Report •

Ask Question