I have a logic problem, just need more brains working on this.
For Each JobNode In JobNodes
Dim Source = JobNode.SelectNodes("Source")
For Each item As System.Xml.XmlNode In Source
Dim infoReader As System.IO.FileInfo
''''Discerns whether a file or directory
If item.InnerText.Contains(".") Then 'is a file
If Dir$(item.InnerText) <> vbNullString Then
mySize += FileLen(item.InnerText)
End If
zip.AddFile(item.InnerText, "")
Dim numFiles2 = filenames.Count
mySize = BytesTO(mySize, convTo.MB)
Console.WriteLine("...Added all " & numFiles2 & " files. Total loose file collection size is " & Math.Round(mySize, 2) & " MB")
Console.WriteLine(vbCrLf)
Else 'is a directory
zip.AddDirectory(item.InnerText, GetLastDirName(item.InnerText & " "))
Dim dinfo As New DirectoryInfo(item.InnerText)
Dim sizeOfDir As Long = DirectorySize(dinfo, True)
Dim numFiles As Integer = CountFiles_FolderAndSubFolders(item.InnerText)
Console.WriteLine("...Added all " & numFiles & " files. Total directory size is {0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))
Console.WriteLine(vbCrLf)
End If
Next
This is part of a backup program I made. This is the part that determines if the object to be added to the zip is a file (first part of the IF statement) or a directory (ELSE part of the IF statement).
My problem is, I am trying to add this code:
Dim numFiles2 = filenames.Count
mySize = BytesTO(mySize, convTo.MB)
Console.WriteLine("...Added all " & numFiles2 & " files. Total loose file collection size is " & Math.Round(mySize, 2) & " MB")
Console.WriteLine(vbCrLf)
Just after the zip.AddFile(item.innertext,"")
part. I want to add this so that I can get a console print out of the information.
The problem is, if I just put it directly after the zip.addfile()
, it outputs the code directly above each time. I only want it to print out that part (directly above) once it has finished adding all of the loose files (or files added using zip.AddFile()
)
EDIT: adding the for loops at the top for more clarification
Add a flag and then look for it after the loop completes. It seems as though you are looping through the files instead of calling this recursively. I am guessing you have only one loop right now.
Before Loop iteration:
Dim FilesBeingAdded as Boolean = False
Dim numFiles2 as Integer = 0
Inside of iteration:
If item.InnerText.Contains(".") Then 'is a file
If FilesBeingAdded = False Then
FilesBeingAdded = true
End If
If Dir$(item.InnerText) <> vbNullString Then
mySize += FileLen(item.InnerText)
End If
zip.AddFile(item.InnerText, "")
numFiles2 +=1
Else 'is a directory
zip.AddDirectory(item.InnerText, GetLastDirName(item.InnerText & " "))
Dim dinfo As New DirectoryInfo(item.InnerText)
Dim sizeOfDir As Long = DirectorySize(dinfo, True)
Dim numFiles As Integer = CountFiles_FolderAndSubFolders(item.InnerText)
Console.WriteLine("...Added all " & numFiles & " files. Total directory size is {0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))
Console.WriteLine(vbCrLf)
End If
After Loop Iteration:
If FilesBeingAdded = True Then
mySize = BytesTO(mySize, convTo.MB)
Console.WriteLine("...Added all " & numFiles2 & " files. Total loose file collection size is " & Math.Round(mySize, 2) & " MB")
Console.WriteLine(vbCrLf)
mySize = 0
numFiles2 = 0
FilesBeingAdded = False
End If
So when it finishes the current iteration it will take the tally of files it added, along with the size and print it to the console. It then zeros the variables so that the next iteration of the inner loop has clean values. I am presuming you run this on more than one directory. By checking FilesBeingAdded
you can be sure to avoid situations where an ugly zero count output is shown if the directory contained no files.
This solution presumes a lot about how you enter this piece of code. If my presumptions were not correct, please modify the question to enable a better solution.