Vbscript to check if the directory is empty

Posted on 2007-11-18
Last Modified: 2008-05-19
HI, I would just like to ask for a vbscript code to check if the directory is empty.. if it is not empty it will execute a specific program.
Question by:oracle_beginner
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
  • 2
LVL 13

Expert Comment

ID: 20309777
I use all the time in ASP like so:

dim fs,fo,x,i
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder("<your folder name>")

i = 0

for each x in fo.files
    i = i + 1
set fo=nothing
set fs=nothing

if i > 0 then
   'you found a file
end if

LVL 93

Accepted Solution

Patrick Matthews earned 125 total points
ID: 20309793
Hello oracle_beginner,

You need to check both files AND subfolders.

Dim fso, fld

Set fso = CreateObject("Scripting.FileSystemObject")
Set fld = fso.GetFolder("c:folder\subfolder")

If fld.Files.Count + fld.SubFolders.Count = 0 Then
    MsgBox "Empty"
    MsgBox "Not Empty"
End If

Set fld = Nothing
Set fso = Nothing



Author Comment

ID: 20310911

 how do we execute a dos base application?
LVL 13

Assisted Solution

samic400 earned 125 total points
ID: 20311053
option Explicit
'//public Constants
public Const NORMAL_PRIORITY_CLASS = &H20&
public Const INFINITE = -1&
'//public Types
   cb as Long
   lpReserved as string
   lpDesktop as string
   lpTitle as string
   dwX as Long
   dwY as Long
   dwXSize as Long
   dwYSize as Long
   dwXCountChars as Long
   dwYCountChars as Long
   dwFillAttribute as Long
   dwFlags as Long
   wShowWindow as Integer
   cbReserved2 as Integer
   lpReserved2 as Long
   hStdInput as Long
   hStdOutput as Long
   hStdError as Long
End Type
   hProcess as Long
   hThread as Long
   dwProcessID as Long
   dwThreadID as Long
End Type
'//API Declarations
public Declare Function WaitForSingleObject Lib "kernel32" (byval _
   hHandle as Long, byval dwMilliseconds as Long) as Long
public Declare Function CreateProcessA Lib "kernel32" (byval _
   lpApplicationName as Long, byval lpCommandLine as string, byval _
   lpProcessAttributes as Long, byval lpThreadAttributes as Long, _
   byval bInheritHandles as Long, byval dwCreationFlags as Long, _
   byval lpEnvironment as Long, byval lpCurrentDirectory as Long, _
   lpStartupInfo as STARTUPINFO, lpProcessInformation as _
public Declare Function CloseHandle Lib "kernel32" (byval _
   hObject as Long) as Long

Put This Code In A Form (Form1):

private Sub Command1_Click()
    Dim AppToRun as string
    Dim ParamForApp as string
    Dim CmdLine as string
    '//set Application to Run
    AppToRun = "C:\WINNT\System32\CMD.exe"
    '//set Command Line Parameters
    '//The "/C" Tells Windows to Run The Command then Terminate
    '//The Command Line (CMD.exe)
    ParamForApp = " /C MD New_Folder" '//Make new Directory Called 'New_Folder'
    '//Build Command string
    CmdLine = AppToRun & ParamForApp
    '//Shell App And Wait for It to Finish
    ExecCmd CmdLine
End Sub
public Sub ExecCmd(CmdLine as string)
    Dim start as STARTUPINFO
    Dim ReturnValue as Integer
    '//Initialize The STARTUPINFO Structure
    start.cb = len(start)
    '//Start The Shelled Application
    ReturnValue = CreateProcessA(0&, CmdLine, 0&, 0&, 1&, _
       NORMAL_PRIORITY_CLASS, 0&, 0&, start, Proc)
    '//Wait for The Shelled Application to Finish
       ReturnValue = WaitForSingleObject(Proc.hProcess, 0)
    Loop Until ReturnValue <> 258
    '//Close Handle to Shelled Application
    ReturnValue = CloseHandle(Proc.hProcess)
End Sub

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Part One of the two-part Q&A series with MalwareTech.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month6 days, 15 hours left to enroll

623 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