Listing .rar-files, simple question

elfduck

New member
Joined
Feb 23, 2010
Messages
3
Programming Experience
Beginner
Hi

I'm having a problem when I list .rar-files into a ListBox. I can list .rar-files OK from subdirectories, but I need help when I want to determine if the file is actually the first part of .rar-archive. The new .rar-format packs files like:

Archive.part01.rar
Archive.part02.rar
Archive.part03.rar
...

So I have no idea when I'm listing the first part of the archive or actually listing the other parts. As you can see from my code below, I have done this but it's really primitive solution.

VB.NET:
    Function UpdateFiles(ByVal FolderPath As String) As Boolean

        'If FolderPath is empty, return false
        If FolderPath = "" Then
            Return False
        End If

        'Check to see if FolderPath exists
        If System.IO.Directory.Exists(FolderPath) = False Then

            'Define ExtensionList
            Dim ExtentionsList() As String = {"*.rar"}

            'Clear FileList
            FileList.Items.Clear()

            'Loop through files
            For Each CurrentExtension As String In ExtentionsList
                For Each CurrentFile As String In My.Computer.FileSystem.GetFiles(FolderPath, FileIO.SearchOption.SearchAllSubDirectories, CurrentExtension)
                    If CurrentFile.Contains(".part") Then
                        If CurrentFile.Contains(".part01") Then
                            'Add file to FileList
                            FileList.Items.Add(CurrentFile)
                        ElseIf CurrentFile.Contains(".part001") Then
                            'Add file to FileList
                            FileList.Items.Add(CurrentFile)
                        ElseIf CurrentFile.Contains(".part0001") Then
                            'Add file to FileList
                            FileList.Items.Add(CurrentFile)
                        End If
                    Else
                        'Add file to FileList
                        FileList.Items.Add(CurrentFile)
                    End If
                Next
            Next

        Else 'If FolderPath does not exist, return false
            Return False
        End If

        'Return true
        Return True

    End Function

Also other optimization suggestions to my function are very welcome :)
 
I came up with a solution for this. RegEx. I'll post my function here in case someone else finds it useful:

VB.NET:
    Function UpdateFiles(ByVal FolderPath As String) As Boolean

        'Check to see if FolderPath exists
        If FolderPath = "" Or Directory.Exists(FolderPath) = False Then
            Return False
        End If

        'Define variables
        Dim ExtentionsList() As String = {"*.rar"}
        Dim RegExToMatch As New Regex("\.part[0-9]*1\.rar", RegexOptions.Compiled And _
                            RegexOptions.IgnoreCase And RegexOptions.Singleline)
        Dim TopDirectoryInfo As New IO.DirectoryInfo(FolderPath)

        'Clear FileList
        FileList.Items.Clear()

        'Loop through directories
        For Each Directory As IO.DirectoryInfo In TopDirectoryInfo.GetDirectories

            'Exclude system directories
            If Not ((Directory.Attributes And IO.FileAttributes.System) = IO.FileAttributes.System) Then



                'Loop through files
                For Each CurrentExtension As String In ExtentionsList
                    For Each CurrentFile As String In My.Computer.FileSystem.GetFiles(Directory.FullName, FileIO.SearchOption.SearchAllSubDirectories, CurrentExtension)
                        If CurrentFile.Contains(".part") Then 'Match new type RAR files
                            If RegExToMatch.IsMatch(CurrentFile) Then
                                'Add file to FileList
                                FileList.Items.Add(CurrentFile)
                            End If
                        Else
                            'Add file to FileList
                            FileList.Items.Add(CurrentFile)
                        End If
                    Next
                Next

            End If
        Next

        'Listing successful, return true
        Return True

    End Function

Still, as you can see, it's rather messy. If someone has some ideas how to make it better, please post.
 
Back
Top