I have some code to return a list of all files and sub-directories from a chosen directory. As it looks through the directory, the folder names are shortened and dumped to a text file (using a hash table to avoid duplication). The code works for the first part, but is not returning the names of empty folders (which I want). I have spent a while toying with it and can't get it to work properly and am honestly lost. Any help with it would be great...
Code:Private Sub writeFileAndDirectoryList(ByVal sDirectory As String, ByVal bRecursive As Boolean, ByVal iLevel As Integer) Dim dirInfo As New IO.DirectoryInfo(sDirectory) Dim dirIter As IO.DirectoryInfo Dim fileInfo As IO.FileInfo Dim dirFiles As String() = Directory.GetFiles(selectedInputFolder) MsgBox("this") ' Read first the subdirectories Try For Each dirIter In dirInfo.GetDirectories() If bRecursive Then ' Increment the directory level iLevel = iLevel + 1 ' Call the procedure for each subdirectories writeFileAndDirectoryList(dirIter.FullName, bRecursive, iLevel) Application.DoEvents() End If ' Then read the files For Each dirIter2 In dirInfo.GetDirectories() hashValueCounter += 1 Dim dirBuilder As New StringBuilder(dirInfo.FullName) dirBuilder.Replace(pathToDir, "\") If dirIter2.GetFiles.Length = 0 Then 'Folder has no files MsgBox("me") Try foldersList.Add(dirBuilder.ToString & ">" & "0", hashValueCounter) fileListWriter2.WriteLine(dirBuilder.ToString & "\" & ">" & "0") Catch e As Exception ' MsgBox("Exists") End Try End If Next Next For Each fileInfo In dirInfo.GetFiles() hashValueCounter += 1 Dim builder As New StringBuilder(fileInfo.FullName) builder.Replace(pathToDir, "\") fileListWriter2.WriteLine(builder.ToString & ">" & fileInfo.Length) debugForm.msgList.Text += fileInfo.FullName & ">" & fileInfo.Length & vbCrLf Application.DoEvents() Next Catch e As UnauthorizedAccessException MsgBox("Not allowed to read DIR") End Try![]()




Reply With Quote