Link to home
Create AccountLog in
Avatar of Lynchie435
Lynchie435

asked on

Investigating A DIrectory.GetFiles for particular FileName

Hi,

I am curious to know if there is a quicker/cleaner way of me establishing whether a particular filename existing when I doa  Directory.GetFiles on a Folder.

Basically can I avoid doing the following:

Try
Dim strFiles = Directory.GetFiles(GlobalVariables.strFolderPath)
            Dim FileName As String
            'Get Count of Files in Directory
            Dim strCnt = My.Computer.FileSystem.GetFiles(GlobalVariables.strFolderPath).Count
            Dim FileConnection As String = ""

For Each FileName In strFiles

                If strCnt < 2 Then

                    If FileName = "Covernote.pdf" Then
                        'Cover Note Email - Does not work at present
                    ElseIf FileName = "Quotation.pdf" Then
                        'Quotation Email - Does not work at present
                    Else
                        FailedEmail("1 Attachment Only - Unknown Type", "There is only one attachment, this must either be a Covernote or a Quotation")
                    End If

                Else

                    If FileName = "Welcome.pdf" Or FileName = "Cover_Letter_PC.pdf" Then
                        'Do New Business
                    ElseIf FileName = "Renew.pdf" Or FileName = "Renewed.pdf" Then
                        'Do Renewal / Transfrd NB
                    Else
                        'FailedEmail("Unknown Letter Type", "Unable to find a 'Main Letter' Type these can be <br><br>Welcome.pdf<br>Cover_Letter_PC.pdf<br>Renew.pdf<br>Renewed.pdf")
                    End If

                End If

            Next

Catch ex As Exception
            MessageBox.Show(ex.Message, "Main Processing", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            WriteToLog(ex.Message)
        End Try

Open in new window


Something along of the lines of
If Exists(Directory.GetFIles) = "Renew.pdf" = True Then 'Do This End If

Open in new window


Regards,

James
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
To be frank, you don't have to resolve the files in the directory to check for the existence of a file.  You can simply use the File or FileInfo class to do the same:
Module Module1
	Sub Main()
		Console.WriteLine(If(System.IO.File.Exists("C:\Windows\System32\kernel32.dll"), "Sytem.IO.File reports that C:\Windows\System32\kernel32.dll; exists.", "Sytem.IO.File reports that C:\Windows\System32\kernel32.dll; does not exist."))
		Console.WriteLine(If(New System.IO.FileInfo("C:\Windows\System32\kernel32.dll").Exists, "Sytem.IO.FileInfo reports that C:\Windows\System32\kernel32.dll; exists.", "Sytem.IO.FileInfo reports that C:\Windows\System32\kernel32.dll; does not exist."))
		Console.ReadLine()
	End Sub
End Module

Open in new window


Produces the following output:User generated image
-saige-
Avatar of Lynchie435
Lynchie435

ASKER

Can't believe I was practically on the same path, thanks guys.

You know where you stare at something too long you miss the simple things.