Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 135,999 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,393 people online right now. Registration is fast and FREE... Join Now!




Reading From A Text File

 
Reply to this topicStart new topic

Reading From A Text File, reading into Excel

vbDALIN
27 Jun, 2002 - 05:23 PM
Post #1

New D.I.C Head
*

Joined: 27 Jun, 2002
Posts: 22

I know I can use the OpenAsTextStream, but I only need a certain part of the text file. Is there a way I can search for a certain string, copy it, and also have it copy a certain number of lines above that string. What I mean is, is there a way to tell it to move from word to word separated by spaces?
User is offlineProfile CardPM
+Quote Post

Dr. Tim
RE: Reading From A Text File
27 Jun, 2002 - 05:31 PM
Post #2

D.I.C Addict
****

Joined: 20 Mar, 2002
Posts: 993

I know just the guy you need to talk to about that...

Instant message "A g a z e u s" he can always help w/ my VB problems...
User is offlineProfile CardPM
+Quote Post

S8NSSON
RE: Reading From A Text File
28 Jun, 2002 - 06:57 AM
Post #3

New D.I.C Head
*

Joined: 22 Jun, 2002
Posts: 13

Dim LineHolder As String 'Variable to hold each line
Dim FFile As Variant 'Variant to hold next available file number
FFile = FreeFile 'Set next available file number

Open "TESTFILE" For Input As #FFile ' Open file.
Do While Not EOF(FFile) ' Loop until end of file.
Line Input #FFile, LineHolder ' Read line into variable.

'Search for string, or whatever, here

Loop
Close #1 ' Close file.

Hope this helps

Keith Charron
User is offlineProfile CardPM
+Quote Post

S8NSSON
RE: Reading From A Text File
28 Jun, 2002 - 08:46 AM
Post #4

New D.I.C Head
*

Joined: 22 Jun, 2002
Posts: 13

I forgot...
In the line: Open "TESTFILE" For Input As #FFile ' Open file.
"TESTFILE" should be the file to open, or a variable holding the name and path.

Example:
Dim FFile as Variant
Dim FileName as String
FFile = FreeFile
FileName = "C:\myfile.txt"
Open FileName For Input As #FFile

<other code and stuff here>

Close #FFile 'This line is also wrong in the above example...DOH!!!
User is offlineProfile CardPM
+Quote Post

vbDALIN
RE: Reading From A Text File
28 Jun, 2002 - 07:51 PM
Post #5

New D.I.C Head
*

Joined: 27 Jun, 2002
Posts: 22

thanx alot, that was exactly what I was looking for. And I'd be able to do this in a loop for numerous files, using a string array for the names of each. My question concerning that is being able to get all the filenames in a specific directory. I tried using this:

fileNames(0) = Dir(myPath & "*.txt")

While i = 1 To 30
fileNames(i) = Dir
Next

Because the first array element (if that's the word) gets the first text file from the directory, I expected that using the Dir function without parameters to get the following files, but it only gets the second. What am I doing wrong?
User is offlineProfile CardPM
+Quote Post

S8NSSON
RE: Reading From A Text File
29 Jun, 2002 - 04:45 PM
Post #6

New D.I.C Head
*

Joined: 22 Jun, 2002
Posts: 13

Why not just use a FileListBox and run through all the items in it after you set it to the desired path?
User is offlineProfile CardPM
+Quote Post

vbDALIN
RE: Reading From A Text File
29 Jun, 2002 - 08:03 PM
Post #7

New D.I.C Head
*

Joined: 27 Jun, 2002
Posts: 22

I'll check into FileListBox, never used it before though. The thing is I wont know how many files there are, or the names of them at the time the script is run, does that change anything?
User is offlineProfile CardPM
+Quote Post

S8NSSON
RE: Reading From A Text File
30 Jun, 2002 - 03:23 AM
Post #8

New D.I.C Head
*

Joined: 22 Jun, 2002
Posts: 13

In VB, if you drop a FileListBox on a form and set its path to wherever you desire, it will list all the files in that directory. Could be hundreds, doesn't matter.

The FileListBox is merely an extended ListBox, so you just treat it as such.

To look at every file in a given path with the FileListBox do as follows:

Dim iLIndex As Integer
For iLIndex = 0 To File1.ListCount - 1
Debug.Print File1.Path & "\" & File1.List(iLIndex)
Next x
User is offlineProfile CardPM
+Quote Post

vbDALIN
RE: Reading From A Text File
30 Jun, 2002 - 09:34 AM
Post #9

New D.I.C Head
*

Joined: 27 Jun, 2002
Posts: 22

thanx.that's exactly what I needed
User is offlineProfile CardPM
+Quote Post

vbDALIN
RE: Reading From A Text File
30 Jun, 2002 - 05:50 PM
Post #10

New D.I.C Head
*

Joined: 27 Jun, 2002
Posts: 22

Actually, I dont think the FileListBox is available in Visual Basic for Excel, but I found sample code that essentially does what I need. Only thing is that it's written for Access and I dont get some of the lines in it. It's written by Dev Ashish:

Public Sub FillFiles(stDir As String)
Dim stName As String

On Error GoTo err_FillFiles

'List all files in this folder
stName = Dir(stDir & "\*.*")
Do While stName <> ""
On Error Resume Next
If (GetAttr(stDir & stName) And vbDirectory) <> vbDirectory Then
'File Already open?
If Err.Number = 5 Then Err.Clear

If stName <> "." Or stName <> ".." Then
FileList.Add Item:=(stName)
End If
End If
'Get Next entry
stName = Dir
Loop

exit_FillFiles:
Exit Sub
err_FillFiles:
If Err.Number = 71 Then
MsgBox AccessError(Err.Number) _
& " Please try again. ", vbCritical + vbOKOnly, _
"Error Reading Drive " & stDir
End If
Resume exit_FillFiles
End Sub

could someone explain how this works?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 12:07PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month