BP Forums
Listing directories - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+---- Thread: Listing directories (/showthread.php?tid=391)



Listing directories - Covert2String - 08-19-2011

Hi, I saw this video: <!-- m --><a class="postlink" href="http://www.youtube.com/watch?v=UwGjbPwrMXo">http://www.youtube.com/watch?v=UwGjbPwrMXo</a><!-- m --> to list all files in a directorie, however I need to list all sub directories, like this:

->Main dir
-->file1
-->Sub dir
--->file2

I need the app to list it this way:

file1
Sub dir/file2

How can I do this?


Re: Listing directories - brandonio21 - 08-19-2011

Hello Covert2String - Welcome to the forum!!

Here is a code that should work for you, not sure if it is exactly what you wanted:

Code:
Dim output As String = ""
        Dim dirinfo As New System.IO.DirectoryInfo(My.Application.Info.DirectoryPath)
        Dim allfileinfo() As System.IO.FileInfo = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
        For Each item In allfileinfo
            If item.DirectoryName = My.Application.Info.DirectoryPath Then
                output = output + item.Name + vbNewLine
            Else
                output = output + item.DirectoryName + item.Name + vbNewLine
            End If
        Next



Re: Listing directories - Covert2String - 08-20-2011

Thanks for the code, but it's not working exactly as I want, the application it's showing the absolute path, like this:


C:\Users\REMOVED\Desktop\list\list\bin\Debug\actlist.vshost.exe
C:\Users\REMOVED\Desktop\list\list\bin\Debug\actlist.vshost.exe
C:\Users\REMOVED\Desktop\list\list\bin\Debug\act\ddlist.xml

I want it only to show act\ddlist or the file if it's not in a sub dir.


Re: Listing directories - brandonio21 - 08-20-2011

Ah okay, well what you can do is use this code...

Code:
Dim output As String = ""
        Dim dirinfo As New System.IO.DirectoryInfo(My.Application.Info.DirectoryPath)
        Dim allfileinfo() As System.IO.FileInfo = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
        For Each item In allfileinfo
            If item.DirectoryName = My.Application.Info.DirectoryPath Then
                dim path as string = item.fullname.replace(my.application.info.directorypath, "")
                output = output + path + vbNewLine
            Else
dim path as string = item.fullname.replace(my.application.info.directorypath, "")
                output = output + path + vbNewLine
            End If
        Next

This should work!


Re: Listing directories - Covert2String - 08-20-2011

Well, almost there! Idk why, but it writes the last file twice o.O

Edit: Not only the last file but all of them...


Re: Listing directories - brandonio21 - 08-20-2011

Well that could be a problem... Try this code!

Code:
Dim output As String = ""
        Dim dirinfo As New System.IO.DirectoryInfo(My.Application.Info.DirectoryPath)
        Dim allfileinfo() As System.IO.FileInfo = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
        For Each item In allfileinfo
                dim path as string = item.fullname.replace(my.application.info.directorypath, "")
                output = output + path + vbNewLine
            
        Next

This should do it!!!


Re: Listing directories - Covert2String - 08-20-2011

Not working, it repeats some files :S

Edit: Forget it, my mistake! Thank you! You're a life saver!


Re: Listing directories - brandonio21 - 08-20-2011

Ah, sounds good! I am glad I could help!! Do you fully understand the code?


Re: Listing directories - Covert2String - 08-20-2011

Yes, all thanks to you!