[Resolved][2008]searching subfolders
Well looks like 99% of the codes I found in the help file & in this forum are broken, they simply arent working. I get errors like file type not defined, or not declared.
I know this is simple, but i just cant figure out how can i make it search further than 1 sub folder. I heard that this should be done recrusively, but I dont really understand how that thing works. Mayb someone can help me out hire?
This is my code so far, that works it cant search Drives tho..
Code:
Dim startdir = "C:\New Folder\"
For Each listfolders As String In My.Computer.FileSystem.GetDirectories(startdir)
Me.ListBox1.Items.Add(listfolders)
For Each listfiles As String In My.Computer.FileSystem.GetFiles(listfolders)
Me.ListBox1.Items.Add(listfiles)
Next
Next
Re: [2008]searching subfolders
Code:
Dim sGetFiles() As String = IO.Directory.GetFiles("PATH", "*.*", SearchOption.AllDirectories)
That retrieves all files the "*.*" part from, PATH, and searches all sub directories.. for files as well..
Cheers
Re: [2008]searching subfolders
Quote:
Originally Posted by Icyculyr
That retrieves all files the "*.*" part from, PATH, and searches all sub directories.. for files as well..
Cheers
this is strange Im getting: 'SearchOption' is not declared. error
Re: [2008]searching subfolders
If you haven't imported the System.IO namespace then it would be IO.SearchOption.AllDirectories.
Just keep in mind that if such a search encounters an inaccessible folder it will fail and throw an exception. For that reason you cannot search your entire C; drive that way as it will always fail. If you might be searching a folder tree with inaccessible folders then you need to write your own recursive file search. It's dead easy using Directory.GetFiles and Directory.GetDirectories and there would be more than one example on the forums already.
Re: [Resolved][2008]searching subfolders
Thanx both of you.
Hire is the code if someone has the same question
Code:
Dim file = "C:\New Folder"
Dim sGetFiles() As String = IO.Directory.GetFiles(file, "*.*", IO.SearchOption.AllDirectories)
For ii = 0 To sGetFiles.GetUpperBound(0)
MsgBox(IO.Path.GetFileName(sGetFiles(ii)))
Next