find the highest folder named like a GUID

Jul 30, 2011 at 9:28 PM


not a question about the library, but I hope to get ideas to find solution :-)

In an archive-file exist the below folder structure:


My problem is to find the highest folder, here "\MainFolder\GUID".

I tried to read all entries with SelectEntries and check the entry with IsDirectory, but I'm unable to find a solution for this issue.

So I would like to ask you about a possibility.

Kind regards,

Jul 31, 2011 at 12:42 AM

I don't understand what makes "\MainFolder\GUID" the "highest" folder?   Do you mean to sort the folders by folder name?

If so, you can do it with a LINQ query and an orderby clause.

When searching the filesystem you could do something like this:

var nameOfLastGuidDirectory = 
    (from dn in Directory.GetDirectories("MainFolder", "????????-????-????-????-????????????")
        orderby dn).Take(1);

Searching the items in an archive is a little different. There is no concept of a "directory" as a container in a zip archive. So you need to parse the FileName of the ZipEntry in order to determine if the "parent directory" is a guid, and then order by the parent directory.  I think something like this might work:

Regex regexGuid = new Regex("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$", RegexOptions.Compiled);
using (var zip = ZipFile.Read(archiveToRead))
    var entry = (from e in zip.Entries
        where regexGuid.IsMatch(Path.GetDirectoryName(e.FileName))
        orderby Path.GetDirectoryName(e.FileName) 
        select e).Take(1);
    var lastGuid = Path.GetFileName(Path.GetDirectoryName(entry));

I didn't try this... 

Jul 31, 2011 at 8:09 AM
Edited Jul 31, 2011 at 8:11 AM

Hi Cheeso,

you are brilliant, with your answer I have written my function:

Private Function EngFirstGuidPath() As String
        'Expression to find a GUID
        Dim regexGuid As New System.Text.RegularExpressions.Regex("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}", System.Text.RegularExpressions.RegexOptions.Compiled)
        'Select all entries like a folder and matched to a GUID
        Dim GUID_Entries =
                From DirectoryEntry As Ionic.Zip.ZipEntry In _zipFile.Entries
                Where DirectoryEntry.IsDirectory And regexGuid.IsMatch(System.IO.Path.GetDirectoryName(DirectoryEntry.FileName))
                Order By System.IO.Path.GetDirectoryName(DirectoryEntry.FileName)
        'Return the first folder or nothing if the array count = 0
        If GUID_Entries.Count > 0 Then
            Return System.IO.Path.GetDirectoryName(GUID_Entries.First.FileName)
            Return Nothing
        End If
End Function

At finally, the library is very helpful and easy to use it (if you are able to find a solution)  :-)

Thank you for the great library.

Best regards,