הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, August 26, 2016 3:22 PM
How do I get all *.txt" or any type/s of files inside a given directory(including all said *.txt files inside the sub-directories if exists) and add it to a string array? I've tried
Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory, "*.txt")
Where targetDirectory is a string containing the folder path, but using it I only get the files of the parent directory and not the files of sub-directories? Can anyone help me on this?
All replies (6)
Friday, August 26, 2016 3:28 PM ✅Answered | 2 votes
Try this:
Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory, "*.txt", SearchOption.AllDirectories)
Friday, August 26, 2016 3:32 PM
The following gets all files but can be adapted to get files by file extensions
https://msdn.microsoft.com/en-us/library/ff477033%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Friday, August 26, 2016 3:55 PM
Don,
If you happen to run into this at some point:
https://msdn.microsoft.com/en-us/library/system.unauthorizedaccessexception(v=vs.110).aspx
... know that you're not the first to do so.
I'll show you a way around that (many hundreds of lines of code) if these other examples don't work for you, but just as a head's up I thought I'd toss this in here. :)
Some people succeed because they were destined to, but most people succeed because they were determined to.
Friday, August 26, 2016 4:00 PM
Thanks mate :)
Friday, August 26, 2016 4:11 PM
Dear Viorel_,
Can you also tell me how can I find say *.txt files inside a given folder inside which there are folders in the structure
- 12345\30123\128\txt\100.txt
- 12345\30123\129\txt\105.txt
- 1245\323\1281\txt\200.txt
and there could be other txt files just inside the main folder whose path I have given or in some other subfolders in the path, but I only want to get the *.txt files which are in the above mentioned folder format i.e. only *.txt files inside the txt folders. How do I do that? I know its a bit annoying but if you can help, I'll really appreciate it...
Thanks in advance.
Friday, August 26, 2016 5:31 PM
Try this:
Dim files =
From d In Directory.EnumerateDirectories(targetDirectory, "txt", SearchOption.AllDirectories)
From f In Directory.EnumerateFiles(d, "*.txt")
Select f
The results (a sequence of found paths) can be enumerated with For Each etc. If you want to wait and get an array, then add this:
Dim txtFilesArray As String() = files.ToArray()