Solved

VB6 - Number of files in folder.

Posted on 2003-11-03
8
349 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 …
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…
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…

830 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