-
I am having a hard time finding some information on how to get file names into strings.
What I want is to search a folder on my hard drive (ie...C:\programs\training\) to find all the files of a certain extension (ie... *.cor). As the files are found, I want to set the file name to a string variable array and count the amount of files with that extension.
Can anyone help me?
-
Here is a simple function that would extract the file name & extension from the file path that I use often:
Code:
Dim fName
dim fPath as string
fName = Split(fPath, "\")
List1.AddItem fName(UBound(fName))
Basically this takes the file path (fPath) and creates an array by splitting fPath at every backslash. Then just pull the upper bound (the last item in the array) and gives you to this name & extension.
-
Thanks, but will this go through all the files in the folder?
-
-
Care to explain a little more Frans C? I am fairly inexperienced.
I have found a work around, I am using a file list box. As long as I set the pattern and path, I get what I want.
-
May this will help :
Code:
Sub Main
Dim result as Long
result = extCounter("C:\Programs\Training\*.COR")
Msgbox "Count result : " & str(result)
End Sub
'-- File with specific Extension counter --
'-- Pattern will accept wildcards ( ? and * ) --
Function extCounter(Pattern as String) as long
Dim ctr as Long
Dim itm as String
ctr = 0 '-- Initialize counter --
itm = dir(Pattern, vbNormal) '-- Find first occurence --
Do Until itm = vbNullString
ctr = ctr + 1 '-- Increment counter --
itm = Dir() '-- Find next occurence --
Loop
extCounter = ctr '-- Return count --
End Function
I'm sure this will go thru all the files in the folder.
-
Here is another way:
Code:
Dim fs, fldr
Set fs = CreateObject("Scripting.FileSystemObject")
Set fldr = fs.GetFolder("c:\") 'gets the folder you want to search
Dim fle As File
For Each fle In fldr.Files
If Right(fle.Name, 3) = "bat" Then 'checks to see if it is the extension you want
List1.AddItem fle.Name 'adds it to a list box
'here is where you put what you want it to do when it finds a file of that type
End If
Next
[Edited by Edneeis on 05-30-2000 at 02:25 AM]
-
Thanks a lot Edneeis, your way is exactly what I was looking for. I appreciate everyones help.
-
Much though i personally like the file scripting object, the Dir function is remarkably easy to use.
Code:
Dim myFile As String
myFile = Dir$("c:\*.Bat")
Do While myFile <> ""
List1.AddItem myFile
myFile = Dir
Loop
You don't need the scripting runtime library either.;)