[RESOLVED] Getting all file names from all subfolders
Hi! I have a question, how to get all file names from all subfolders and write it to a file? I tried a code, but It gets only the first subfolder level, and its files, what if in that subfolder is another subfolder, and in that, another? How to make a dynamic code, like not to write a for loop as many times as are there subfolders? Thank you!
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("cuccli.txt", False)
Dim mappa = System.IO.Directory.GetDirectories(hely)
For i As Integer = 0 To mappa.Count - 1
file.WriteLine(mappa(i).ToArray & " ---->")
Dim fajl = System.IO.Directory.EnumerateFiles(mappa(i))
For j As Integer = 0 To fajl.Count - 1
file.WriteLine("---" & fajl(j).ToArray)
Next
file.WriteLine("===================")
Next
file.Close()
End Sub
Re: Getting all file names from all subfolders
Read the documentation for the EnumerateFiles method you're already calling, or just pay attention to Intellisense as you type it. It is all you need.
Re: Getting all file names from all subfolders
If you want to do processing on nested items like that (and there isn't an easy option as mentioned above), there are two main ways to do that kind of thing.
You can do it by writing a recursive function (a function that calls a 'copy' of itself), but that is something that many people struggle to understand, so if you are relatively new to programming it is probably best to avoid that option.
An alternative that is much easier to understand is to use a loop based version, which just extends your current code a bit.
Instead of using the mappa array and a For loop, use a List(Of String) for the directories (use AddRange to add all the items from mappa) and a Do loop (Do While theList.Count > 0 ), and inside the loop always use the first element of the list. At the end of the loop, remove the first element.
So far this will make no difference to the behaviour of your code, but the next step is to also do a GetDirectories inside the loop, and add those items to the list. The subfolders will then be automatically added to the folders to check (and the subfolders from them, etc).
Re: Getting all file names from all subfolders
Quote:
Originally Posted by
si_the_geek
If you want to do processing on nested items like that (and there isn't an easy option as mentioned above), there are two main ways to do that kind of thing.
You can do it by writing a recursive function (a function that calls a 'copy' of itself), but that is something that many people struggle to understand, so if you are relatively new to programming it is probably best to avoid that option.
An alternative that is much easier to understand is to use a loop based version, which just extends your current code a bit.
Instead of using the mappa array and a For loop, use a List(Of String) for the directories (use AddRange to add all the items from mappa) and a Do loop (Do While theList.Count > 0 ), and inside the loop always use the first element of the list. At the end of the loop, remove the first element.
So far this will make no difference to the behaviour of your code, but the next step is to also do a GetDirectories inside the loop, and add those items to the list. The subfolders will then be automatically added to the folders to check (and the subfolders from them, etc).
Thank you, the main idea is great, I've tried, and the first step its OK, it works, but I dont think that I understand the second step, how to do that, can you help me?
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(user.Text & ".txt", False)
Dim mappa = System.IO.Directory.EnumerateDirectories(hely)
Dim lista As List(Of String) = New List(Of String)(mappa)
Do While lista.Count > 0
Dim mappa2 = System.IO.Directory.EnumerateDirectories(lista.Item(0))
lista.Add(mappa2.ToString)
file.WriteLine(lista.Item(0))
file.WriteLine("")
lista.RemoveAt(0)
Loop
file.Close()
End Sub
Re: Getting all file names from all subfolders
You will still need to use the same code inside the main loop as before (so EnumerateFiles, and the associated j loop), but using lista.Item(0) instead of mappa(i)
Apart from that what you have seems good, except this line:
Code:
lista.Add(mappa2.ToString) 'this adds a data type description, which tells you it is an array
...which should be:
Code:
lista.AddRange(mappa2) 'this adds each of the items from mappa2
Note however that the advice in post #2 may be a better solution for you (it will be less code, but might not give the output you want).
Re: Getting all file names from all subfolders
Oh, thank you, I'm satisfied with your answer, it worked, thanks a lot!
But finally, can you tell me, how to write in the document the Directory and after that his Subfolder, and etc..?
Because in the output, its like:
-E:\Folder1\Folder1\something.txt
-E:\Folder1\Folder2\something.txt
-E:\Folder1\Folder3\something.txt
-E:\Folder1\Folder1\Folder1\something.txt
-E:\Folder1\Folder1\Folder2\something.txt
-E:\Folder1\Folder1\Folder3\something.txt
-E:\Folder1\Folder2\Folder1\something.txt
....
Instead of:
-E:\Folder1\Folder1\something.txt
--E:\Folder1\Folder1\Folder1\something.txt
--E:\Folder1\Folder1\Folder2\something.txt
--E:\Folder1\Folder1\Folder3\something.txt
-E:\Folder1\Folder2\something.txt
--E:\Folder1\Folder2\Folder1\something.txt
etc...
here's my final code:
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter(user.Text & ".txt", False)
Dim mappa = System.IO.Directory.EnumerateDirectories(hely)
Dim lista As List(Of String) = New List(Of String)(mappa)
Do While lista.Count > 0
Dim mappa2 = System.IO.Directory.GetDirectories(lista.Item(0))
lista.AddRange(mappa2)
file.WriteLine(lista.Item(0) & " ---->")
Dim fajl = System.IO.Directory.EnumerateFiles(lista.Item(0))
For j As Integer = 0 To fajl.Count - 1
file.WriteLine("---" & fajl(j).ToArray)
Next
file.WriteLine("===================")
lista.RemoveAt(0)
Loop
file.Close()
End Sub
Re: Getting all file names from all subfolders
For that you will need to use two loops: one that just adds the folders to the List, then sort that list, and then a loop that does the rest of it.
Re: Getting all file names from all subfolders
Hi,
you could use the CommandPrompt, that would give you a Tree structure
open the CMD and enter
Code:
tree E:\ChrisCopy /A /F >E:\OutputTree.txt
this will write the structur of the Folder E:\ChrisCopy with all Files and subfolders to the Textfile E:\OutputTree.txt
Re: Getting all file names from all subfolders
Thank you very much guys!