Jump to content

Recommended Posts

Posted (edited)

Hi all,

i recently started building a project i have wanted to do for a long time, i dived into Vb .NET 2005 and im loving it, but i need some help if anyone has any experience with it.

im trying to specify a directory (which i can do) and then have it look at each sub directory in turn and return the name of that directory

eg.

m:\test

should return "test"

files have a .fullname property, but im not sure if directories do?

i think i need a for each loop, but im not sure.

if i can just make it pop each subdirectories name up a in a msgbox for now that would be great, then il move onto to doing more with it.

thanks

Edited by eyeball

Posted

You need a recursive function.

First, add an Imports System.IO statement.

Then create the following function:

Private Function Recursive(ByVal strPath As String)
Dim oDir As New DirectoryInfo(strPath)
Dim oSubDir() As DirectoryInfo
Dim i As Int32

oSubDir = oDir.GetDirectories()
For i = 0 To oSubDir.Length - 1
'Do something with the directory, in this case spit it out to the Debug Output window.
Debug.WriteLine(oSubDir(i).Name.ToString)

Call Recursive(oSubDir(i))
Next
End Function

Start it by calling recursive the first time on the root folder to begin at... Call Recursive("C:\Folder\")

Posted

thanks jcarle :)

i got this code which also works

For Each folder As String In IO.Directory.GetDirectories(txtDirectory.Text)
MessageBox.Show(IO.Path.GetFileName(folder))
Next folder

which one is better in your opinion? and why?

thank you :)

Posted
thanks jcarle :)

i got this code which also works

For Each folder As String In IO.Directory.GetDirectories(txtDirectory.Text)
MessageBox.Show(IO.Path.GetFileName(folder))
Next folder

which one is better in your opinion? and why?

thank you :)

That'll do the folders in a folder, but it won't do the subfolders of those folders...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...