Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB6 - Number of files in folder.

Posted on 2003-11-03
8
Medium Priority
?
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Suggested Courses

704 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