Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for recursively adding file names to a text file via VB.NET. http://dougv.us/r5 for details.
Imports System.IO
Sub GetAllFiles(strPath As String)
Dim objRoot As New DirectoryInfo(strPath)
Dim objSubDir As DirectoryInfo()
Dim objFile As FileInfo()
If objRoot.Exists Then
'if you don't want this to be recursive, remove this for loop
For Each objSubDir In objRoot.GetDirectories
GetAllFiles(objSubDir.FullName)
Next
'keep this for loop
For Each objFile In objRoot.GetFiles
AddFileName(strPath & "\" & objFile.Name)
Next
End If
End Sub
Sub AddFileName(strName As String)
Dim strOutput As String = "C:\\path_to_output.txt"
If Not File.Exists(strOutput) Then
File.CreateText(strOutput)
End If
Dim objStream As StreamWriter = File.AppendText(strOutput)
objStream.WriteLine(strName)
objStream.Flush()
objStream.Close()
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment