Problem getting file names recursively.

C0d3M0nk3y

Member
Joined
May 6, 2011
Messages
10
Programming Experience
Beginner
Hello, I'm new to the forum and new to VB.Net. I am trying to create a small application that will pull values from a config file (which is working) and then based on those values will search through directories recursively for files with specified extensions. In the end I would like these files deleted but I also need to have the app write a list of each file that is successfully delted to a .log file.

Here is what I have so far, the values from the config file are successfully being passed to the sub routine however nothing is ever written to the .log file.

Sub activeRecursive(ByVal activeStat As String, ByVal rootDirectory As String, ByVal fileExtension As String, ByVal maxAge As Int32)
If Not Directory.Exists(rootDirectory) Then
End If
Dim tmpFile As String = "C:\Files.log"
Dim objWriter As New StreamWriter(tmpFile)
Dim allFiles() As FileInfo
Dim dirInfo As DirectoryInfo
dirInfo = New DirectoryInfo(rootDirectory)
allFiles = dirInfo.GetFiles(fileExtension,
SearchOption.AllDirectories)
For Each File In allFiles
objWriter.Write(allFiles)
'IO.File.Delete(fileNames)
Next File
objWriter.close
End Sub

Thanks in advance for the help!
 
You are not using recursion. A recursive function calls itself.

Sub RecursivelyGoThruFolder(di as directoryinfo)
   For each fi as io.fileinfo in di.getfiles
      'each file
   Next fi
   For dir as io.directoryinfo in di.getdirectories
      RecursivelyGoThruFolder(dir)
   Next dir
End Sub
 
Hello, I'm new to the forum and new to VB.Net. I am trying to create a small application that will pull values from a config file (which is working) and then based on those values will search through directories recursively for files with specified extensions. In the end I would like these files deleted but I also need to have the app write a list of each file that is successfully delted to a .log file.

Here is what I have so far, the values from the config file are successfully being passed to the sub routine however nothing is ever written to the .log file.

Sub activeRecursive(ByVal activeStat As String, ByVal rootDirectory As String, ByVal fileExtension As String, ByVal maxAge As Int32)
If Not Directory.Exists(rootDirectory) Then
End If
Dim tmpFile As String = "C:\Files.log"
Dim objWriter As New StreamWriter(tmpFile)
Dim allFiles() As FileInfo
Dim dirInfo As DirectoryInfo
dirInfo = New DirectoryInfo(rootDirectory)
allFiles = dirInfo.GetFiles(fileExtension,
SearchOption.AllDirectories)
For Each File In allFiles
objWriter.Write(allFiles)
'IO.File.Delete(fileNames)
Next File
objWriter.close
End Sub

Thanks in advance for the help!

This line: objWriter.Write(allFiles)
You're writing the .ToString output of the entire array, when you're really wanting to write the 'File' variable contents from the loop.

You are not using recursion. A recursive function calls itself.

Sub RecursivelyGoThruFolder(di as directoryinfo)
   For each fi as io.fileinfo in di.getfiles
      'each file
   Next fi
   For dir as io.directoryinfo in di.getdirectories
      RecursivelyGoThruFolder(dir)
   Next dir
End Sub
The Directory.GetFiles() method call, he's specifying all Directories, which will recursively pull all sub directorie's files as well.

The issue with GetFiles() is that if there's an error (usually no folder viewing permissions) with any of the folders in the tree, the whole thing fails, if he wants the ability to skip a folder in that fasion then he'll want to recursevely traverse the folders him self & put a try/catch block around the recursive call so that it'll gracefully continue with the rest of the folders.
 
Resolved

Thank you very much JuggaloBrotha!

I found out that the reason nothing was being written to the text file is because the user account that I was running the app as didn't have the necessary permission to delete the files but I didn't have any error handling in my script...what's interesting is that the application didn't error...

anyway everything is working now so here is my updated code for those of your who are interested:

Sub activeDeleteRecursive(ByVal activeStat As String, ByVal rootDirectory As String, ByVal fileExtension As String, ByVal maxAge As Int32)
' Specify the directory you want to manipulate.
Dim di As DirectoryInfo = New DirectoryInfo(rootDirectory)
Dim files() As FileInfo = _
di.GetFiles(fileExtension,
SearchOption.AllDirectories)
Dim sfile As FileInfo
Dim tempFileName As String = IO.Path.GetTempFileName()
Dim temp As New IO.FileStream(tempFileName, IO.FileMode.Create)
Dim writeFile As New StreamWriter(temp)
For Each sfile In files
If (Now - sfile.CreationTime).Days > maxAge Then
File.Delete(sfile.FullName)
End If
writeFile.WriteLine(sfile.FullName & cReturn & sfile.CreationTime & cReturn)
Next sfile
Console.WriteLine(tempFileName)
writeFile.Close()
End Sub
 
Back
Top