8 Replies - 2588 Views - Last Post: 21 December 2011 - 08:20 AM Rate Topic: -----

#1 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Count Number of Directories and Get the First Directory's Name

Posted 20 December 2011 - 06:30 PM

I am writing a simple GUI for a 7-Zip command that will zip multiple directories, with two files in the working directory, to their own separate archives. The problem is that I have no idea on how to count the number if directories (only directories and not files) and find the name of just the first directory. Here is what I have so far:

        Dim path As String = folderBrowse.SelectedPath
        Dim i As Integer
        Dim o As Integer
        'Count the number of directories in <path> and set it to <o>
        'Convert the first directory's number, which is the 7th and 8th character in the name. Then set that number to <i>
        Do Until i = o
            Shell("cmd /c 7za a -tzip " & path & " Lesson" & i & ".zip" & path & "*.exe *.dat " & "Lesson" & i)
            i += 1
        Loop


Does anyone know how to do this or have any literature that may help?

Is This A Good Question/Topic? 0
  • +

Replies To: Count Number of Directories and Get the First Directory's Name

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9080
  • View blog
  • Posts: 34,127
  • Joined: 12-June 08

Re: Count Number of Directories and Get the First Directory's Name

Posted 20 December 2011 - 06:33 PM

Have you, by chance, looked at the system.io.directory namespace?

Specifically:
Directory.GetDirectories Method

Directory.GetDirectoryRoot Method
Was This Post Helpful? 2
  • +
  • -

#3 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Count Number of Directories and Get the First Directory's Name

Posted 20 December 2011 - 07:39 PM

View PostSpice, on 21 December 2011 - 11:30 AM, said:

I am writing a simple GUI for a 7-Zip command that will zip multiple directories, with two files in the working directory, to their own separate archives. The problem is that I have no idea on how to count the number if directories (only directories and not files) and find the name of just the first directory. Here is what I have so far:

        Dim path As String = folderBrowse.SelectedPath
        Dim i As Integer
        Dim o As Integer
        'Count the number of directories in <path> and set it to <o>
        'Convert the first directory's number, which is the 7th and 8th character in the name. Then set that number to <i>
        Do Until i = o
            Shell("cmd /c 7za a -tzip " & path & " Lesson" & i & ".zip" & path & "*.exe *.dat " & "Lesson" & i)
            i += 1
        Loop


Does anyone know how to do this or have any literature that may help?

Your loop here will only ever run one time. This is due to both integers being instanced (i, o) as 0. If you add 1 to i then the loop wouldn't run because i no longer equals o.
Was This Post Helpful? 1
  • +
  • -

#4 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Count Number of Directories and Get the First Directory's Name

Posted 20 December 2011 - 08:17 PM

Thanks modi123_1. I was able to get the number of directories.

Dim o As Integer = System.IO.Directory.GetDirectories(path).Length


Now I just need to find out how to get the name of the first directory (alphabetically).
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1048
  • View blog
  • Posts: 4,075
  • Joined: 02-July 08

Re: Count Number of Directories and Get the First Directory's Name

Posted 20 December 2011 - 08:40 PM

Private Function GetFirstFileAtoZ(<path> As String) As String
  Dim files As New List(Of String)
  files = Directory.GetDirectories(<path>).ToList
  'sort them
  files.Sort()
  'the first one is the first alphanumeric one
  Return files.First
End Function

This post has been edited by _HAWK_: 20 December 2011 - 08:41 PM

Was This Post Helpful? 1
  • +
  • -

#6 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Count Number of Directories and Get the First Directory's Name

Posted 20 December 2011 - 11:29 PM

Once again you come to the rescue, _HAWK_! I see what it's supposed to do in your example, but it won't work for me (I assume <path> is to be changed to the variable I have for the directory that contains the subdirectories I am working with).
Was This Post Helpful? 0
  • +
  • -

#7 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 583
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Count Number of Directories and Get the First Directory's Name

Posted 21 December 2011 - 12:51 AM

What about files(0)
Was This Post Helpful? 1
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1048
  • View blog
  • Posts: 4,075
  • Joined: 02-July 08

Re: Count Number of Directories and Get the First Directory's Name

Posted 21 December 2011 - 04:31 AM

That returned the file name. What may prove to be more helpful is this example where you can change the Return value.

Private Function GetFirstFileAtoZ(ByVal path As String) As String
    Dim files As New List(Of FileInfo)
    Dim dir As New DirectoryInfo(path)
    files = dir.GetFiles.ToList
    'sort them
    files.Sort()
    'the first one is the first alphanumeric one
    Return files.First.FullName 'full path with ext.
End Function

Was This Post Helpful? 2
  • +
  • -

#9 Spice  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 03-October 11

Re: Count Number of Directories and Get the First Directory's Name

Posted 21 December 2011 - 08:20 AM

Got it! Using you're examples and thinking it through, I loaded an array with the directories using 'System.IO.Directory.GetDirectories(path)', then used dir(0) for the directory I want. Thanks for the help, everyone!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1