Link to home
Start Free TrialLog in
Avatar of Mark Bakelaar
Mark BakelaarFlag for Norway

asked on

Check if file exists

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
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

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

Avatar of Mark Bakelaar

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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