Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

batch program to fix text file 1

Status
Not open for further replies.

maximas

Programmer
Nov 29, 2002
40
US
hello:
I have a text file will continuous line without any line breaks with a box as a exclamation as a divider, ie. abcde!abced!abcde. Could I write a batch file that will open the text file and then loop thru the line and if there is an exclamation put a line break there? Any help will be great. Thank you.
 
it's a continuous line, ie. one character after another. ie. 1234567890!908405802384502!812380481083!9080890832323 where the exclamation mark is one record. I want to use the exclamation mark as the line break. how can I do that in a batch file.
p.s about 2000 records in a line, but since notepad only display to some point, it wrap around.
 
Here is a GWBASIC program that does what you need.
[blue]
Code:
100 DEFINT A-Z
110 DIM A$(4000)
120 OPEN "banger.asc" FOR RANDOM ACCESS READ AS #1 LEN=1
130 OPEN "banger.txt" FOR OUTPUT AS #2
140 FIELD 1,1 AS INBUFFER$
150 WHILE NOT EOF(1)
160   R=R+1
170   GET #1,R
180   IF INBUFFER$="!" THEN GOSUB 1000 ELSE GOSUB 2000
190 WEND
200 CLOSE
210 END
1000 'Subroutine to write a record to the output file
1010 J = 0
1020 WHILE I > 1
1030   PRINT #2,A$(J);
1040   I = I - 1
1050   J = J + 1
1060 WEND
1070 PRINT #2,A$(J)
1080 I = 0
1090 RETURN
2000 'Subroutine to update current array with next character
2010 A$(I)=INBUFFER$
2020 I=I+1
2030 RETURN
[/color]

 
Interesting approach, Skip. But Word appears to insert additional line breaks when loading the file. Close, but no cigar.

The main difficulty seems to be the 2,000 character size of the lines. (No problem with my GWBASIC approach, as I used an array.)
 
It's not the number of characters. It's the number of characters between the exclamation points.

Also, I used Word 97. The newer versions may behave a little better for this type of file.
 
thanks Zathras!
I'll try it out! post the result later!
 
HEY Zathras:

The script you gave me is not a batch script, it's a cobol script. and it doesn't work. I need a batch script, that I can run with a single click without any software. Here an idea! how about write a batch script that will open the text file in excel and run a macro that will parse out the delimiters, and then save the parsed file back into text format, so that it will eliminate the delimiters and then exit. That will work right.
 
...it's a cobol script...

No, it's a BASIC program.

...run with a single click without any software...

Windows is software.
Excel is software.
GWBASIC.EXE is software.

If the file names are always the same (hard-coded in the program), then you can simply create a shortcut with the command line
[blue]
Code:
  GWBASIC.EXE banger.bas
[/color]

And add this line to the program to automatically exit basic when finished:
[blue]
Code:
  205 SYSTEM
[/color]

Double-click the shortcut and the file is created.

If you don't have GWBASIC, you can easily find it on the web -- Google search : "download gwbasic.exe"
 
From any application that runs VBA, the following code will work:

Sub ConvertTextFile()
Dim s As String, q As Long, p As Long
Open "c:\long.txt" For Input As #1
Open "C:\tall.txt" For Output As #2
Input #1, s
Close #1
p = 0: q = 1
Do
p = InStr(p + 1, s, "!")
If p > 0 Then
Print #2, Mid(s, q, p - q - 1)
q = p + 1
End If
Loop Until p = 0
Close #1
Close #2
End Sub

You could put this in the workbook_open event for an otherwise empty excel workbook, and use it to launch the code.


Rob
[flowerface]
 
Oops - it just occurred to me that this ignores the very last record. Add an else to the if construct:

If p > 0 Then
Print #2, Mid(s, q, p - q - 1)
q = p + 1
else
print #2,mid(s,q)
End If



Rob
[flowerface]
 
maximas: Whichever way you go, be sure to test very thoroughly. When I ran Tony's routine, it seems to lose the last character of each record before the ! and the last record seems to show up twice (with or without the modification).
 
Zathras

I've used Skip's method for quite some time and didn't seem to have any problems.

I just tried with a string of 3000 characters "A" followed by one character "!" then 3000 more "A"'s followed by one character "!"

after replace ! with ^p I ended up with two lines as expected.

Using Word 2k now but I can remember doing this a few years ago with earlier version.

BTW nice tight code Rob (good to have you back)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top