Solved

VB6 - Number of files in folder.

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

Expert Comment

by:fantasy1001
ID: 9675974
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 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

12 Experts available now in Live!

Get 1:1 Help Now