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.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
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.
Thank you for your help, and if anyone has any more questions, please feel free to ask.
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
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: 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
Code: Select all
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"Access to the path 'H:\System Volume Information\' is denied.
Thank you for your help, and if anyone has any more questions, please feel free to ask.

Mybe put all your code between a "try" and "end try" and leave the "catch ex as exception" blank
comathi wrote:Mybe put all your code between a "try" and "end try" and leave the "catch ex as exception" blankwhy ? 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
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:
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.
Axel wrote:why ? I'd rather check if the directory exists using Directory.Exists(path) than let the exception be thereThe 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]()
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!

Okay, so now I have the following:
Thanks.
Code: Select all
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 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
Code: Select all
but this is understandable from the error catchers. Any suggestions as to what I should do from here? A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
Thanks.

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.
You should also only create the directory once.
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?
And what did you mean by create the directory only once?
There is actually a typo in the code. I have updated this, you will need to swap these lines:
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.
Code: Select all
folders = Directory.GetFiles(folder)
Code: Select all
You will need to add a destination folder parameter to the recursive_search function if you want to keep the same directory structure.folders = Directory.GetDirectories(folder)
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.
Copyright Information
Copyright © Codenstuff.com 2020 - 2023