Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB6 - Number of files in folder.

Posted on 2003-11-03
8
Medium Priority
?
358 Views
Last Modified: 2010-05-03
Hi,

How can I know the number of files that are in a folder with out looping in that folder?



Thanks,
Angel
0
Comment
Question by:2Angel
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:wuttrain
ID: 9670896
try this:

Public Function FileCount(ByVal DirectoryName As String) As Long

'E.g. MsgBox FileCount("C:\windows\system")

Dim objFS As New Scripting.FileSystemObject
Dim objFolder As Scripting.Folder

If objFS.FolderExists(DirectoryName) Then
    Set objFolder = objFS.GetFolder(DirectoryName)
    FileCount = objFolder.Files.Count
End If

set objFolder = Nothing
set objFS = Nothing

End Function
0
 
LVL 2

Author Comment

by:2Angel
ID: 9671434
tks for your answer, but it's not working. I have an error "user-defined type not defined"

Do you know an API to this?


Tks,
Angel
0
 
LVL 2

Expert Comment

by:wuttrain
ID: 9671760
Make a reference to Microsoft Scripting Runtime.

Project | References, then check on Microsoft Scripting Runtime. the function I gave should work.

I got this API from the Internet using ListBox control to count it. this works too

Option Explicit
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Sub Command1_Click()
    Dim r As Long
    r = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\windows\*.*")
    MsgBox r + 1
End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:fantasy1001
ID: 9675974
0
 
LVL 2

Author Comment

by:2Angel
ID: 9677369
wuttrain:

Thank you very much,
the API is working very nice - but every time I "Click" the number in the MsgBox is doubled. Way? Is it because of that the List1 is not cleared? What is the method to clear it?


Tks,
Angel
0
 
LVL 2

Accepted Solution

by:
wuttrain earned 200 total points
ID: 9678167
add this line: List1.Clear

the new click event should look like this:

Private Sub Command1_Click()
Dim r As Long
    List1.Clear
    r = SendMessageStr(List1.hwnd, &H18D, &H20, "C:\windows\*.*")
    MsgBox r + 1
End Sub

this should work for you now.

thanks
0
 
LVL 2

Author Comment

by:2Angel
ID: 9678261
Thank you very much!!! (I alredy fund that... )

It's working just fine.

Tks again,
Angel
0
 
LVL 2

Expert Comment

by:wuttrain
ID: 9678378
you're welcome. thanks for the points
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question