How do I check if a ftp directory is empty?

Posted on 2011-04-19
Last Modified: 2013-11-26
I am writing an application to connect to a ftp server, and read the ftp files in a a ftp directory. How do I check if the ftp directory is empty or not? Any suggestions will be appreciate
Question by:Jeeck
    LVL 12

    Expert Comment

    One way would be to examine the return from the ls command.

    Author Comment

    This is what I am doing
    Dim strFileName  as string() = Strings.Split(newFTP.ListDirectory("/MyDirectory/")(0), vblf)I
    This code works fine if I have files in the directory. But, of Course, if there are no files in the directory, it doesn't work anymore. I am thinking about checking  newFTP.ListDirectory("/MyDirectory/") first, but don't know. I am trying all combinations but not succes yet.
    If newFTP.ListDirectoryDetail("/ClientUpdateLog/") Is Nothing Then

                  '  Do nothing
     End If
    Can you be specific about the Is command?
    LVL 12

    Accepted Solution

    Here's a discussion that you may find useful:

    Meanwhile, the fact that the statement is failing when there are no files allows you to use error handling to accomodate that situation, right?

    Also you might consider using FtpWebResponse. Any data returned by the request, such as the list of file names returned for a ListDirectory request, is available in the stream returned by the GetResponseStream method. The length of the stream data can be obtained from the ContentLength property.  I believe a length of zero would tell you what you need to know.


    Author Closing Comment

    Just have to copy and paste. :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now