11 Replies - 16389 Views - Last Post: 19 September 2009 - 03:05 AM Rate Topic: -----

#1 JohnorSky  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 137
  • Joined: 02-September 08

How to list all subfiles/folders in dir?

Posted 15 September 2009 - 04:10 PM

I want to take a directory "C:\" and list all files, folders, subfiles and subfolders... basically every file and folder located in "C:\" and if the file cant be read for some reason it goes to the next file..

I have this code:
Dim Files As String() = Directory.GetFiles("C:\")
		Dim Dirs As String() = Directory.GetDirectories("C:\")

		ListBox1.Items.Add("Root Files")
		Dim Filename As String

		For Each Filename In Files
			ListBox1.Items.Add(Filename)
		Next

		ListBox1.Items.Add("Root Directories")
		Dim DirectoryName As String

		For Each DirectoryName In Dirs
			ListBox1.Items.Add(DirectoryName)
		Next


But it doesn't list any subfiles and subfolders..

This post has been edited by JohnorSky: 15 September 2009 - 04:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to list all subfiles/folders in dir?

#2 vbnetskywalker  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 98
  • Joined: 08-June 08

Re: How to list all subfiles/folders in dir?

Posted 15 September 2009 - 07:22 PM

try this ........


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim BaseFolder As New IO.DirectoryInfo(Me.TextBox1.Text)

        For Each f As IO.DirectoryInfo In BaseFolder.GetDirectories()
            AddContents(f)
        Next
    End Sub
    Sub AddContents(ByVal BaseFolder As IO.DirectoryInfo)

        ListBox1.Items.AddRange((From FI As IO.FileInfo In BaseFolder.GetFiles Select FI.Name).ToArray)
        For Each subF As IO.DirectoryInfo In BaseFolder.GetDirectories()
            Application.DoEvents()
            ListBox1.Items.Add("[" & BaseFolder.Name & "]")
            AddContents(subF)
        Next
    End Sub




just make it fits your needs
good luck
Was This Post Helpful? 0
  • +
  • -

#3 JohnorSky  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 137
  • Joined: 02-September 08

Re: How to list all subfiles/folders in dir?

Posted 15 September 2009 - 07:32 PM

View Postvbnetskywalker, on 15 Sep, 2009 - 06:22 PM, said:

try this ........


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim BaseFolder As New IO.DirectoryInfo(Me.TextBox1.Text)

        For Each f As IO.DirectoryInfo In BaseFolder.GetDirectories()
            AddContents(f)
        Next
    End Sub
    Sub AddContents(ByVal BaseFolder As IO.DirectoryInfo)

        ListBox1.Items.AddRange((From FI As IO.FileInfo In BaseFolder.GetFiles Select FI.Name).ToArray)
        For Each subF As IO.DirectoryInfo In BaseFolder.GetDirectories()
            Application.DoEvents()
            ListBox1.Items.Add("[" & BaseFolder.Name & "]")
            AddContents(subF)
        Next
    End Sub




just make it fits your needs
good luck


This:
ListBox1.Items.AddRange(Directory.GetFiles("DirectoryHere", "*.*", SearchOption.AllDirectories))

would fit my needs perfectly, but there is one thing I'm not sure of.. If and when I come across an inaccessible folder the entire program shuts down and gives an error... And with a "TRY CATCH" the entire operation is skipped.. If the program came across an inaccessible folder how would I let the program know to go to the next folder and not just stop and fail there?
Posted Image

This post has been edited by JohnorSky: 15 September 2009 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: How to list all subfiles/folders in dir?

Posted 16 September 2009 - 02:04 AM

where did you place your try catch statement?
Was This Post Helpful? 0
  • +
  • -

#5 vbnetskywalker  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 98
  • Joined: 08-June 08

Re: How to list all subfiles/folders in dir?

Posted 16 September 2009 - 07:10 AM

Quote

I want to take a directory "C:\" and list all files, folders, subfiles and subfolders... basically every file and folder located in "C:\"


your post meant that you wanted FILES & FOLDERS .......
but the code you're saying that it fits your needs doesn't get FOLDERS names ..........
that's way I posted this "Recursive Sub Routine"

------------------------------
any way.........



I suppose that you have to add a "Try Catch" statement for every single "ListBox1.Items.Add" statement
try this .....

    Sub AddContents(ByVal BaseFolder As IO.DirectoryInfo)
        Try
            Dim tmp = BaseFolder.GetFiles
            Dim tmp1 = BaseFolder.GetDirectories
        Catch ex As Exception
            Exit Sub
        End Try

        Try
            ListBox1.Items.AddRange((From FI As IO.FileInfo In BaseFolder.GetFiles Select FI.Name).ToArray)
        Catch ex As Exception
            Application.DoEvents()
        End Try


        For Each subF As IO.DirectoryInfo In BaseFolder.GetDirectories()
            Application.DoEvents()
            Try
                ListBox1.Items.Add("[" & BaseFolder.Name & "]")
            Catch ex As Exception
                Application.DoEvents()
            End Try
            AddContents(subF)
        Next
    End Sub



but I don't believe this is gonna be a pro way to do this ......
any way take care that "Exceptions" take too much of "System Resources"

hope this helps....

This post has been edited by vbnetskywalker: 16 September 2009 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 JohnorSky  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 137
  • Joined: 02-September 08

Re: How to list all subfiles/folders in dir?

Posted 16 September 2009 - 10:49 AM

The way that this code searches the directory it gives the path of folders to the file.. That's what I would like..
Posted Image

But when this code is searching something like "C:\" it comes across inaccessible folders and shows an error and then fails.. So it can never complete and list a scan if there is an inaccessible folder is the way..
Posted Image
If I were to place the code in a "TRY CATCH" then when it comes across the system folder it doesn't produce an error, but it doesn't continue scanning either..
try
 ListBox1.Items.AddRange(Directory.GetFiles("c:\test", "*.*", SearchOption.AllDirectories))
catch
end try



I could probably wright another method of doing this to hard code out all of the system folders, but that would bite.. is there some kind of ignore statement or something I could use? Or a method of forcing the folder open..

This post has been edited by JohnorSky: 16 September 2009 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#7 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: How to list all subfiles/folders in dir?

Posted 16 September 2009 - 01:59 PM

You just need to re-order the algorithm so that the try blocks can surround only the troublesome method calls - Directory.GetFiles and Directory.GetDirectories.

You can use a Stack to simplify the code. You grab all the directories into the stack, and then grab the files from each directory on the stack. A quick google found a C# example:

Recursive File and Directory Method in C# (dotnetpearls.com)


Module Module1

	Sub Main()
		Dim files() As String = FileHelper.GetFilesRecursive("C:\test")
		For Each file As String In files
			Console.WriteLine(file)
		Next
	End Sub

End Module

Public Class FileHelper

	' http://dotnetperls.com/recursively-find-files
	Public Shared Function GetFilesRecursive(ByVal topFolder As String) As String()		
		Dim results As New List(Of String)
		Dim folders As New Stack(Of String)		
		folders.Push(topFolder)
		While folders.Count > 0			
			Dim folder As String = folders.Pop
			Try
				results.AddRange(IO.Directory.GetFiles(folder, "*.*"))
				For Each subFolder As String In IO.Directory.GetDirectories(folder)
					folders.Push(subFolder)
				Next			
			Catch ex As IO.IOException
				' The folder might no longer exist when you try to read its files.
				Debug.WriteLine(ex.Message)
			Catch ex As UnauthorizedAccessException
				' No permission.
				Debug.WriteLine(ex.Message)
			End Try
		End While
		Return results.ToArray
	End Function

End Class

Was This Post Helpful? 0
  • +
  • -

#8 JohnorSky  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 137
  • Joined: 02-September 08

Re: How to list all subfiles/folders in dir?

Posted 16 September 2009 - 02:13 PM

This looks like the same thing but in VB.NET: http://dotnetperls.c...directory-vbnet

I tested this code and it doesn't seem to be working correctly..
Imports System
Imports System.Collections.Generic
Imports System.IO
Public Class Form1
	Public Class FileHelper
		''' <summary>
		''' This method starts at the specified directory, and traverses all subdirectories.
		''' It returns a List of those directories.
		''' </summary>
		Public Shared Function GetFilesRecursive(ByVal initial As String) As List(Of String)
			' This list stores the results.
			Dim result As New List(Of String)
			' This stack stores the directories to process.
			Dim stack As New Stack(Of String)
			' Add the initial directory
			stack.Push(initial)
			' Continue processing for each stacked directory
			Do While (stack.Count > 0)
				' Get top directory string
				Dim dir As String = stack.Pop
				Try
					' Add all immediate file paths
					result.AddRange(Directory.GetFiles("c:\test", "*.*"))

					' Loop through all subdirectories and add them to the stack.
					Dim directoryName As String
					For Each directoryName In Directory.GetDirectories(dir)
						stack.Push(directoryName)
					Next
				Catch ex As Exception
				End Try
			Loop
			' Return the list
			Return result
		End Function
	End Class
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		' Get recursive List of all files starting in this directory.
		Dim list As List(Of String) = FileHelper.GetFilesRecursive("C:\test")
		' Loop through and display each path.
		For Each path In list
			ListBox1.Items.Add(path)
		Next
		' Write total number of paths found.
	End Sub
End Class



Output:
Posted Image

This post has been edited by JohnorSky: 16 September 2009 - 02:26 PM

Was This Post Helpful? 0
  • +
  • -

#9 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: How to list all subfiles/folders in dir?

Posted 16 September 2009 - 03:27 PM

Ah, could have saved myself the bother of translating it...
Was This Post Helpful? 0
  • +
  • -

#10 StealthTools  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 52
  • Joined: 11-March 08

Re: How to list all subfiles/folders in dir?

Posted 17 September 2009 - 09:01 PM

I spent the time and coded a completely new method.. Much easier to understand, though it's probably not as efficient..

This post has been edited by StealthTools: 17 September 2009 - 09:02 PM

Was This Post Helpful? 0
  • +
  • -

#11 JohnorSky  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 137
  • Joined: 02-September 08

Re: How to list all subfiles/folders in dir?

Posted 18 September 2009 - 11:49 PM

View PostStealthTools, on 17 Sep, 2009 - 08:01 PM, said:

I spent the time and coded a completely new method.. Much easier to understand, though it's probably not as efficient..


Thanks for the file stealth.. works great :-P
Was This Post Helpful? 0
  • +
  • -

#12 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: How to list all subfiles/folders in dir?

Posted 19 September 2009 - 03:05 AM

View PostJohnorSky, on 16 Sep, 2009 - 01:13 PM, said:

I tested this code and it doesn't seem to be working correctly..
Imports System
Imports System.Collections.Generic
Imports System.IO
Public Class Form1
	Public Class FileHelper
		''' <summary>
		''' This method starts at the specified directory, and traverses all subdirectories.
		''' It returns a List of those directories.
		''' </summary>
		Public Shared Function GetFilesRecursive(ByVal initial As String) As List(Of String)
			' This list stores the results.
			Dim result As New List(Of String)
			' This stack stores the directories to process.
			Dim stack As New Stack(Of String)
			' Add the initial directory
			stack.Push(initial)
			' Continue processing for each stacked directory
			Do While (stack.Count > 0)
				' Get top directory string
				Dim dir As String = stack.Pop
				Try
					' Add all immediate file paths
					result.AddRange(Directory.GetFiles("c:\test", "*.*"))

					' Loop through all subdirectories and add them to the stack.
					Dim directoryName As String
					For Each directoryName In Directory.GetDirectories(dir)
						stack.Push(directoryName)
					Next
				Catch ex As Exception
				End Try
			Loop
			' Return the list
			Return result
		End Function
	End Class
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		' Get recursive List of all files starting in this directory.
		Dim list As List(Of String) = FileHelper.GetFilesRecursive("C:\test")
		' Loop through and display each path.
		For Each path In list
			ListBox1.Items.Add(path)
		Next
		' Write total number of paths found.
	End Sub
End Class



You've altered the code found at the link.


' Add all immediate file paths
result.AddRange(Directory.GetFiles("c:\test", "*.*"))


This means it keeps adding files from C:\test, rather than from the directory that it is in.

Should be:

' Add all immediate file paths
result.AddRange(Directory.GetFiles(dir, "*.*"))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1