tags:

views:

128

answers:

3

USING VB6

I want to search a file from the path.

For Example.

Path = "C:\Newfolder\" file name = *.txt, *.fin

I want to get all the *.txt, *.fin file from the newfloder.

Need VB6 code Help.

+1  A: 

Use the Scripting.FileSystemObject.

Call it with GetFolder("C:\Newfolder"), then loop through the files in that folder with the .Files property and filter them on extensions using the GetExtensionName method.

For example:

Dim fso as Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim f as Object
For Each f in fso.GetFolder("<folderpath>").Files
   If fso.GetExtensionName(f.Path) = "txt" Then 'or maybe it's .txt, I'm not sure
      ' also test for 'fin'
      '... do stuff
   End If
Next f
Eric Minkes
Getting Error as object not support
Gopal
At what point? At the CreateObject line? If so, make sure that the scripting runtime is installed (should be the case on most systems, what OS are you using?)
Eric Minkes
xp Service pack2
Gopal
should be alright. Maybe use Dim f as Object? I'll edit my answer.
Eric Minkes
you need to do fso.GetFolder("path").Files in the for each.
Jeff Widmer
Ah, thanks. Stupid mistake. Answer has been edited.
Eric Minkes
You should use a reference rather than use CreateObject it under Microsoft Scripting Runtime
RS Conley
A: 

Check out the Scripting.FileSystemObject.
In your project add a reference to the "Microsoft Scripting Runtime".

Then you can do something like this:

Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject

Dim fld As Scripting.Folder
Set fld = fso.GetFolder("d:\temp\newfolder")

Dim i As Integer
Dim ext As String
Dim fl As Scripting.File
For Each fl In fld.files
    'get extension
    ext = Mid(fl.Name, Len(fl.Name) - 2)
    If ext = "txt" Or ext = "fin" Then
        'do something with the file
    End If
Next fl

This is one of the areas that got so much better with .NET.

Jeff Widmer
You should probably use GetExtensionName to get the extension of the file.
Eric Minkes
Forgot about the fso.GetExtensionName method (see deathofrats answer). You can probably use that instead of what i did.
Jeff Widmer
A: 

Just drop-in a CDirDrill class that does it all for you, in full native VB6. Another excellent solution from Karl Peterson :)

The FileSystemObject is not recommended for a couple of reasons: one, it adds a dependency, and two, it depends on scripting, which may be disabled per policy. I've had bugs because some customer has managed to muck up scrrun.dll on their PC. Eliminate dependencies unless they're really helping you a lot.

BTW this question is a duplicate of this

MarkJ