Listvew - Add Large Amount Of Files At One Time-VBForums
Results 1 to 4 of 4

Thread: Listvew - Add Large Amount Of Files At One Time

  1. #1

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Listvew - Add Large Amount Of Files At One Time

    Hello im new at vbnet i need a recursive function to add add a large amount of files and folders from the folders below if anyone can help me would be awsome!!


    Code:
        Public Function DirExists(ByVal Path As String) As Boolean
            If My.Computer.FileSystem.DirectoryExists(Path) Or My.Computer.FileSystem.FileExists(Path) Then
                Return True
            Else
                Return False
            End If
        End Function
    
          Public Function IsFileInUse(ByVal FileName As String) As Boolean
            Dim Locked As Boolean = False
            Try
                ' Open the file in a try block in exclusive mode. 
                ' If the file is in use, it will throw an IOException.
                Dim fs As IO.FileStream = IO.File.Open(FileName, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
                fs.Close()
                ' If an exception is caught, it means that the file is in Use
            Catch ex As Exception
                Locked = True
            End Try
            Return Locked
        End Function
    
          Public Function AbsoluteFolderPath(ByVal Path As String) As String
            Try
                Dim f As System.IO.FileInfo
                f = My.Computer.FileSystem.GetFileInfo(Path)
                Dim folderPath As String = f.DirectoryName
                Return folderPath
            Catch ex As Exception
                'err
            End Try
        End Function
    
        Public Function AbsoluteFilePath(ByVal Path As String) As String
            Try
                Dim f As System.IO.FileInfo
                f = My.Computer.FileSystem.GetFileInfo(Path)
                Dim fileName As String = f.Name
                Return fileName
            Catch ex As Exception
                'err
            End Try
        End Function
    
            Public Function FormatBytes(ByVal Path) As String
            Dim f As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(Path)
            Dim BytesCaller As Long = f.Length
            Dim DoubleBytes As Double
            Try
                Select Case BytesCaller
                    Case Is >= 1099511627776
                        DoubleBytes = CDbl(BytesCaller / 1099511627776) 'TB
                        Return FormatNumber(DoubleBytes, 2) & " TB"
                    Case 1073741824 To 1099511627775
                        DoubleBytes = CDbl(BytesCaller / 1073741824) 'GB
                        Return FormatNumber(DoubleBytes, 2) & " GB"
                    Case 1048576 To 1073741823
                        DoubleBytes = CDbl(BytesCaller / 1048576) 'MB
                        Return FormatNumber(DoubleBytes, 2) & " MB"
                    Case 1024 To 1048575
                        DoubleBytes = CDbl(BytesCaller / 1024) 'KB
                        Return FormatNumber(DoubleBytes, 2) & " KB"
                    Case 0 To 1023
                        DoubleBytes = BytesCaller ' bytes
                        Return FormatNumber(DoubleBytes, 2) & " bytes"
                    Case Else
                        Return ""
                End Select
            Catch
                Return ""
            End Try
        End Function
    
    
            Dim Cookies As String = (Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
            Dim History As String = (Environment.GetFolderPath(Environment.SpecialFolder.History))
            Dim IECache As String = (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
            Dim WinTemp As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Temp"
            Dim Prefetch As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Prefetch"
            Dim UserTemp As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) + "\Temp"
            Dim ChromeCache As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) + "\Google\Chrome\User Data\Default\Cache"
    
            Dim SpecialFolders As String() = {Cookies, History, IECache, WinTemp, Prefetch, UserTemp, ChromeCache}
    
            'Path is our starting point
          
            For Each Path As String In SpecialFolders
               If DirExists(Path) Then
    
                Dim list As List(Of String) = ScanSpecialFolders(Path) ' this is the function i need to come up with
    
                For Each Item As String In list
                    If Not IsFileInUse(Item) Then
                        Dim arr As String() = New String(3) {}
                        Dim itm As ListViewItem
                        'add items to ListView
                        arr(0) = AbsoluteFilePath(Item)
                        arr(1) = FormatBytes(Item)
                        arr(2) = Item
                        itm = New ListViewItem(arr)
                        Me.ListView1.Items.Add(itm)
                        Me.ListView1.Items(Me.ListView1.CheckedItems.Count).Checked = True
                    End If
                Next
             End If
            Next
    Last edited by sinner0636; Dec 20th, 2017 at 09:10 AM.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,647

    Re: Listvew - Add Large Amount Of Files At One Time

    Try this...

    Code:
    Dim list As List(Of String) = ScanSpecialFolders(Path) ' this is the function i need to come up with
    Dim items() As ListViewItem = list.ConvertAll(Function(s As String) New ListViewItem(New String() {AbsoluteFilePath(s), FormatBytes(s), s})).toarray
    ListView1.Items.AddRange(items)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    21,647

    Re: Listvew - Add Large Amount Of Files At One Time

    If it's the ScanSpecialFolders function you need, you'll have to wait for someone else to answer...

  4. #4

    Thread Starter
    Addicted Member sinner0636's Avatar
    Join Date
    Sep 2009
    Posts
    233

    Re: Listvew - Add Large Amount Of Files At One Time

    This is what i come up with but need some tweaks if possible to make the code run smooth as possible


    Code:
        Public CancelScan As Boolean
        Public ItemCount As Integer
    
    Public Function ProcessSpecialFolders(ByVal L As ListView)
            Dim Cookies As String = (Environment.GetFolderPath(Environment.SpecialFolder.Cookies))
            Dim History As String = (Environment.GetFolderPath(Environment.SpecialFolder.History))
            Dim IECache As String = (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))
            Dim WinTemp As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Temp"
            Dim Prefetch As String = (Environment.GetFolderPath(Environment.SpecialFolder.Windows)) + "\Prefetch"
            Dim UserTemp As String = (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)) + "\Temp"
          
            Dim SpecialFolders As String() = {Cookies, History, IECache, WinTemp, Prefetch, UserTemp}
    
            'Path is our starting point
            For Each Path As String In SpecialFolders
                If DirExists(Path) Then
                    Dim list As List(Of String) = ScanSpecialFolders(Path)
    
                    For Each Item As String In list
                        If Not IsFileInUse(Item) Then
                            Dim arr As String() = New String(3) {}
                            Dim itm As ListViewItem
                            'add items to ListView
                            arr(0) = AbsoluteFilePath(Item)
                            arr(1) = FormatBytes(Item)
                            arr(2) = Item
                            itm = New ListViewItem(arr)
                            L.Items.Add(itm)
                            L.Items(L.CheckedItems.Count).Checked = True
                        End If
                    Next
                End If
            Next
        End Function

    Code:
        Public Function ScanSpecialFolders(ByVal Path As String)
            ' This list stores the results.
            Dim result As New List(Of String)
            ' This stack stores the directories to process.
            Dim stack As New Stack(Of String)
            ' Add the initial directory
            stack.Push(Path)
            ' Continue processing for each stacked directory
            Do While (stack.Count > 0)
                ' Get top directory string
                Try
                    Dim dir As String = stack.Pop
                    ' Add all immediate file path
                    'Cancel Scan
                    If CancelScan = True Then GoTo Kill
                    result.AddRange(IO.Directory.GetFiles(dir, "*.*"))
                    ' Loop through all subdirectories and add them to the stack.
                    Dim directoryName As String
                    For Each directoryName In IO.Directory.GetDirectories(dir)
                        'folder path
                        stack.Push(directoryName)
                    Next
                    'status path
                    Application.DoEvents()
                    ItemCount = ItemCount + 1
                Catch ex As Exception
                    'err
                End Try
            Loop
            ' Return the list
            Return result
    kill:
            Me.ToolStripStatusLabel1.Text = " Scan Stopped:"
            Exit Function
        End Function
    Called like this
    Code:
     Call ProcessSpecialFolders(Me.ListView1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width