how do you get the folder names from a directory

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

how do you get the folder names from a directory

Posted 15 February 2013 - 08:36 AM

I am trying just to get the colder names from a directory after i search to see if a file exists or not.

my current code is as follows:

 Private Sub file_search_Click(sender As System.Object, e As System.EventArgs) Handles file_search.Click
        Dim search_file As String = "xxxxxxxxx/yyyyyyy/vvvvvvv/*/*/*.sig"

        If System.IO.File.Exists(search_file) = False Then


            For Each missing_file In search_file

                txt_PS_Progress.Text += vbCrLf + missing_file

            Next


        End If
    End Sub



in the search_file as string i had to block out the path with xxx yyy vvv .
the * are muliple folders in the above directories that must be searched for the missing .sig file

all i want to return is the folder name from the search that is missing the file with the .sig


thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: how do you get the folder names from a directory

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

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

Posted 15 February 2013 - 08:43 AM

Directory.getfiles typically does a good job returning a string array of file names...

http://msdn.microsof...y.getfiles.aspx


or if you want just folder-folders the directory.getdirectories works.
http://msdn.microsof...irectories.aspx
Was This Post Helpful? 1
  • +
  • -

#3 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

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

Posted 15 February 2013 - 01:06 PM

this is what i came up with. Now the only problem i have is that it still grabs the files containing sig.jpg even though inthe if statement it is tellign it not to grab those.



 For Each fileInfo In dirInfo.GetFiles("sig.jpg", IO.SearchOption.AllDirectories)


                Dim fileDir As String = fileInfo.DirectoryName
                Dim fileName As String = fileInfo.Name.ToString()
                Dim fileFullName As String = fileDir & "\" & fileName
                If System.IO.File.Exists(fileName) = False Then
                    txt_PS_Progress.Text += vbCrLf + fileFullName
                End If

            Next

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

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

Posted 15 February 2013 - 01:13 PM

dirInfo.GetFiles("sig.jpg", IO.SearchOption.AllDirectories)


Look at your usage of 'getfiles'... you are not quite doing it right. The first string is the path to search, and the pattern to match file names against.

http://msdn.microsof...y/wz42302f.aspx
Was This Post Helpful? 0
  • +
  • -

#5 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

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

Posted 15 February 2013 - 01:16 PM

correct i want to make sure that the file extension is NOT in the folders so i am searching by that "sig.jpg" if it is not in the folder i want the folder name to be outputted. i am able to find all the folders that contain the "sig.jpg" but that is in the 10's of thousands.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

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

Posted 15 February 2013 - 01:24 PM

Again, no.. you are using it wrong. The first string is the directory you want to search in. If you have "sig.jpg" that is *NOT* a directory.

Second - the use of " IO.SearchOption.AllDirectories" is only when the input to 'getfiles' is: string, string, IO.SearchOption. So to use ' IO.SearchOption.AllDirectories' you need a second string variable to be input.

Third - notice how all three forms of 'getfiles' return an array of strings?

Example: I am searching a folder called 'test' for any file that is a .csv and printing that string of a file name out.
For Each tempstring As String In IO.Directory.GetFiles("C:\test", "*.csv")
            
Console.WriteLine(tempstring)
        
Next


Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

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

Posted 15 February 2013 - 01:29 PM

modie i have this



        Dim myDir As String = "\\my path here (cant show do to ip stuff)\"         Dim dirInfo As New System.IO.DirectoryInfo(myDir) 'this object is to read and write info. for one directory
        Dim fileInfo As System.IO.FileInfo 'this object is to read and write info. for one physical file



        Try
            For Each fileInfo In dirInfo.GetFiles("sig.jpg", IO.SearchOption.AllDirectories)


                Dim fileDir As String = fileInfo.DirectoryName
                Dim fileName As String = fileInfo.Name.ToString()
                Dim fileFullName As String = fileDir & "\" & fileName
                If System.IO.File.Exists(fileName) = False Then
                    txt_PS_Progress.Text += vbCrLf + fileFullName
                End If

            Next


so i am declaring a path, i just did it differently then you suggested.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

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

Posted 15 February 2013 - 01:41 PM

Okay then look at what you are asking it. You are asking it to find all instances of "sig.jpg".. and only that. So if you do indeed have ten thousand of those files in ten thousand folders it will return an array of 'fileinfo' objects.

Say I am looking for them in 'C:\test', and there is one in that root directory, and one in a directory one level down.

'-- gets all the file locations for 'sig.jpg' in what ever directory
For Each fileInfo In dirInfo.GetFiles("sig.jpg", IO.SearchOption.AllDirectories)

'-- fileDir is "c:\test".
Dim fileDir As String = fileInfo.DirectoryName

'-- filename is "sig.jpg" because .. well that's what we told it to look for.
Dim fileName As String = fileInfo.Name.ToString()
            

'-- filefullname is 'C:\test\sig.jpg' though I am unclear why this is happening.. and why not use fileInfo.FullName
Dim fileFullName As String = fileDir & "\" & fileName
            

'-- here you are asking if file "sig.jpg" exists.. well of course it doesn't since there is no path for it.  '-- Now if it was 'if System.IO.File.Exists(fileInfo.FullName)' that might be a different story.
If System.IO.File.Exists(fileName) = False Then
                
Console.WriteLine(fileInfo.FullName)
            
End If



Ultimately if you are looking to *AVOID* sig.jpg then you need to find a better search pattern or perhaps just get all the files and do a quick forloop to drop any that have that name.
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

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

Posted 15 February 2013 - 05:03 PM

View PostDarenR, on 15 February 2013 - 10:36 AM, said:

I am trying just to get the colder names from a directory after i search to see if a file exists or not.


Hello DarenR,
Here is the way i did it in an old program i made. I changed it to work for your specs. It searches every directory in (MyDir) and if there is no file called (sig.jpg)it will list the full path of the directory that does not have the file. I listed them in a listbox so you will need to change it a little to use a textbox. Let me know if you need help with it.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myDir As String = "\\my path here (cant show do to ip stuff)\"
        Dim alldirs As New System.IO.DirectoryInfo(myDir)
        For Each srchdir In alldirs.GetDirectories()
            If srchdir.GetFiles.Length > 0 Then
                For Each fln In srchdir.GetFiles()
                    If Not fln.Name = "sig.jpg" Then
                        If Not ListBox1.Items.Contains(srchdir.FullName) Then ListBox1.Items.Add(srchdir.FullName)
                    End If
                Next
            Else
                If Not ListBox1.Items.Contains(srchdir.FullName) Then ListBox1.Items.Add(srchdir.FullName)
            End If
        Next

    End Sub


Was This Post Helpful? 0
  • +
  • -

#10 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

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

Posted 15 February 2013 - 06:30 PM

View PostDarenR, on 15 February 2013 - 10:36 AM, said:

I am trying just to get the colder names from a directory after i search to see if a file exists or not.


Sorry about the last post i didn`t see you where searching in all sub directories. The first post only searched 1 directory deep. This one will search all sub directories inside (MyDir).

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim myDir As String = "\\my path here (cant show do to ip stuff)\"
        Dim dirInfo As New System.IO.DirectoryInfo(myDir) 'this object is to read and write info. for one directory
        For Each dr In dirInfo.GetDirectories("*.*", IO.SearchOption.AllDirectories)
            If dr.GetFiles.Length > 0 Then
                For Each fln In dr.GetFiles()
                    If Not fln.Name = "sig.jpg" Then
                        If Not ListBox1.Items.Contains(dr.FullName) Then ListBox1.Items.Add(dr.FullName)
                    End If
                Next
            Else
                If Not ListBox1.Items.Contains(dr.FullName) Then ListBox1.Items.Add(dr.FullName)
            End If
        Next

    End Sub


This post has been edited by IronRazer: 15 February 2013 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

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

Posted 18 February 2013 - 07:00 AM

Thanks for the reply but the problem with your code above is that it doesnt compensate if the folder contains that file. each of the folders that i am searching through has 1- 50 files in it and it may or may not contain the sig.jpg. Your code checks each file and if it is not the one we are looking for it adds that folder into the list box because it can not tell if the other files shouold be counted.
Was This Post Helpful? 0
  • +
  • -

#12 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 295
  • View blog
  • Posts: 925
  • Joined: 01-February 13

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

Posted 18 February 2013 - 09:42 AM

View PostDarenR, on 18 February 2013 - 09:00 AM, said:

Thanks for the reply but the problem with your code above is that it doesnt compensate if the folder contains that file. each of the folders that i am searching through has 1- 50 files in it and it may or may not contain the sig.jpg. Your code checks each file and if it is not the one we are looking for it adds that folder into the list box because it can not tell if the other files shouold be counted.


I guess i just didn`t understand how you are searching. Are you trying to search only the folders that you tell it to inside of a specific directory. For example - Search in directory "XXXX" but, only search in the folders named "aaa & bbb & ccc" and tell me if aaa or bbb or ccc does not have sig.jpg ? If so do you want to search all the sub directories in aaa bbb and ccc ? Or am i not even close ?
Was This Post Helpful? 0
  • +
  • -

#13 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

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

Posted 18 February 2013 - 09:53 AM

I want to search all folders and sub folders in a directory and if the sig.jpg does not exist in any of the folders to display the folders. Currently I tried the exist for files such as
If System.IO.File.Exists(fileName) 
but this doesnt work if there are other files in the folder-- it simply reads the other files and says they dont match what i am looking for.

thanks
Was This Post Helpful? 0
  • +
  • -

#14 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 469
  • View blog
  • Posts: 3,201
  • Joined: 12-January 10

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

Posted 18 February 2013 - 09:53 AM

I want to search all folders and sub folders in a directory and if the sig.jpg does not exist in any of the folders to display the folders. Currently I tried the exist for files such as
If System.IO.File.Exists(fileName) 
but this doesnt work if there are other files in the folder-- it simply reads the other files and says they dont match what i am looking for.

thanks
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9296
  • View blog
  • Posts: 34,832
  • Joined: 12-June 08

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

Posted 18 February 2013 - 10:29 AM

Okay.. so you want folders that don't hold that specific file? Then just get list of all folders for a given directory, and get a list of all locations of that file.

Do a quick compare and remove any entries in the 'all directories' list that can be found in the second list (of the locations of the file you are looking for).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2