2

I want to check a few files in a folder. i have no problem if check file one by one. Currently i have this code for checking only one file.

  If File.Exists("C:\FINAL.txt") = False Then
        MsgBox("Field does not exist!", MsgBoxStyle.Critical, "File Not Found")
    Else
        MsgBox("File Exist in System Folder", MsgBoxStyle.Information, "File is Found")
    End If

How do i change the code so that it will check more than one file at same time?

5
  • Do you have an array of files you want to check?
    – Dave Chen
    Sep 20, 2014 at 10:00
  • nope.. for now, i just want to if two of the files exist.. i even try this code If File.Exists("C:\FINAL.txt") = False and File.Exists("C:\FIRST.txt") = False then Sep 20, 2014 at 10:03
  • Both filenames are the same, if you changed the second one to SECOND.txt or something, it should work fine.
    – Dave Chen
    Sep 20, 2014 at 10:06
  • You should not be writing code like this at all. File.Exists() just tells you that the file existed a microsecond ago. It makes no promises that it still exists when you need it to exist. The perils of a multi-tasking operating system. Those kind of bugs are very hard to diagnose, it doesn't go wrong often enough. It is otherwise entirely unclear why you can't use an array and a For loop. Sep 20, 2014 at 10:11
  • i try change the file name but the code only check the first file and ignore the second file. Sep 20, 2014 at 10:12

2 Answers 2

1

There is a lot of ways to do this...

Just one:

Public Class Form1

    Private Shadows Sub Load() Handles MyBase.Load

        Dim Files As String() = {"C:\File1.txt", "C:\File2.txt"}

        For Each File As String In Me.CheckFileExists(Files)

            MessageBox.Show(String.Format("File doesn't exist: {0}", File), "File Not Found",
                            MessageBoxButtons.OK, MessageBoxIcon.Error)

        Next File

    End Sub

    Private Function CheckFileExists(ByVal Files As IEnumerable(Of String)) As IEnumerable(Of String)

        Dim sb As New System.Text.StringBuilder

        For Each File As String In Files

            If Not IO.File.Exists(File) Then
                sb.AppendLine(File)
            End If

        Next File

        Return sb.ToString.Split({Environment.NewLine},
                                 StringSplitOptions.RemoveEmptyEntries)

    End Function

End Class
1
  • Thanks!! it works. just one quick question. if both file present, and i want to prompt msgbox that says both file exist, how do i do that? MsgBox("File Exist in System Folder", MsgBoxStyle.Information, "File is Found") Sep 20, 2014 at 10:50
-1

You can check all files in a directory and find whether the required file is present or not with the following query:

 Dim dir As DirectoryInfo = New DirectoryInfo("d:\")
 dim flag As Integer=0
 For Each File As FileInfo In dir.GetFiles
    If File.Name = "FINAL.txt" Then
        MsgBox("File Exist in System Folder", MsgBoxStyle.Information, "File is Found") 
        flag=1 
    End If
 Next
 If flag = 0 Then
    MsgBox("File does not Exist in System Folder", MsgBoxStyle.Information, "File not Found") 
 End
1
  • i can do it like that but the problem after that is what if the file directory contain too much file? and how do i check if the second file is exist or not? Sep 20, 2014 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.