Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How check if folder is empty?

Posted on 2003-03-31
7
Medium Priority
?
234 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

564 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