Solved

How to Check Whether an Array Is Empty

Posted on 2009-04-10
4
1,902 Views
Last Modified: 2013-12-25
In the code below, strFileList is a array of file names (strings).

I want to be sure the array is not empty before I run the File Delete portion of the code.  

Will the method below work for skipping the File Delete portion if there are no values in the array?  

Is there a better way to check to see if the array is empty.
Dim strFileList As String()

        Dim strFileName As String

 

        Dim intFileListItemCount As Integer = 0

 

        If Directory.Exists(strBackupPath) = True Then

 

            strFileList = Directory.GetFiles(strBackupPath, "*.bak")

 

            For Each strFileName In strFileList

                intFileListItemCount = intFileListItemCount + 1

            Next

 

            If intFileListItemCount > 0 Then

                For Each strFileName In strFileList

                    File.Delete(strFileName)

                Next

 

                Return True

            Else

                Return False

 

            End If

        Else 'Directory does not exist

            Return False

        End If

Open in new window

0
Comment
Question by:TimSledge
  • 2
4 Comments
 
LVL 48

Accepted Solution

by:
jpaulino earned 100 total points
ID: 24120212
You can check the Length of the array that gives you the number of elements you have

        If strFileName.Length = 0 Then

            Stop ' array is empty

        End If

Open in new window

0
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 25 total points
ID: 24120256
I believe that should be...

    strFileList.Length = 0

Wayne
        Dim strFileList As String()

        Dim strFileName As String

  

        If Directory.Exists(strBackupPath) = True Then

             strFileList = Directory.GetFiles(strBackupPath, "*.bak") 

            If strFileList.Length > 0 Then

                For Each strFileName In strFileList

                    File.Delete(strFileName)

                Next 

                Return True

            Else

                Return False 

            End If

        Else 'Directory does not exist

            Return False

        End If

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 24120262
>> I believe that should be...
It was a typo :)
0
 
LVL 4

Author Comment

by:TimSledge
ID: 24120812
Thanks for your help!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now