Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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?
Posted

can u guys tell more on it... if possible got any examples to let me see? i really need to do this.. i've 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? I'm very new to VB.NET and i need to get this done by VB.NET... thanks in advance...

Posted
To search all the files on a computer would make your Access table very large, if nobody has gotton back with you tonight I'll 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, that's 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 they're located doesn't matter. There might be an easier way to do whatever it is your trying to accomplish is what I'm saying, it's 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.
Posted

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 shouldn't 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 didn't really think it through:

 

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);
		}
	}
}

Posted
yeah, see looking back at this this could've been in one function, also my try-catch should've 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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...