Get files in folder and all sub folders

dannyres

Well-known member
Joined
Aug 29, 2003
Messages
67
Hi guys im wondering if anyone has some code that will return all files in a folder (i already know how todo this with directory.getfiles or whatever it is) but how can i get all the files in a directory and all its sub-folders...


Thanks, Dan
 
GetDirectories will get all the directories within a directory, with GetFiles you have all the information you need.... are you trying to do it in once sentance or something?
 
You will have to do a recursive function that gets the files in the current directory and then calls itself once per directory in it.
 
can u guys tell more on it... if possible got any examples to let me see? i really need to do this.. ive been looking for help all over.. hoping someone can really help...

i wanted to search through a root folder for files in the sub folders within folders of the root folders..

i need to pass the filenames and their directories into an Access2000 table[.mdb].

Can u guys please help me? Im very new to VB.NET and i need to get this done by VB.NET... thanks in advance...
 
To search all the files on a computer would make your Access table very large, if nobody has gotton back with you tonight Ill work up an example in the morning. May I ask why you would want to keep all that info in a database? As I said, thats a lot of information to track, especially if your starting at the root and going through every sub folder until you hit bottom. Also are going to want to track the full path, or just files names and where theyre located doesnt matter. There might be an easier way to do whatever it is your trying to accomplish is what Im saying, its been on a very rare occasion that I needed to go through a files system, and even then it was more of for a search rather than for tracking purposes, then that location would be saved in registry value or a file.
 
Okay man, this goes through all the folders and files on your E: drive if you have one, change if necessary; this is in C#, but you should be able to translate into VB without a problem. Pay particular attention to the Try/Catch statements! I have a P2 1.0Ghz and my E: drive is only about 600Mbs and it executed pretty quick; however on a typical C: drive it will take longer, especially if your adding this stuff to tables. If you can make since of this, putting the code in for putting it into a database shouldnt be a problem for you. There are probably more efficient codes for doing this, but I scraped it out in 5 minutes so you could move on with your project and didnt really think it through:

C#:
class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			GetFiles("E:/");
			GetSubFolders("E:/");
			Console.Read();
		}
		static void GetFiles(string directory)
		{
			try
			{
				string [] files = Directory.GetFiles(directory);
				Console.WriteLine("Files in folder \"{0}\"", directory);
				foreach(string f in files)
				{
					Console.WriteLine(f);
				}
			}
			catch(System.UnauthorizedAccessException)
			{
				Console.WriteLine("Access not allowed to \"{0}\"", directory);
			}

		}
		static void GetSubFolders(string directory)
		{
			try
			{
				//Get files for this folder
				GetFiles(directory);
				string [] subFolders = Directory.GetDirectories(directory);
				foreach(string sf in subFolders)
				{
					GetSubFolders(sf);
				}
			}
			catch(System.UnauthorizedAccessException)
			{
				Console.WriteLine("Access not allowed to \"{0}\"", directory);
			}
		}
	}
 
yeah, see looking back at this this couldve been in one function, also my try-catch shouldve been in the loop itself because as it stands if it encounters an error the function is exited and there still maybe umpteen sub-folder past the one it is checking to check. Like I said I whipped it out without really thinking about it.
 
Back
Top