lionne
04-29-2013, 05:24 AM
hi there,
The task is to extract a special string from .log files in one folder. The number of such .log files is > 4k.
I faced the problem with reading the files. Here's the code snippet:
Private Sub cmb_Import_Click()
Dim PathSelected As String
Dim Folder As Object
Dim iFileNum As Long
Dim objFso As Object
Dim objFiles As Object
Dim objFile As Object
Set Folder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder:", 0)
PathSelected = Folder.self.Path
strExt = "log"
iFileNum = FreeFile()
LineNum = 0
Row = 3
'Set Error Handling
'On Error GoTo EarlyExit
'Create objects to get a count of files in the directory
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(PathSelected).Files
'Count files (that match the extension if provided)
For Each objFile In objFiles
If Right(objFile.Name, 3) = strExt Then 'open only .log Files
'open the file to read lines
Open objFile For Input As iFileNum
'loop through the lines
Do While Not EOF(iFileNum)
'... omitted as not relevant for the problem
Loop
Close iFileNum
iFileNum = iFileNum + 1
End If
Next objFile
EarlyExit:
'Clean up
On Error Resume Next
Set objFiles = Nothing
Set objFso = Nothing
Set objFile = Nothing
On Error GoTo 0
End Sub
The problem is, when iFileNum reaches 513 it crashes with the following error: "Runtime error 52: File name or file number is wrong."
Is it true, that Excel is not able to open the 513th file in the same run?
Any hints are as always highly appreciated..
The task is to extract a special string from .log files in one folder. The number of such .log files is > 4k.
I faced the problem with reading the files. Here's the code snippet:
Private Sub cmb_Import_Click()
Dim PathSelected As String
Dim Folder As Object
Dim iFileNum As Long
Dim objFso As Object
Dim objFiles As Object
Dim objFile As Object
Set Folder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder:", 0)
PathSelected = Folder.self.Path
strExt = "log"
iFileNum = FreeFile()
LineNum = 0
Row = 3
'Set Error Handling
'On Error GoTo EarlyExit
'Create objects to get a count of files in the directory
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFiles = objFso.GetFolder(PathSelected).Files
'Count files (that match the extension if provided)
For Each objFile In objFiles
If Right(objFile.Name, 3) = strExt Then 'open only .log Files
'open the file to read lines
Open objFile For Input As iFileNum
'loop through the lines
Do While Not EOF(iFileNum)
'... omitted as not relevant for the problem
Loop
Close iFileNum
iFileNum = iFileNum + 1
End If
Next objFile
EarlyExit:
'Clean up
On Error Resume Next
Set objFiles = Nothing
Set objFso = Nothing
Set objFile = Nothing
On Error GoTo 0
End Sub
The problem is, when iFileNum reaches 513 it crashes with the following error: "Runtime error 52: File name or file number is wrong."
Is it true, that Excel is not able to open the 513th file in the same run?
Any hints are as always highly appreciated..