Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Sub Directory in VB.net  (Read 4674 times)

0 Members and 1 Guest are viewing this topic.

almn

  • Guest
Sub Directory in VB.net
« on: November 09, 2006, 08:27:12 AM »
Hello,

I wrote a program in vb.net that shows shows the path of every file in a directory in a messagebox however I would like to do the same thing so that the program shows every file in the main directory and sub directories. Here is my code:

        Dim dirInfo As New System.IO.DirectoryInfo("C:\my documents")
        Dim file As System.IO.FileInfo
        Dim files() As System.io.FileInfo = dirInfo.GetFiles("*.*")
For Each file In files
                Dim sr As New IO.StreamReader(file.FullName)
                Dim str As String = sr.ReadToEnd
MESSAGEBOX.SHOW(file.fullname)
Next


Thanks

Al968

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Sub Directory in VB.net
« Reply #1 on: November 09, 2006, 03:07:47 PM »
You need to make your code recursive (a module calling itself) in order to climb down each branch of the directory tree. I found an example of recursion in the snippet closet. It's written in VBScript but it should give you some idea how to go about it.

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives

For each ds in dc
        Select Case ds.DriveType
                Case 2
                        Set RootDir = fso.GetFolder(ds & "\")    'starting directory goes here
                        GetThePaths(RootDir)
        End Select
Next

Function GetThePaths(Folder)
  For Each Subfolder in Folder.SubFolders
        GetTheFiles(Subfolder.Path)
        GetThePaths Subfolder
  Next
End Function

Function GetTheFiles(FileFolder)
  Set f = fso.GetFolder(FileFolder)
  Set fc = f.Files
  For Each fs in fc
       WScript.Echo fs
  End If
  Next
End Function

Good luck.  8-)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

almn

  • Guest
Re: Sub Directory in VB.net
« Reply #2 on: November 11, 2006, 12:49:44 PM »
Thanks for the responce.
"It's written in VBScript but it should give you some idea how to go about it. "
Actually I have no idea how VBScript works  :P
But thanks anyways  ;)

Al968

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Sub Directory in VB.net
« Reply #3 on: November 11, 2006, 02:51:07 PM »
Quote
Actually I have no idea how VBScript works

VBScript is a subset of VB but without the forms, the variable declarations, and all that pesky compiling. Actually the snippet was posted to show you a method to accomplish your task. It was not posted as a solution as there are differences between VB and VBScript.

 8-)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

almn

  • Guest
Re: Sub Directory in VB.net
« Reply #4 on: November 27, 2006, 05:06:44 AM »
Well thanks for your help  ;)
I understand your code a little but I don't know how to write it in vb.net  :-/
But I least know I have something  ;)

Thanks

Al968