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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Moving cursor inside open TEXT file

Status
Not open for further replies.
Apr 23, 2002
39
US
This may be a stupid question, but I can't find the answer. I have a text file open, I'm extracting portions of the data in the file for import into a table. How can I move within the file. For example, I'm at the end of a page and I need to go back to the top of the page, or perhaps up 5 lines.

Thanks,
Don in Phoenix[shadeshappy]
 
BTW, using VBA, if I wasn't clear.

Open myFILE For Input As #1
Line Input #1, InputData

Do Until EOF(1)
'need stuff here
Loop
 
How are ya PhoenixDon . . .

It may be better for you to open the file in an editor and cut/paste as necessary to make your new file. There are so many formatting characters to get around let alone the many ways different software implement these characters (no standard!). If there's alot of files involved you may have to convert them to text (no formatting characters) to make things easier on yourself . . .

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
These text files are documents that I have to find a way to split apart. Typically they may be anywhere from 6 to 600 pages, with each document containing 6-9 pages, and they go to different people. A program we use currently converts them to a WORD doc, then emails it to one office. I want to split them apart into individual files. I can either work with the raw TEXT file, or the WORD version, but because of the numbers I'm talking about, it has to be automated.
 
I think I would first load the entire file into something that's easier to work with - a database table for example.

Another option is use Word automation to read through the Word document, as there are collections of Paragraphs, Lines, Words, etc. that you can reference by index position.

 
If you can't load the document into a table, and the text file is too big to load into memory, you could also use the Binary form of Line Input. You can then move anywhere in the file, but you will have to find end-of-line markers (vbCrLf) using instr(), etc.

Something like this:

Public Function ConvertCRLFtoLF(ByVal FileToConvert$) As Boolean
Dim Char As String * 1
Dim CharPlus1 As String * 1
Dim ChannelIn As Integer
Dim ChannelOut As Integer
Dim FileMaxChars As Long
Dim FilePosIn As Long
Dim FilePosOut As Long
Dim FileTemp$
Dim I As Integer
Dim Iplus1 As Integer
'Dim CR$
Dim SaveFound$
Dim SavePanel$
FileMaxChars = FileLen(FileToConvert$)
FileTemp$ = StripFile(FileToConvert$) & "junkCRLF.tmp" ' Final file is overwritten onto original.
ChannelIn = FreeFile
Open FileToConvert$ For Binary Access Read As #ChannelIn
ChannelOut = FreeFile
Open FileTemp$ For Binary Access Write As #ChannelOut


FilePosIn = 0
Do Until EOF(ChannelIn)
FilePosIn = FilePosIn + 1
Get #ChannelIn, FilePosIn, Char
I = Asc(Char)
If I <> 13 Then ' Found Carriage Return, strip it
FilePosOut = FilePosOut + 1
Put #ChannelOut, FilePosOut, Char
Else
Get #ChannelIn, FilePosIn + 1, CharPlus1
Iplus1 = Asc(CharPlus1)
If Iplus1 = 10 Then
FilePosOut = FilePosOut + 1
Put #ChannelOut, FilePosOut, CharPlus1
FilePosIn = FilePosIn + 1
Else
FilePosOut = FilePosOut + 1
Put #ChannelOut, FilePosOut, Char
End If
End If
Loop

Close #ChannelIn
Close #ChannelOut
DoEvents

Kill FileToConvert$
Name FileTemp$ As FileToConvert$
ConvertCRLFtoLF = True
End Function

Note that this example reads one character at a time, which is the SLOWEST way to do this. It's much better to read blocks of, say, 1000 characters at a time if you're interested in speed.
 
PhoenixDon . . .

There's alot for you to think about here. First, Word Docs are a bad choice, particularly as far as parsing is concerned. Consider the following text . . .

[blue]Now is the time for

all good men

to come to

the aid of

their countrymen![/blue]

[purple] . . . generates a 19K .doc file![/purple] Where as a text file .txt generates 1K with actual character count of 80.
PhoenixDon said:
[blue]These text files are documents that I have to find a way to split apart.[/blue]
Split in what way?

Are you saying they may contain [blue]multiple docs[/blue] hence the splitting? . . .

[blue]If the above is true how can you tell seperate docs just by looking at one file?[/blue] (this excites search criteria). How to differentiate one set of text meant to be a file from another is the key here. This remains hidden at present and is obviously what your looking for.

Any clues! . . . perhaps you can post a small file (as small as possible) or post an url where we can view one! . . .

Calvin.gif
See Ya! . . . . . .
 
PhoenixDon,
Here is a shot out of left field. Start with a routine similar what Kotaro24 suggested, just create a new file with all the carriage return ([tt]vbCrLf[/tt]) characters replaced with say Bars ([tt]Chr(124)[/tt]).

Once you have the file in a new format you could use the Link Table Wizard to link to the file, choose delimited and set the Bar as your field delimiter. This should give you a linked table with one field and a record for each line.

Now you navigate through the lines (records) in the file using record operations and could then write a routine output the records (lines) to a file in any combination you want. Just be sure to insert a Carriage Return between each record (line) when you reconstruct the files.

All this would be much easier if you could just specify a Carriage Return as a field delimiter, but then you would have a document and not a dataset:)


Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT+08:00) Singapore
 
The signature says it all...
CautionMP said:
For the best results do what I'm thinking, not what I'm saying.
...I was thinking replace the Carriage Return with a Bar Carriage Return ([tt]Chr(124) & vbCrLf[/tt]) and then remove the Bar when writting the file.

CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT+08:00) Singapore
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top