Page 1 of 2
Using .GetFiles
Posted: Mon Aug 29, 2011 3:45 am
by 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 allAccess 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.

Re: Using .GetFiles
Posted: Mon Aug 29, 2011 9:30 am
by comathi
Mybe put all your code between a "try" and "end try" and leave the "catch ex as exception" blank
Re: Using .GetFiles
Posted: Mon Aug 29, 2011 9:54 am
by 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
Re: Using .GetFiles
Posted: Mon Aug 29, 2011 11:02 am
by 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
Re: Using .GetFiles
Posted: Mon Aug 29, 2011 12:07 pm
by 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.
Re: Using .GetFiles
Posted: Mon Aug 29, 2011 12:09 pm
by mandai
I think we gathered that.
Re: Using .GetFiles
Posted: Tue Aug 30, 2011 3:29 am
by 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 allA 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.

Re: Using .GetFiles
Posted: Tue Aug 30, 2011 11:38 am
by 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.
Re: Using .GetFiles
Posted: Tue Aug 30, 2011 11:50 pm
by 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?
Re: Using .GetFiles
Posted: Wed Aug 31, 2011 10:34 am
by mandai
There is actually a typo in the code. I have updated this, you will need to swap these lines:
Code: Select allfolders = Directory.GetFiles(folder)
Code: Select allfolders = 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.