?
Solved

How check if folder is empty?

Posted on 2003-03-31
7
Medium Priority
?
233 Views
Last Modified: 2012-05-04
In VB6 I want to check if a folder under Windows is completely empty before I save some files in it. My question now is: How can i check if an existing folder is empty.
Tx
0
Comment
Question by:jansp
[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
7 Comments
 
LVL 14

Expert Comment

by:aelatik
ID: 8237497
This should do...

Dim sFilename As String
sFilename = Dir("c:\*.*")
If Len(sFilename) = 0 Then
    MsgBox "folder is empty"
Else
    MsgBox "folder is NOT empty"
End If

Happy programming,
Ayhan Elatik
0
 
LVL 4

Accepted Solution

by:
TigerZhao earned 200 total points
ID: 8237508
if dir("c:\data\*.*") = vbnullstring then
    MsgBox "Folder Is empty."
Else
    MsgBox "Folder not empty."
End IF
0
 
LVL 3

Expert Comment

by:happel
ID: 8237511
With FileSystemObject (scrrun.dll - Miscrosoft Scripting Runtime)

    Dim mfSo As FileSystemObject
    Set mfSo = New FileSystemObject
    Debug.Print mfSo.GetFolder("C:\temp").Files.Count

0
Technology Partners: 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 14

Expert Comment

by:aelatik
ID: 8237560
If its about code shortening...

If Dir("c:\*.*") = vbNullString Then MsgBox "Empty" Else MsgBox "Not empty"
0
 
LVL 3

Expert Comment

by:DocM
ID: 8237718
Hi,
To check if it contains no files, no folders:
Private Function IsDirectoryEmpty(myPath) As Boolean

    IsDirectoryEmpty = True
    myname = Dir(myPath, vbDirectory)
   
    Do While myname <> ""
   
        ' Ignore the current directory and the encompassing directory.
        If myname <> "." And myname <> ".." Then
            IsDirectoryEmpty = False
            Exit Function
        End If
        ' Get next entry.
        myname = Dir
       
    Loop
End Function

Private Sub Command1_Click()
    result = IsDirectoryEmpty("c:\")
End Sub
0
 
LVL 11

Expert Comment

by:supunr
ID: 8238254
FolderName = "C:\MyFolder\"

if (Dir(FolderName & "*") = "" and Dir(FolderName & "*.*") = "") then
    msgbox "Folder is empty"
else
    msgbox "Folder is NOT empty"
end if

'Good Luck!
0
 

Author Comment

by:jansp
ID: 8238317
This seems to be the easiest way to handle this problem and it gives me the result I needed.
Thanks a lot.

K.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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 …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

764 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