Hi All,

I have a folder, here's the folder tree:

\---DC20120127-00000831
    \---PABO20120127000017
        \---AT
            |   V20.txt
            |   V23.txt
            |   
            +---2012033
            |       00000002_00000000.tif
            |       00000003_00000000.tif
            |       00000004_00000000.tif
            |       00000005_00000000.tif
            |       
            \---INVALID IMAGES
                    00000001_.tif
                    00000002_00000001.tif
                    00000003_00000001.tif
                    00000004_00000001.tif
                    00000005_00000001.tif

I have coded a program that will compare if the total count of tif in folder 2012033 (2012033 is the combination of year and dayofyear) is equal to the total count of ".tif" string found in V20.txt and V23.txt.

Here's my code so far:

Public Class Form1
    Dim openFolder As New FolderBrowserDialog
    Private Sub btnValidate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnValidate.Click
        Try
            ListView1.Items.Clear()
            openFolder.RootFolder = Environment.SpecialFolder.MyComputer
            If openFolder.ShowDialog() = Windows.Forms.DialogResult.OK Then
                For Each Dir As String In Directory.GetDirectories(openFolder.SelectedPath)
                    Dim intPos As Integer
                    intPos = Dir.LastIndexOfAny("\")
                    intPos += 1
                    Dim strName As String = Dir.Substring(intPos, (Len(Dir) - intPos))
                    If GetCountTxt(Dir) <> GetTifCount(Dir & "\" & cLab.Text & "\" & DateTime.Now.Year & String.Format("{0:000}", DateTime.Now.DayOfYear)) Then
                        Dim str(6) As String
                        Dim itm As ListViewItem
                        str(0) = openFolder.SelectedPath
                        str(1) = strName
                        str(2) = "1"
                        str(3) = "1"
                        str(4) = "Unmatched count of tif in text vs. count of tif in folder, please check!"
                        str(5) = "Count in text: " & GetCountTxt(Dir) & " <---> " & "Count in folder: " & GetTifCount(Dir & "\" & cLab.Text & "\" & DateTime.Now.Year & String.Format("{0:000}", DateTime.Now.DayOfYear))
                        itm = New ListViewItem(str)
                        ListView1.Items.Add(itm)
                    End If
                Next
                MessageBox.Show("Done Edit Checking!", "Information Box", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
                 Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Function GetTifCount(ByVal strPath As String) As Integer
        Try
            Return Directory.GetFiles(strPath, "*.TIF", SearchOption.AllDirectories).Length()
        Catch ex As Exception
            Dim str(6) As String
            Dim itm As ListViewItem
            str(0) = strPath
            str(1) = strPath
            str(2) = "N/A"
            str(3) = "N/A"
            str(4) = "Day of the year is not set to current, please check the folder name of valid tif files. This should be " & DateTime.Now.Year & String.Format("{0:000}", DateTime.Now.DayOfYear)
            str(5) = ""
            itm = New ListViewItem(str)
            ListView1.Items.Add(itm)
            Throw New Exception("Critical error, please check the on-screen error.", ex)
        End Try
    End Function
    Public Function GetCountTxt(ByVal strPath As String) As Integer
        Try
            Dim fileEntries As String() = Directory.GetFiles(strPath, "*.txt", SearchOption.AllDirectories)
            ' Process the list of .txt files found in the directory. ' 
            Dim fileName As String
            Dim tcount As Integer = 0
            For Each fileName In fileEntries
                Dim basa As New StreamReader(fileName)
                While Not basa.EndOfStream
                    Dim law As String = basa.ReadLine()
                    If law.ToLower.Contains(".tif") Then
                        tcount += 1
                    End If
                End While
            Next
            Return tcount
        Catch ex As Exception
            Throw New Exception("Error From GetCountTxt Function" & ex.Message, ex)
        End Try
    End Function
End Class

The problem is that the result is always unmatched because the filename of tif from folder 2012033 was used in V20.txt and V23.txt. For example, string "00000002_00000000.tif" is present in V20.txt as well as in V23.txt that's why I can't accurately match them. What should be my work around here?

Is there a way to simplify my code? I'm sorry I'm just starting to learn vb.net 2010.

I've attached my input files here for your reference.

Thanks in advance.

You can make two new lists in RAM:
List A that is all of V20
List B that is all of V23 minus the contents of V20

Imports System.Linq
Module Module1
    Sub Main()
        ' simulated pulling from directory
        Dim lst20 As New List(Of String) From
         {"00000001_00000000.tif",
          "00000002_00000000.tif",
          "00000003_00000000.tif",
          "00000004_00000000.tif"}

        ' simulated pulling from directory
        Dim lst23 As New List(Of String) From
         {"00000001_00000001.tif",
          "00000002_00000000.tif",
          "00000002_00000001.tif",
          "00000003_00000001.tif"}

        Dim lst23New = lst23.Except(lst20).ToList()

        lst23New.ForEach(Sub(s) Console.WriteLine(s))
    End Sub
End Module

Once you have the lists (lst20 and lst23New), you can compare the contents of the directories to them.

Hi thines01,

Thanks for the input. How can I do that if the tif filenames are not constant?

That was to simulate reading the directory dynamically.
If memory serves me, the command is Directory.GetFiles()
...one read for each array (or list)

You can make two new lists in RAM:
List A that is all of V20
List B that is all of V23 minus the contents of V20

Imports System.Linq
Module Module1
    Sub Main()
        ' simulated pulling from directory
        Dim lst20 As New List(Of String) From
         {"00000001_00000000.tif",
          "00000002_00000000.tif",
          "00000003_00000000.tif",
          "00000004_00000000.tif"}

        ' simulated pulling from directory
        Dim lst23 As New List(Of String) From
         {"00000001_00000001.tif",
          "00000002_00000000.tif",
          "00000002_00000001.tif",
          "00000003_00000001.tif"}

        Dim lst23New = lst23.Except(lst20).ToList()

        lst23New.ForEach(Sub(s) Console.WriteLine(s))
    End Sub
End Module

Once you have the lists (lst20 and lst23New), you can compare the contents of the directories to them.

I have no problems with the directory of tif. I have problem in the text files as stated above. Thanks for the reply.

That was to simulate reading the directory dynamically.
If memory serves me, the command is Directory.GetFiles()
...one read for each array (or list)

I do get the exact count in the directory and it is accurate, but when I am finding the string ".tif" in v23.txt and v20.txt, the result is not accurate because of what I explaid in my first post, by the way, thanks for the new idea.

OK (right)... then the lists will be of the contents of those text files "except"-ed against each other.

I'm completely confused about this issue.
Are you trying to compare 2.files that contain file.paths of your 2.folders(1.file for each folder), and If similar file.names to Not count them?

I'm completely confused about this issue.
Are you trying to compare 2.files that contain file.paths of your 2.folders(1.file for each folder), and If similar file.names to Not count them?

hi codeorder,

i am trying to compare the count of tif files from folder 2012033 and the count of "tif" string found in all text files of my folder, v20.txt and v23.txt.

See if this helps.

Imports System.IO
Public Class Form1
    Private myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With New FolderBrowserDialog
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                If getFileLinesCount(myFolder & "V20.txt") + getFileLinesCount(myFolder & "V23.txt") = getTifFilesCount(.SelectedPath) Then
                    MsgBox("=")
                Else : MsgBox(":(")
                End If
            End If
        End With
    End Sub
    Public Function getTifFilesCount(ByVal selFolder As String) As Integer
        If Directory.Exists(selFolder) Then Return Directory.GetFiles(selFolder, "*.TIF", SearchOption.AllDirectories).Length Else Return 0
    End Function
    Public Function getFileLinesCount(ByVal selFile As String) As Integer
        If File.Exists(selFile) Then Return File.ReadAllLines(selFile).Length Else Return 0
    End Function
End Class

See if this helps.

Imports System.IO
Public Class Form1
    Private myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\"

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With New FolderBrowserDialog
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                If getFileLinesCount(myFolder & "V20.txt") + getFileLinesCount(myFolder & "V23.txt") = getTifFilesCount(.SelectedPath) Then
                    MsgBox("=")
                Else : MsgBox(":(")
                End If
            End If
        End With
    End Sub
    Public Function getTifFilesCount(ByVal selFolder As String) As Integer
        If Directory.Exists(selFolder) Then Return Directory.GetFiles(selFolder, "*.TIF", SearchOption.AllDirectories).Length Else Return 0
    End Function
    Public Function getFileLinesCount(ByVal selFile As String) As Integer
        If File.Exists(selFile) Then Return File.ReadAllLines(selFile).Length Else Return 0
    End Function
End Class

thanks for this one codeorder but it doesn't seem to be accurate since the ".tif" string in the text files can be a duplicate string. For example, the contents of 2012033 folder are 00000002_00000000.tif, 00000003_00000000.tif, 00000004_00000000.tif and 00000005_00000000.tif and in the v20.txt 00000002_00000000.tif is present as well as in v23.txt; the count of string ".tif" in text files will be over one since 00000002_00000000.tif was used twice, is there a way that we can avoid that to happen? I hope there is.

thanks in advance.

1.Add a ArrayList.
2.Clear it before loading your files.
3.Loop thru each line in each file and check if the ArrayList.Contains(file.line); If Not, add to ArrayList.
4.Get count of files in folder and compare to Arraylist.Count.
5.:)

1.Add a ArrayList.
2.Clear it before loading your files.
3.Loop thru each line in each file and check if the ArrayList.Contains(file.line); If Not, add to ArrayList.
4.Get count of files in folder and compare to Arraylist.Count.
5.:)

this is noted codeorder but it seems that it is advanced compared to what I have learned and i am having a hard time coding it, I understand the logic but I don't know where to start. But I will try.

Let me know how it goes and if no results on your part, notify .Me and I'll post the solution.
.btw, ArrayLists are just like ListBoxes.
Instead of ListBox1.Items.Add("something"), you only use myArrayList.Add("something"), w/out the ".Items".

Let me know how it goes and if no results on your part, notify .Me and I'll post the solution.
.btw, ArrayLists are just like ListBoxes.
Instead of ListBox1.Items.Add("something"), you only use myArrayList.Add("something"), w/out the ".Items".

ok I will do it now. Thanks for usual support.

hi codeorder,

pardon me with this off topic question. I'm just wondering.

I just wanted this to ask, is this possible?

In my folder, there are two folders? now I used Directory.GetDirectories command with alldirectories search option. Is is possible to process the first folder (sorted by name) only?

See if this helps.

Dim myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\.vb.net\"
        MsgBox(IO.Directory.GetDirectories(myFolder)(0).ToString) '// folder.1.
        MsgBox(IO.Directory.GetDirectories(myFolder)(1).ToString) '// folder.2.

See if this helps.

Dim myFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\.vb.net\"
        MsgBox(IO.Directory.GetDirectories(myFolder)(0).ToString) '// folder.1.
        MsgBox(IO.Directory.GetDirectories(myFolder)(1).ToString) '// folder.2.

Thanks for this codeorder.

Hi codeorder,

I did it, but my code is not that professionally written, here it is.

Imports System.IO
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If openFolder.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim myFolder As String() = Directory.GetDirectories(openFolder.SelectedPath)
            For Each iFolder As String In myFolder
                Dim intPos As Integer
                intPos = iFolder.LastIndexOfAny("\")
                intPos += 1
                Dim strName As String = iFolder.Substring(intPos, (Len(iFolder) - intPos))
                MessageBox.Show(strName & ": " & GetCountofTifString(iFolder))
                MessageBox.Show(strName & ": " & GetCountofTif(iFolder))
                Directory.Delete(strName, True)
            Next
        Else
            Exit Sub
        End If
    End Sub
    Public Function GetCountofTif(ByVal iFolder As String) As Integer
        Dim rpath As String() = Directory.GetFiles(iFolder, "*.TIF", SearchOption.AllDirectories)
        Dim tcount As Integer = 0
        For Each tpath As String In rpath
            If tpath.ToLower.Contains("invalid") = False Then
                tcount += 1
            End If
        Next
        Return tcount
    End Function
    Public Function GetCountofTifString(ByVal iFolder As String) As Integer
        Dim rpath As String() = Directory.GetFiles(iFolder, "*.txt", SearchOption.AllDirectories)
        Dim scount As Integer = 0
        For Each tpath As String In rpath
            Dim intPos As Integer
            intPos = iFolder.LastIndexOfAny("\")
            intPos += 1
            Dim strName As String = iFolder.Substring(intPos, (Len(iFolder) - intPos))
            Dim basa As New StreamReader(tpath)
            If Not Directory.Exists(strName) Then
                Directory.CreateDirectory(strName)
            End If
            While Not basa.EndOfStream
                Dim law As String = basa.ReadLine()
                If tpath.ToLower.Contains("v13") = True Then
                    If law.ToLower.Contains("tif") = True Then
                        If Trim(law.Substring(402, 21)) <> Nothing Then
                            Dim sulat As New StreamWriter(strName & "\" & law.Substring(402, 21) & ".txt", True)
                            sulat.WriteLine(law.Substring(402, 21), True)
                            sulat.Dispose()
                            sulat.Close()
                        End If
                    End If
                ElseIf tpath.ToLower.Contains("v20") = True Then
                    If law.ToLower.Contains("tif") = True Then
                        If Trim(law.Substring(129, 21)) <> Nothing Then
                            Dim sulat As New StreamWriter(strName & "\" & law.Substring(129, 21) & ".txt", True)
                            sulat.WriteLine(law.Substring(129, 21), True)
                            sulat.Dispose()
                            sulat.Close()
                        End If
                    End If
                ElseIf tpath.ToLower.Contains("v23") = True Then
                    If law.ToLower.Contains("tif") = True Then
                        If Trim(law.Substring(129, 21)) <> Nothing Then
                            Dim sulat As New StreamWriter(strName & "\" & law.Substring(129, 21) & ".txt", True)
                            sulat.WriteLine(law.Substring(129, 21), True)
                            sulat.Dispose()
                            sulat.Close()
                        End If
                    End If
                    End If
            End While
            TextBox1.Text = Directory.GetFiles(strName, "*.txt").Count
        Next
        Return TextBox1.Text
    End Function
End Class

I didn't use arraylist, my method in getting unique string is to write it in file. Can you simplify my code or any suggestions? Attaching my input file for you to try.

Thanks for all the help.

-renzlo

Since your recent post and only wanting the lines that contain a .tif image, see if this helps.

Imports System.IO
Public Class Form1
    Private arlFileLines As New ArrayList
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim fbd As New FolderBrowserDialog '// declared in code, easier to manage.
        If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then '// send all .txt files for evaluation.
            MsgBox("File lines containing images: " & getFileLinesCount(Directory.GetFiles(fbd.SelectedPath, "*.txt", SearchOption.AllDirectories)))
        End If
        '// FOR.TESTING/ETC.
        'If Not arlFileLines.Count = 0 Then
        '    For Each itm As String In arlFileLines
        '        MsgBox(itm)
        '    Next
        'End If
    End Sub
    Public Function getFileLinesCount(ByVal selCoolFiles() As String) As Integer
        arlFileLines.Clear()  '// reset to store non.duplicate lines.
        For Each selFile As String In selCoolFiles '// loop thru all found.files.
            For Each line As String In File.ReadAllLines(selFile) '// loop thru each file's lines.
                If line.Contains(".tif") Then
                    line = line.Substring(line.IndexOf("\") + 1, line.IndexOf(".tif") + 3 - line.IndexOf("\")) '// get .tif image from line.
                    If Not arlFileLines.Contains(line) Then arlFileLines.Add(line) '// if ArrayList does Not contain line, add.line.
                End If
            Next
        Next
        Return arlFileLines.Count
    End Function
End Class

>>Can you simplify my code or any suggestions?
I managed to do a little code clean up and added a Sub to save each file.

Public Function GetCountofTifString(ByVal iFolder As String) As Integer
        Dim intPos As Integer, strName As String = ""
        For Each tpath As String In Directory.GetFiles(iFolder, "*.txt", SearchOption.AllDirectories)
            intPos = (iFolder.LastIndexOfAny("\")) + 1
            strName = iFolder.Substring(intPos, (Len(iFolder) - intPos))
            Dim basa As New StreamReader(tpath)
            If Not Directory.Exists(strName) Then Directory.CreateDirectory(strName)
            While Not basa.EndOfStream
                Dim law As String = basa.ReadLine()
                If law.ToLower.Contains("tif") Then
                    With tpath.ToLower
                        If .Contains("v13") Then saveFile(strName, law, 402, 21)
                        If .Contains("v20") OrElse .Contains("v23") Then saveFile(strName, law, 129, 21)
                    End With
                End If
            End While
        Next
        Return Directory.GetFiles(strName, "*.txt").Length
    End Function
    Private Sub saveFile(ByVal selFolder As String, ByVal selLine As String, iStartIndex As Integer, iLength As Integer)
        If Trim(selLine.Substring(iStartIndex, iLength)) <> Nothing Then
            Dim sulat As New StreamWriter(selFolder & "\" & selLine.Substring(iStartIndex, iLength) & ".txt", True)
            With sulat : .WriteLine(selLine.Substring(iStartIndex, iLength), True) : .Dispose() : .Close() : End With
        End If
    End Sub

remarkable, thanks as always codeorder.

Solved +=1 For .Me :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.