Using .GetFiles

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
15 posts Page 1 of 2
Contributors
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

Using .GetFiles
2cool4cereal2
Hello Code'N'Stuff members.

For the past few days I have been trying to find a way that I could use ".GetFiles" along with a progress bar. However, at this point a progress bar is not even the majority of my worries, but instead, why .getfiles is not working.
Code: Select all
  For Each f As String In My.Computer.FileSystem.GetFiles("H:\", FileIO.SearchOption.SearchAllSubDirectories, "*.exe")



            If userCancel Then
                GetAllFiles_BGW.CancelAsync()
                Exit Sub
            End If

            If My.Computer.FileSystem.FileExists(f) Then
                allFiles.Add(f)
            End If
        Next
This is my current coding, and I know it works correctly if "H:\" is changed to something like, "H:\Folder", however, since I want to search the entire drive, I wish to leave it just, "H:\". As of right now, when I attempt to debug the program, I receive this error:
Code: Select all
Access to the path 'H:\System Volume Information\' is denied.
As of right now, I need to find a way to skip 'H:\System Volume Information\' and continue to scan for the other files, while allowing "f" to still be used as a string for ".FileExists" and "allfiles.add"


Thank you for your help, and if anyone has any more questions, please feel free to ask. :)
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Using .GetFiles
comathi
Mybe put all your code between a "try" and "end try" and leave the "catch ex as exception" blank
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Using .GetFiles
Axel
comathi wrote:
Mybe put all your code between a "try" and "end try" and leave the "catch ex as exception" blank
why ? I'd rather check if the directory exists using Directory.Exists(path) than let the exception be there

I would do this for the progressbar:
Set the maximum value of the progressbar to the amount of files in the directory
for each file add 1 to the progressbar's value
http://vagex.com/?ref=25000
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Using .GetFiles
mandai
It is not clear how a progressbar/looking to see if the directory exists will resolve this issue.

My.Computer.FileSystem.GetFiles will cause an exception if you don't have access to any of the subfolders, then it will stop.

For it to continue, you will need to use a recursive function with better error correction instead:
Code: Select all
    Dim userCancel As Boolean = False

    Sub recursive_search(ByVal folder As String)

        If userCancel Then
            Return
        End If

        Dim files As String()
        Try
            files = Directory.GetFiles(folder)
        Catch ex As Exception
            MsgBox("Error listing files in " & folder)
            GoTo skipfiles
        End Try

        For i As Integer = 0 To files.Length - 1
            Dim ext As String = Path.GetExtension(files(i))
            If ext = ".exe" Then
                'do whatever
            End If
        Next

skipfiles:
        Dim folders As String()
        Try
            folders = Directory.GetDirectories(folder)
        Catch ex As Exception
            MsgBox("Error listing folders in " & folder)
            Return
        End Try

        For i As Integer = 0 To folders.Length - 1
            recursive_search(folders(i))
        Next
    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        recursive_search("H:\")
    End Sub
Last edited by mandai on Wed Aug 31, 2011 10:51 am, edited 1 time in total.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Using .GetFiles
MrAksel
Axel wrote:
why ? I'd rather check if the directory exists using Directory.Exists(path) than let the exception be there
The problem isnt that the directory dont exists, but he does not have access to the path.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Using .GetFiles
mandai
I think we gathered that.
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

Re: Using .GetFiles
2cool4cereal2
Okay, so now I have the following:
Code: Select all
 Sub recursive_search(ByVal folder As String)

        If userCancel Then
            Return
        End If

        Dim files As String()
        Try
            files = Directory.GetFiles(folder)
        Catch ex As Exception

            GoTo skipfiles
        End Try

        For i As Integer = 0 To files.Length - 1
            Dim ext As String = Path.GetExtension(files(i))
            If ext = "*.exe" Then
                My.Computer.FileSystem.CreateDirectory("H:\Copy")
                My.Computer.FileSystem.CopyFile(files.ToString, "H:\Copy")

            End If
        Next

skipfiles:
        Dim folders As String()
        Try
            folders = Directory.GetFiles(folder)
        Catch ex As Exception

            Return
        End Try

        For i As Integer = 0 To folders.Length - 1
            recursive_search(folders(i))
        Next
    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        recursive_search("H:\")
    End Sub
However, the code does not seem to find any .exe files to backup, alothough I know they exist. Instead in the "Immediate Window" I get tons of
Code: Select all
A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
but this is understandable from the error catchers. Any suggestions as to what I should do from here?

Thanks. :)
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Using .GetFiles
mandai
CopyFile requires valid destination and source file names. Also you don't want to look for an asterisk character in the ext variable as wildcard characters are not used there.

You should also only create the directory once.
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

Re: Using .GetFiles
2cool4cereal2
Okay, I understand the asterisk thing, however, how do I get a valid path and source file names from this coding?

And what did you mean by create the directory only once?
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Using .GetFiles
mandai
There is actually a typo in the code. I have updated this, you will need to swap these lines:
Code: Select all
folders = Directory.GetFiles(folder)
Code: Select all
folders = Directory.GetDirectories(folder)
You will need to add a destination folder parameter to the recursive_search function if you want to keep the same directory structure.

You are using CreateDirectory to create the same directory for each file in the loop. There is no need to do this if the path does not change.
15 posts Page 1 of 2
Return to “Tutorial Requests”