13 Replies - 433 Views - Last Post: 09 July 2014 - 02:22 PM Rate Topic: -----

#1 MalwareMedic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 13

how to list all computer files

Posted 09 July 2014 - 05:57 AM

What I would like to do is list all files on your computer in a listbox so they would look like this: c:/file
I could never get all the files here are the two codes I used. FolderLocation was defined in code before as
(Environment.SystemDirectory, Me.ListBox1)
the first code worked, but only some files.

code1:
For Each s In My.Computer.FileSystem.GetFiles(FolderLocation)
            Try
                lstbox.Items.Add(s)
            Catch ex As Exception

            End Try
        Next
        For Each s In My.Computer.FileSystem.GetDirectories(FolderLocation)
            Try
                scanSubfolders(s, ListBox1)
            Catch ex As Exception

            End Try
        Next
    End Sub


code2:
For Each strDir As String In
         System.IO.Directory.GetDirectories("C:\")

                For Each strFile As String In System.IO.Directory.GetFiles(strDir)

                    ListBox1.Items.Add(strFile)


Is This A Good Question/Topic? 0
  • +

Replies To: how to list all computer files

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • Joined: 12-June 08

Re: how to list all computer files

Posted 09 July 2014 - 07:01 AM

Quote

I could never get all the files here are the two codes I used.

Explain this.
Was This Post Helpful? 0
  • +
  • -

#3 Sheepings  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 323
  • Joined: 05-December 13

Re: how to list all computer files

Posted 09 July 2014 - 09:57 AM

Example 1
Private Sub RunThis(ByVal PathLocation As String)
            For Each currentFile In Directory.GetFiles(PathLocation, "*", SearchOption.AllDirectories).Where(Function(f) GetExt(f) <> ".exe" OrElse GetExt(f) <> ".ini" OrElse GetExt(f) <> ".rar")
            'Do as you need with each
            Next
        End Sub
        Private Function GetExt(ByVal File As String) As String
            Return Path.GetExtension(File)
        End Function
Example 2
Private Sub CheckLocation(ByVal eCheckLocation As String)
            Dim MyFiles = Directory.GetFiles(eCheckLocation).Where(Function(f) GetExt(f) <> ".exe" OrElse GetExt(f) <> ".ini" OrElse GetExt(f) <> ".rar").ToArray
            Me.ListBox1.Items.Add(MyFiles.Count.ToString)
            For Each CurrentFile In MyFiles
                'Do what you need with each file
            Next
        End Sub
        Private Function GetExt(ByVal File As String) As String
            Return Path.GetExtension(File)
        End Function
Adapt to your desires.
Was This Post Helpful? 0
  • +
  • -

#4 MalwareMedic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 13

Re: how to list all computer files

Posted 09 July 2014 - 10:18 AM

View Postmodi123_1, on 09 July 2014 - 07:01 AM, said:

Quote

I could never get all the files here are the two codes I used.

Explain this.


I couldn't get it to find all the files and the two codes where the ones I used to try.
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 323
  • Joined: 05-December 13

Re: how to list all computer files

Posted 09 July 2014 - 10:46 AM

Did you read what I posted to you? Obviously not. Google SearchOption.AllDirectories VB.Net
Was This Post Helpful? 0
  • +
  • -

#6 MalwareMedic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 13

Re: how to list all computer files

Posted 09 July 2014 - 10:55 AM

[quote name='Sheepings' date='09 July 2014 - 09:57 AM' timestamp='1404925060' post='2029476']

Quote

Adapt to your desires.

I did what you said and changed it a bit here it is:

For Each currentFile In System.IO.Directory.GetFiles("c:/", "*", SearchOption.AllDirectories)
ListBox1.Items.Add(currentFile)



The problem is the ("*", SearchOption.AllDirectories) It refuses to find anything and without it, it won't scan all directories. I ran the program as admin to see if that would help, but still it didn't.

The second code you suggested had the same problem.(it didn't scan all directories)
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5191
  • View blog
  • Posts: 26,899
  • Joined: 10-May 07

Re: how to list all computer files

Posted 09 July 2014 - 11:01 AM

View PostSheepings, on 09 July 2014 - 01:46 PM, said:

Did you read what I posted to you?

He was replying to modi123_1.
Was This Post Helpful? 1
  • +
  • -

#8 MalwareMedic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 13

Re: how to list all computer files

Posted 09 July 2014 - 11:10 AM

View Postno2pencil, on 09 July 2014 - 11:01 AM, said:

He was replying to modi123_1.
Yes I was, thank you for cleaning up the forum anyway do you have any suggestions to fix my errors I can't get it to scan all directories.
Was This Post Helpful? 0
  • +
  • -

#9 Sheepings  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 323
  • Joined: 05-December 13

Re: how to list all computer files

Posted 09 July 2014 - 11:27 AM

View Postno2pencil, on 09 July 2014 - 07:01 PM, said:

He was replying to modi123_1.

I know. Which is why I asked if he seen what I gave to him.

Debugged and works fine my end.
Step into: Stepping over method without symbols 'System.Diagnostics.Debug.WriteLine'
C:\pagefile.sys
Step into: Stepping over method without symbols 'System.Windows.Forms.Button.onmouseup'

Also, check your / \ location. You are also not passing the location to the method.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CheckLocation(eCheckLocation:="C:\")
    End Sub
    Private Sub CheckLocation(ByVal eCheckLocation As String)
        Dim MyFiles = Directory.GetFiles(eCheckLocation).Where(Function(f) GetExt(f) <> ".exe" OrElse GetExt(f) <> ".ini" OrElse GetExt(f) <> ".rar").ToArray
        Me.ListBox1.Items.Add(MyFiles.Count.ToString)
        For Each CurrentFile In MyFiles
            'Do what you need with each file
            Debug.WriteLine(CurrentFile)
        Next
    End Sub
    Private Function GetExt(ByVal File As String) As String
        Return Path.GetExtension(File)
    End Function

Did you import system IO? What you need is SearchOption.AllDirectories.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8955
  • View blog
  • Posts: 33,571
  • Joined: 12-June 08

Re: how to list all computer files

Posted 09 July 2014 - 11:31 AM

Quote

The problem is the ("*", SearchOption.AllDirectories) It refuses to find anything and without it, it won't scan all directories. I ran the program as admin to see if that would help, but still it didn't.

Great googlie-mooglie. Turn on options strict and explicit.

Tools -> projects and solutions -> vb defaults.

Also throw a try/catch around it. You'll notice it is probably stopping with a "cannot access" message. It is an issue of permissions and using managed code. I am guessing pretty akin to the problem you had back 23 November 2013.

To get around this you will have to a. accept some folders you will not have permission to, and b. to utilize some recursion and get deal with directories/files you do not have access to gracefully.

Quote

The weakness in this approach is that if any one of the subdirectories under the specified root causes a DirectoryNotFoundException or UnauthorizedAccessException, the whole method fails and returns no directories. The same is true when you use the GetFiles method. If you have to handle these exceptions on specific subfolders, you must manually walk the directory tree, as shown in the following examples.

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

http://www.blackwasp...rRecursion.aspx
Was This Post Helpful? 1
  • +
  • -

#11 MalwareMedic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 13

Re: how to list all computer files

Posted 09 July 2014 - 11:45 AM

Thank you so much for helping I didn't try the two last suggestions because I got mine to work. Thank you for offering your time to help me and I would like to ask one simple question. How could I change c:/ to the defualt drive for the computer maybe something like this? Dim DDrive as (something)
Was This Post Helpful? 0
  • +
  • -

#12 Sheepings  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 323
  • Joined: 05-December 13

Re: how to list all computer files

Posted 09 July 2014 - 11:54 AM

Dim DriveC As String = "C:\"

But you should look up System IO and special directories on MSDN.

Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
        Dim allDrives() As DriveInfo = DriveInfo.GetDrives()

        Dim d As DriveInfo
        For Each d In allDrives
            Console.WriteLine("Drive {0}", d.Name)
            Console.WriteLine("  File type: {0}", d.DriveType)
            If d.IsReady = True Then
                Console.WriteLine("  Volume label: {0}", d.VolumeLabel)
                Console.WriteLine("  File system: {0}", d.DriveFormat)
                Console.WriteLine( _
                    "  Available space to current user:{0, 15} bytes", _
                    d.AvailableFreeSpace)

                Console.WriteLine( _
                    "  Total available space:          {0, 15} bytes", _
                    d.TotalFreeSpace)

                Console.WriteLine( _
                    "  Total size of drive:            {0, 15} bytes ", _
                    d.TotalSize)
            End If 
        Next 
    End Sub 
End Class 

Was This Post Helpful? 0
  • +
  • -

#13 MalwareMedic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-November 13

Re: how to list all computer files

Posted 09 July 2014 - 11:59 AM

View PostSheepings, on 09 July 2014 - 11:54 AM, said:

Dim DriveC As String = "C:\"

iv
Not everyone has there default drive set as c so I would like DDrive to repersent the default drive.

This post has been edited by andrewsw: 09 July 2014 - 02:28 PM

Was This Post Helpful? 0
  • +
  • -

#14 Sheepings  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 323
  • Joined: 05-December 13

Re: how to list all computer files

Posted 09 July 2014 - 02:22 PM

I gave you an example and recommended you the relevant starter documentation to look-up. If you do some of your own homework, you can actually find out which drive is hosting windows.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1