-1

Hi everyone i'm just a beginner at .net programming and i really need some help regarding this matter, the logic of my code was "supposed" to look at every directory that a put inside the array.. then will validate if every folder have file inside that's why i used "system.io.file.exist()", sorry for the mess up code please help me...

        Dim schar As String
        Dim number As Integer
        Dim Dirs(4) As String
        Dirs(0) = "E:\_net_programs\test\sample1"
        Dirs(1) = "E:\_net_programs\test\sample2"
        Dirs(2) = "E:\_net_programs\test\sample3"
        Dirs(3) = "E:\_net_programs\test\sample4"
        Dirs(4) = "E:\_net_programs\test\sample5"
        For Each folder As String In Dirs

            Dim getDir As New DirectoryInfo(folder)
            Dim fiArr As FileInfo() = getDir.GetFiles()
            Dim fri As FileInfo
            For Each fri In fiArr

                If Not System.IO.File.Exists(folder + "\" + fri.Name) Then
                    MessageBox.Show("file does not exist. : " + folder + "\" + fri.Name)
                    Return
                Else
                    MessageBox.Show(folder + "\" + fri.Name)
                    Return
                End If

                Label1.Text = "textfile name :" & fri.Name
                Dim file As New System.IO.StreamReader(folder + "\" + fri.Name)
                schar = Trim(file.ReadLine())
                MessageBox.Show(schar)

                Do While file.Peek <> -1
                    schar = Trim(file.ReadLine())
alv
  • 47
  • 5
  • please don't mind the "Return" code – alv Aug 24 '15 at 15:22
  • You can edit your question. It should include what you actually see, best with an example. I understand you are searching for non-empty directories? – planetmaker Aug 24 '15 at 15:34
  • What kind of files are you looking for?? You are enumerating a directory and all files inside it. Then you check whether those files exist or not, which will always exist because you got the file listing of that directory. – Pradeep Kumar Aug 24 '15 at 15:34
  • ow sorry =( what i want to do is.. – alv Aug 24 '15 at 15:35
  • sorry =(.. what i want to do is if the folder doesn't have a file (text file) a message will pop up that the current folder dont have a file – alv Aug 24 '15 at 15:37
  • thanks for the help =) i love you guys hahaa – alv Aug 24 '15 at 15:42

2 Answers2

0

To check if a directory has files, you only need this:

If getDir.GetFiles().Length = 0 Then
    MessageBox.Show(string.Format("Folder {0} doesn't have any file.", folder))
End If

Regarding your sample, you have a issue there.

First you use the GetFiles() method, to get all the files that exists within the specified folder. Why do you need to double check if they exist since they have been just checked?

Jaime Mendes
  • 643
  • 3
  • 9
0

If you are interested in only text(*.txt) files , then you should do:

    Dim Dirs(4) As String
    Dirs(0) = "E:\_net_programs\test\sample1"
    Dirs(1) = "E:\_net_programs\test\sample2"
    Dirs(2) = "E:\_net_programs\test\sample3"
    Dirs(3) = "E:\_net_programs\test\sample4"
    Dirs(4) = "E:\_net_programs\test\sample5"
    For Each folder As String In Dirs
        Dim files = IO.Directory.GetFiles(folder, "*.txt")
        If files.Count = 0 Then
            MessageBox.Show("No txt files in folder : " + folder)
            Return
        Else
            MessageBox.Show(Join(files, vbCrLf))
            Return
        End If

    Next
Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47