473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Recursive directory search in VB.NET

Good afternoon,

I'm currently in the process of designing an application that downloads a zipped directory, extracts it, recurses through the directory while searching for any files. If a file is found, it is moved to a new folder. I've gotten the code that will move the files working, but I'm having a lot of trouble recursing through the directory. My inexperience and lack of knowledge with VB is not helping.

My question, simply put, is how can I recurse through a directory and its subdirectories and search for any files they may contain?
Mar 14 '08 #1
8 4795
Plater
7,872 Expert 4TB
The Directory object will allow you to recurse.
Expand|Select|Wrap|Line Numbers
  1. string[] filenames = System.IO.Directory.GetFiles("path","search pattern", System.IO.SearchOption.AllDirectories)
  2.  
Mar 14 '08 #2
The Directory object will allow you to recurse.
Expand|Select|Wrap|Line Numbers
  1. string[] filenames = System.IO.Directory.GetFiles("path","search pattern", System.IO.SearchOption.AllDirectories)
  2.  

Thanks for the prompt response. Could you kindly explain the function of the "search pattern" parameter?

-B. Labello
Mar 14 '08 #3
Plater
7,872 Expert 4TB
It works just like the dir command in a command prompt.
"*.txt" for all files that end in .txt
"M*.*" all files that start with M
etc
etc
Mar 14 '08 #4
Thanks for the help, Plater.
Mar 14 '08 #5
Killer42
8,435 Expert 8TB
The Directory object will allow you to recurse.
Expand|Select|Wrap|Line Numbers
  1. string[] filenames = System.IO.Directory.GetFiles("path","search pattern", System.IO.SearchOption.AllDirectories)
  2.  
I'm curious whether this actually did resolve the issue. From another recent post, it seems as though GetFiles with the AllDirectories option doesn't give any indication of where the files were found, only their names. I suggested in the other thread that one could instead use GetDirectories to scan through the directories, and use a separate GetFiles for each, so you know where you are.

I can't try it out, as I've only got VB6.
Mar 27 '08 #6
balabaster
797 Expert 512MB
I'm curious whether this actually did resolve the issue. From another recent post, it seems as though GetFiles with the AllDirectories option doesn't give any indication of where the files were found, only their names.
The strings returned are the full path names... so even though there was no confirmation, I'm confident it provided everything needed.

A parse of my Inetpub\wwwroot folder with the all directories option selected returns an array with the following strings:

C:\Inetpub\wwwroot\Default.aspx
C:\Inetpub\wwwroot\OnCall\Default.aspx
C:\Inetpub\wwwroot\OnCall\OnCallEdit.aspx
C:\Inetpub\wwwroot\OnCall\OnCallView.aspx
C:\Inetpub\wwwroot\OnCall\PersonEdit.aspx
C:\Inetpub\wwwroot\OnCall\Admin\RebuildHierarchyFr omDB.aspx

Hope that clarifies things
Mar 27 '08 #7
Killer42
8,435 Expert 8TB
The strings returned are the full path names...
Thanks for that, balabaster.

That's really weird though. The problem encountered by the other person I mentioned was that it only provided the names with no paths, so they couldn't tell where the files came from. I admit, this did seem a surprising and pointless way for the function to operate. Perhaps it was a bug in some version that has since been fixed?

The workaround that I suggested apparently worked, but now I have no idea why it was needed.
Mar 28 '08 #8
Plater
7,872 Expert 4TB
There is an alternate call that returns FileInfo[] and if you did not use the correct combination of properties from THAT object, you would only get the name of the file, and not the directory hierarchy.
Mar 28 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: alanwo | last post by:
For recursive search for files, like http://support.microsoft.com/default.aspx?scid=KB;EN-US;306666, it may lead to "out of stack" error if searching too many files, say millions of files. Do you...
6
by: Talin | last post by:
I've been using generators to implement backtracking search for a while now. Unfortunately, my code is large and complex enough (doing unification on math expressions) that its hard to post a...
2
by: RML | last post by:
hi guys i have searched all day and cannot find the code i am looking for. i am new to c# so please bear with me ;) i want to write a console app that simly lists in the console window, all...
4
by: Elmo Watson | last post by:
Is there a way, with the System.IO class, to do a recursive list of a directory structure? For instance, in DirectoryInfo, you have GetDirectories and GetFiles .... In Directory, you have...
3
by: Gabe Matteson | last post by:
I am trying to set the maximum value of the progress bar so that when a user searches through the specified directory they can see their status. the progress bar name is on form2 and is named...
3
by: Robertico | last post by:
I'am new to php and have a question about a recursive file search. I'd like to do a recursive search for jpg-files and add the filenames (full path) to a mysql database. I appreciate any help ! ...
0
by: Tim Golden | last post by:
Robert Dailey wrote: I haven't checked but I doubt this has changed from Python 2.x. Just use os.walk and filter on what you want. Vaguest and most untested code sample: <code> import os...
0
by: Fredrik Lundh | last post by:
A. Joseph wrote: os.walk traverses the directory tree, so I'm not sure why you think that your program needs to use recursion? wouldn't a plain loop work? import os, shutil for dirpath,...
5
by: kyawsithu | last post by:
Hi, I am new to windows application. Can anyone give me the answer on my issue? The issue is updating the progressbar of bgworker from recursive loop. The recursive loop is using to find the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.