how do you get the folder names from a directory

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1296 Views - Last Post: 18 February 2013 - 03:49 PM Rate Topic: -----

#16 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 237
  • View blog
  • Posts: 785
  • Joined: 01-February 13

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 11:20 AM

Sorry if this is getting to be a pain in the butt. I just can`t seem to wrap my head around it because when i test my code it lists the full path to each folder in the directory that does not have the file in it. Below i have an example. The (*) means the folder has the sig.jpg in it. The rest do not. What folders do you want listed in the listbox ? Sorry for crude drawing but, i couldn`t get a picture to upload.


Directory----> A ----------> A1
| |
| |
| -----> A2(*)
|
|
|
--> B(*) ------> B1(*)
|
|
-----> B2
Was This Post Helpful? 0
  • +
  • -

#17 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 237
  • View blog
  • Posts: 785
  • Joined: 01-February 13

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 12:45 PM

View PostIronRazer, on 18 February 2013 - 01:20 PM, said:

Sorry if this is getting to be a pain in the butt. I just can`t seem to wrap my head around it because when i test my code it lists the full path to each folder in the directory that does not have the file in it. Below i have an example. The (*) means the folder has the sig.jpg in it. The rest do not. What folders do you want listed in the listbox ? Sorry for crude drawing but, i couldn`t get a picture to upload.


Directory----> A ----------> A1
| |
| |
| -----> A2(*)
|
|
|
--> B(*) ------> B1(*)
|
|
-----> B2

Sorry, i tried to upload a pic to diagram a folder structure so you could explain which folders you would want listed but, upload failed. Then i tried it this way but, the editor removed the spaces in the post. If i could see the way your folders where set up and which ones you would want listed i could help better. I guess i just don`t understand. When i run my program it lists every folder in the directory that i tell it to search and it lists all the folders that do not have the file in it. I don`t know what folders it is listing that are wrong. If you can make a small folder structure picture and upload it. Then tell what ones you want listed that would help.
Was This Post Helpful? 0
  • +
  • -

#18 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 12:52 PM

Bah.. but is your suggestion recursive enough to get folders inside folders?! :D
Was This Post Helpful? 0
  • +
  • -

#19 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 237
  • View blog
  • Posts: 785
  • Joined: 01-February 13

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 12:58 PM

View Postmodi123_1, on 18 February 2013 - 02:52 PM, said:

Bah.. but is your suggestion recursive enough to get folders inside folders?! :D/>


Yes. I tested on folders that where 5 folders deep and it listed all of them that did not have my test file in it. the first post only searched 1 folder deep but, the second post searches all sub folders. Test it out.
Was This Post Helpful? 0
  • +
  • -

#20 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 01:02 PM

Quote

Test it out.

I would rather not.
Was This Post Helpful? 0
  • +
  • -

#21 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 444
  • View blog
  • Posts: 3,075
  • Joined: 12-January 10

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 02:28 PM

did you try using your method with folders that contain the sought after file and various other files? It will read the other files and assume that the folder is what you are looking for even though it contains the file you want to exclude.
Was This Post Helpful? 0
  • +
  • -

#22 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,021
  • Joined: 12-June 08

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 02:41 PM

Mine doesn't. :D

Quote

All folders in: C:\test
-----------
C:\test
C:\test\New folder
C:\test\New folder (2)
C:\test\New folder (2)\New folder

Folders containing: sig.jpg
-----------
C:\test\sig.jpg
C:\test\New folder\sig.jpg

Folders not containing: sig.jpg
-----------
C:\test\New folder (2)
C:\test\New folder (2)\New folder

Was This Post Helpful? 0
  • +
  • -

#23 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3336
  • View blog
  • Posts: 11,305
  • Joined: 12-December 12

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 02:51 PM

This is a little confusing. Are we looking for all folders that do not directly contain the searched for file? Or all folders that do not contain the file either directly or in any of its sub-folders?

If it is the first version then here is my take on it:

Imports System.IO

Module Module1

    Sub Main()
        Dim folderList As New List(Of String)

        GetFolders("C:\Users\Andrew\Documents\newfolder\", folderList)

        For Each fold As String In folderList
            Console.WriteLine(fold)
            'Debug.Print(fold)
        Next
        Console.ReadKey()

    End Sub

    Sub GetFolders(ByVal pStartPath As String, ByRef pFolderList As List(Of String))
        Try
            Dim dirs() As String = Directory.GetDirectories(pStartPath)

            pFolderList.AddRange(From item In dirs
                                 Select item
                                 Where Not Directory.GetFiles(item).Contains(item & "\test.txt"))

            For Each aDir As String In dirs
                GetFolders(aDir, pFolderList)
            Next
        Catch ex As System.UnauthorizedAccessException
            'pass, ignore this folder
        Catch ex As Exception
            Throw ex
        End Try

    End Sub
End Module


The second version would be more complicated :whistling:
Was This Post Helpful? 0
  • +
  • -

#24 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 237
  • View blog
  • Posts: 785
  • Joined: 01-February 13

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 03:15 PM

View PostDarenR, on 18 February 2013 - 04:28 PM, said:

did you try using your method with folders that contain the sought after file and various other files? It will read the other files and assume that the folder is what you are looking for even though it contains the file you want to exclude.


You are correct. I was testing on empty folders with just the test file in some of them. I corrected the error if you want to try this. I guess i was looking past you telling me they had other files in them so i put some other files in them and tested this. It works. Sorry i didn`t think about that before.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myDir As String = "c:\test2"
        Dim dirInfo As New System.IO.DirectoryInfo(myDir)
        For Each dr In dirInfo.GetDirectories("*", IO.SearchOption.AllDirectories)
            If dr.GetFiles.Length > 0 Then
                If Not IO.File.Exists(dr.FullName & "\txt.rtf") Then
                    If Not ListBox1.Items.Contains(dr.FullName) Then ListBox1.Items.Add(dr.FullName)
                End If
            Else
                If Not ListBox1.Items.Contains(dr.FullName) Then ListBox1.Items.Add(dr.FullName)
            End If
        Next
    End Sub


Was This Post Helpful? 1
  • +
  • -

#25 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3336
  • View blog
  • Posts: 11,305
  • Joined: 12-December 12

Re: how do you get the folder names from a directory

Posted 18 February 2013 - 03:49 PM

I've solved the second version I mentioned in my recent post :). The following code will list all folders that do not contain the file "test.txt" within its folder OR any of its sub-folders:

Imports System.IO

Module Module1

    Sub Main()
        Dim folderList As New List(Of String)

        GetFolders("C:\Users\Andrew\Documents\newfolder\", folderList)

        For Each fold As String In folderList
            Console.WriteLine(fold)
            Debug.Print(fold)
        Next
        Console.ReadKey()

    End Sub

    Sub GetFolders(ByVal pStartPath As String, ByRef pFolderList As List(Of String))
        Try
            Dim dirs() As String = Directory.GetDirectories(pStartPath)

            pFolderList.AddRange(From item In dirs
                                 Select item
                                 Where Directory.GetFiles(item, "test.txt",
                                                          SearchOption.AllDirectories).Count = 0)

            For Each aDir As String In dirs
                GetFolders(aDir, pFolderList)
            Next
        Catch ex As System.UnauthorizedAccessException
            'pass, ignore this folder
        Catch ex As Exception
            Throw ex
        End Try

    End Sub
End Module

Whe'hay! Well, I like it anyway :bigsmile: :smartass:

Added: It's probably the same as other posters' code but I was on my own little mission.

This post has been edited by andrewsw: 18 February 2013 - 03:53 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2