Check if file exists

Posted on 2009-04-24
Last Modified: 2012-05-06
Hi Experts,
I create a row in a table when I receive summary information. Later I receive files with detailed information belonging to the created row.

I need to check if the file is received and if so I need to store the path&filename in the summary row.

The summery row contains a name and a date that can be used to check if the file exists. However the date in the filename will not always be exactly the same. It could be a few days earlier or later.

Can someone point me in the right direction?

Best regards, MB
Question by:mark_norge
    LVL 53

    Expert Comment

    Do you want to check for a certain date-range than ?
     Public Function FileExists(ByVal FileFullPath As String) _
          As Boolean
            Dim f As New IO.FileInfo(FileFullPath)
            Return f.Exists
        End Function
        Public Function FolderExists(ByVal FolderPath As String) _
       As Boolean
            Dim f As New IO.DirectoryInfo(FolderPath)
            Return f.Exists
        End Function

    Open in new window


    Author Comment

    Thanks for the reply Dhaest,

    The date range is in the file name. The file name is for instance:
    BRF_Green Concordia_12042009_Some other details.xls (The file name is automatically generated but can be changed)

    When I check if the file exists I have Green Concorida (as name) and for instance 10042009 (as date). The name is exactly the same but the date is 2 days less. My problem is how to find a file where I only have part of the filename and regarding the date this has a range.

    Regards, MB
    LVL 53

    Accepted Solution

    You can't use wildcards with File.Exist, however using the Directory.GetFiles may provide the functionality you are after

    string[] files = Directory.GetFiles("D:\Documents and Settings\Lou\My Documents\Visual Studio", "*.txt");
    if (files.Length > 0)
        // Do something

    Open in new window


    Author Closing Comment

    Thanks a lot, it works.

    Private Function ReturnSoundingFileLocation(ByVal Vessel As String, ByVal ReportDateTimeGMT As DateTime) As String
            Dim FileLocation As String = "x"
            Dim Files() As String = Directory.GetFiles("C:\Bunkers\BRF", "*.xls")
            Dim FileDetails() As String
            Dim BunkerDate As DateTime

            If (Files.Length > 0) Then
                For i = 0 To Files.Length - 1
                    FileDetails = Files(i).ToString.Split("_"c)
                    If FileDetails(1).ToString = Vessel Then
                        BunkerDate = New Date(FileDetails(2).Substring(4, 4), FileDetails(2).Substring(2, 2), FileDetails(2).Substring(0, 2))
                        If BunkerDate > ReportDateTimeGMT.AddDays(-5) And BunkerDate < ReportDateTimeGMT.AddDays(5) Then
                            FileLocation = Files(i).ToString
                        End If
                    End If
                Next i
            End If
            Return FileLocation
        End Function

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now