[RESOLVED] pipe files in a folder to a text file
I have a dbase database with a bunch of data tables in a folder. I need to get all of the files in that folder that start with "EN" and end with ".DBF" into a text file. There are a few hundred, otherwise I'd do it manually. I've search the forums but haven't found what I'm looking for and my VB is a little rusty in file handling.
Anyone have a nice snippet to get me close?
EDIT: I should add that I found a FileSystemObject example that should work, but I don't remember the trick to getting the FSO to work. It keeps telling me the user-defined type "Dim FSys As FileSystemObject" is not defined.
Re: pipe files in a folder to a text file
Quote:
Originally Posted by ober0330
I have a dbase database with a bunch of data tables in a folder. I need to get all of the files in that folder that start with "EN" and end with ".DBF" into a text file. There are a few hundred, otherwise I'd do it manually. I've search the forums but haven't found what I'm looking for and my VB is a little rusty in file handling.
Anyone have a nice snippet to get me close?
EDIT: I should add that I found a FileSystemObject example that should work, but I don't remember the trick to getting the FSO to work. It keeps telling me the user-defined type "Dim FSys As FileSystemObject" is not defined.
To use the FSO, click Projects/References and find Microsoft Scripting Runtime. Check it, and click Ok.
Re: pipe files in a folder to a text file
Yeah, I found that finally. Now the stupid script doesn't seem to be finding the files.
VB Code:
Public FSys As FileSystemObject
'Constant Value Description for FileAttributes
'Normal 0 Normal file. No attributes are set.
'ReadOnly 1 Read-only file. Attribute is read/write.
'Hidden 2 Hidden file. Attribute is read/write.
'System 4 System file. Attribute is read/write.
'Volume 8 Disk drive volume label. Attribute is read-only.
'Directory 16 Folder or directory. Attribute is read-only.
'Archive 32 File has changed since last backup. Attribute is read/write.
'Alias 64 Link or shortcut. Attribute is read-only.
'Compressed 128 Compressed file. Attribute is read-only.
Function ScanFolder(FolderSpec As String, SearchStr As String) As String
Dim thisFolder As Folder
Dim allFolders As Folders
Dim thisFile As File
Dim allFiles As Files
Dim nfile As Integer
Dim i As Integer
Dim x As Integer
Set thisFolder = FSys.GetFolder(FolderSpec)
Set allFolders = thisFolder.SubFolders
nfile = FreeFile
For Each thisFolder In allFolders
' List1.AddItem thisFolder.Path
If (thisFolder.Attributes And Hidden) <> Hidden Then ' Leave hidden directories alone
Set allFiles = thisFolder.Files
If allFiles.Count > 0 Then
Open App.Path & "\engines.txt" For Output As #nfile
For Each thisFile In allFiles
If Right(thisFile.Name, 4) = SearchStr Then
Print #nfile, thisFile.Name
End If
Next
Close #nfile
End If
Set allFiles = Nothing
Call ScanFolder(thisFolder.Path, SearchStr)
End If
DoEvents
Next
Set thisFolder = Nothing
Set allFolders = Nothing
Exit Function
End Function
Private Sub Command1_Click()
MousePointer = 11
Call ScanFolder("C:\Documents and Settings\ut9950h\Desktop", ".DBF")
MousePointer = 1
MsgBox "ScanFolders Complete - "
End Sub
Private Sub Form_Load()
Set FSys = New FileSystemObject
End Sub
Any ideas? One of the folders on my desktop is the folder that contains all the DBF files (as well as a bunch of other crap).
Re: pipe files in a folder to a text file
Is it finding anything?
Have you put a break on it and 'walked' through the code to see what it is actually doing?
Re: pipe files in a folder to a text file
Yeah, nevermind. I got it. Just have to chop off the extension now, but I can handle that:
VB Code:
Function ScanFolder(FolderSpec As String, SearchStr As String) As String
Dim thisFolder As Folder
Dim thisFile As File
Dim allFiles As Files
Dim nfile As Integer
Set thisFolder = FSys.GetFolder(FolderSpec)
nfile = FreeFile
Set allFiles = thisFolder.Files
If allFiles.Count > 0 Then
Open App.Path & "\engines.txt" For Output As #nfile
For Each thisFile In allFiles
If Right(thisFile.Name, 4) = SearchStr Then
If Mid(thisFile.Name, 1, 2) = "EN" Then
Print #nfile, Mid(thisFile.Name, 2)
End If
End If
Next
Close #nfile
End If
Set allFiles = Nothing
DoEvents
Set thisFolder = Nothing
End Function