John Collins
John Collins

Reputation: 5

System.IO.Directory.GetFiles doesn't find any files

I am having an issue where I have 2 GetFiles that don't seem to be able to find any files. I have commented the 2 problem lines. I have 1 file in the sourcedir and 1 in a subfolder of the sourcedir. If I take off FileIO.SearchOption.SearchAllSubDirectories, then it will find the file in the sourcedir, but not in subfolders. With FileIO.SearchOption.SearchAllSubDirectories, it finds no files at all. I am running VS2013 as Admin.

'For some reason, this finds no files--> '

Dim xFilesCount = System.IO.Directory.GetFiles(sourcedir, FileIO.SearchOption.SearchAllSubDirectories).Length
Dim xFilesTransferred As Integer = 0

'For some reason, this finds no files'

For Each xFiles In System.IO.Directory.GetFiles(sourcedir, FileIO.SearchOption.SearchAllSubDirectories)
     System.IO.File.Move(xFiles, xNewLocation & "\" & System.IO.Path.GetFileName(xFiles))
     xFilesTransferred += 1                    

Edit:

Thank you very much! I ended up using the following code, in case someone else runs across this same issue.

Dim xFilesCount = System.IO.Directory.GetFiles(sourcedir, "*.pf_import", System.IO.SearchOption.AllDirectories).Length
            Dim xFilesTransferred As Integer = 0

            For Each xFiles In System.IO.Directory.GetFiles(sourcedir, "*.pf_import", System.IO.SearchOption.AllDirectories)

                System.IO.File.Move(xFiles, xNewLocation & "\" & System.IO.Path.GetFileName(xFiles))
                xFilesTransferred += 1

                ProgressBar1.Value = xFilesTransferred * 100 / xFilesCount
                ProgressBar1.Update()
                Application.DoEvents()
            Next

Upvotes: 0

Views: 2090

Answers (2)

CptnVic
CptnVic

Reputation: 11

You don't specify where: sourcedir is - but if it is a root directory you will crash into the special folders pretty quickly (my documents, my music, etc.) with SearchOption.AllDirectories.

If that is the case you should be seeing an "unauthorizedAccessException" (largely because those directories don't actually exist) which you will need to catch in order to proceed with the other "non-special" folders. Then, you can re-visit those folders (MyDocuments for example) like:

Dim myPath As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments.ToString

(That will return something like: c:\users\cptnvic\documents)

Then, something like:

For Each foundFile As String In My.Computer.FileSystem.GetDirectories(myPath, FileIO.SearchOption.SearchTopLevelOnly, "*.*")

will return a list of folders to recurse. You only need to append those folders to myPath. This method does not require any administrative changes (for the current user).

Good luck!

Upvotes: 1

miroxlav
miroxlav

Reputation: 12194

Use

System.IO.Directory.GetFiles(sourcedir, "*", System.IO.SearchOption.AllDirectories)

Problem #1

Currently you are missing one argument.

Because GetFiles() signature is:

GetFiles(String, String, SearchOption)

Important:

Please place Option Strict On as the first line of your code module. This will help you to avoid these errors. Ideally, set Option Strict On in project options so it will be applied to all files. Never work on larget projects without Option Explicit On and Option Strict On. (I also recommend Option Infer Off.)

Problem #2

Correct constant to use with GetFiles() is

System.IO.SearchOption.AllDirectories

and not

FileIO.SearchOption.SearchAllSubDirectories

Happy coding!

Upvotes: 2

Related Questions