Solved

VB6 - Number of files in folder.

Posted on 2003-11-03
8
346 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:fantasy1001
Comment Utility
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Author Comment

by:2Angel
Comment Utility
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 50 total points
Comment Utility
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
Comment Utility
Thank you very much!!! (I alredy fund that... )

It's working just fine.

Tks again,
Angel
0
 
LVL 2

Expert Comment

by:wuttrain
Comment Utility
you're welcome. thanks for the points
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now