|
-
May 28th, 2000, 08:33 PM
#1
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?
-
May 28th, 2000, 08:54 PM
#2
Fanatic Member
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.
-
May 28th, 2000, 09:00 PM
#3
Thanks, but will this go through all the files in the folder?
-
May 29th, 2000, 01:15 AM
#4
-
May 29th, 2000, 04:16 AM
#5
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 29th, 2000, 06:56 AM
#6
New Member
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.
-
May 29th, 2000, 01:23 PM
#7
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]
-
May 29th, 2000, 10:23 PM
#8
Thanks a lot Edneeis, your way is exactly what I was looking for. I appreciate everyones help.
-
May 29th, 2000, 11:35 PM
#9
Fanatic Member
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.
Iain, thats with an i by the way!
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
|