Solved

Investigating A DIrectory.GetFiles for particular FileName

Posted on 2014-11-13
3
98 Views
Last Modified: 2014-11-14
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
0
Comment
Question by:Lynchie435
3 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 40440448
http://msdn.microsoft.com/en-us/library/wz42302f%28v=vs.110%29.aspx

Roughly you want
Directory.GetFiles(GlobalVariables.strFolderPath, "Covernote.pdf")
to check for that file, same with the other files

OR

Directory.GetFiles(GlobalVariables.strFolderPath, "*.pdf")
to return all the files with the extension pdf
0
 
LVL 33

Expert Comment

by:it_saige
ID: 40440488
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:Capture.JPG
-saige-
0
 

Author Closing Comment

by:Lynchie435
ID: 40442099
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

22 Experts available now in Live!

Get 1:1 Help Now