|
-
Jun 15th, 2011, 12:48 PM
#1
Thread Starter
Lively Member
[RESOLVED] Reading from a file
I have an app that reads a text file and places the data into a listview.
It was working fine. Now I have added a column to the listview and there is more information in the saved file.
The file that I am loading into the listview will have a date followed by a tab and then a filepath. For example:
12/25/2001 C:\Myfolder\myfile.doc
That line needs to be placed in a listview, although the columns are reversed. Column 1 is the path and column 2 is the date.
Code:
Private Sub cmdLoad_Click()
Dim FileNum As Integer
Dim RecordCount As Long
Dim strFilePath, MyFile As String
Dim dteDate As Date
'Clear listbox
'List1.Clear
ListView1.ListItems.Clear
FileNum = FreeFile
RecordCount = 0
dlgLoadFile.DialogTitle = "Load a text file"
dlgLoadFile.Filter = "Text Files (*.txt)*.txt"
dlgLoadFile.ShowOpen
If Len(dlgLoadFile.FileName) <> 0 Then
MyFile = dlgLoadFile.FileName
Open dlgLoadFile.FileName For Input As #FileNum
Do Until EOF(FileNum)
Input #FileNum, strFilePath
'Get file type of first path
If RecordCount = 0 Then
GlobalFileType = "*." & Right(strFilePath, 3)
RecordCount = RecordCount + 1
Else
If strFilePath = "" Then
'Do nothing
Else
If Right(strFilePath, 3) <> Right(GlobalFileType, 3) Then
MsgBox "The file contains filenames with different extensions." & vbCrLf & _
"Make sure all filenames listed in the text file have the same extension.", _
vbCritical, "Error"
'List1.Clear
ListView1.ListItems.Clear
Exit Sub
Else
RecordCount = RecordCount + 1
End If
End If
End If
'List1.AddItem strFilePath
ListView1.ListItems.Add strFilePath
Loop
Close #FileNum
'Enable Check Links Button
cmdCheckLinks.Enabled = True
StatusBar1.Panels(3).Text = "File loaded."
Else
'Do nothing
End If
End Sub
-
Jun 16th, 2011, 02:56 PM
#2
Re: Reading from a file
Not too sure what you're asking for but, adding records from a file (in the format you described) to a Listview can be done with something like this
Code:
Option Explicit
Private Sub cmdLoad_Click()
Dim intFile As Integer
Dim intI As Integer
Dim strData As String
Dim strRecords() As String
Dim strFields() As String
Dim lvItem As ListItem
dlgLoadFile.DialogTitle = "Load a text file"
dlgLoadFile.Filter = "Text Files (*.txt)*.txt"
dlgLoadFile.ShowOpen
If Len(dlgLoadFile.FileName) <> 0 Then
intFile = FreeFile
'
' Open the file,read the contents and close it
'
Open dlgLoadFile.FileName For Input As intFile
strData = Input(LOF(intFile), intFile)
Close intFile
'
' Split into records
'
strRecords = Split(strData, vbCrLf)
'
' For each record split it into fields
'(Assumes 2 field, delimitered by a Tab)
' and add to Listview
'
For intI = 0 To UBound(strRecords)
If strRecords(intI) <> vbNullString Then
strFields = Split(strRecords(intI), vbTab)
'
' Here you can check the Path / File etc
' (strFields(1) = Path and file
'
Set lvItem = ListView1.ListItems.Add(, , strFields(1))
lvItem.SubItems(1) = strFields(0)
End If
Next intI
End If
End Sub
Private Sub Form_Load()
ListView1.View = lvwReport
ListView1.ColumnHeaders.Add , , "Path", ListView1.Width / 2
ListView1.ColumnHeaders.Add , , "Date", ListView1.Width / 2
End Sub
-
Jun 16th, 2011, 03:50 PM
#3
Thread Starter
Lively Member
Re: Reading from a file
That works perfectly. Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|