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.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
That was to simulate reading the directory dynamically.
If memory serves me, the command is Directory.GetFiles()
...one read for each array (or list)
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
OK (right)... then the lists will be of the contents of those text files "except"-ed against each other.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
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?
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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.:)
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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".
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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.
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
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
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384