Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-14-2005, 10:24 PM   PM User | #1
weronpc
Regular Coder

 
Join Date: Apr 2003
Location: Canada, Ontario, Mississauga
Posts: 312
Thanks: 0
Thanked 0 Times in 0 Posts
weronpc is an unknown quantity at this point
VB.Net, how to copy folder contents

Hello, I am having a .Net problem, not sure this is posible or not, please help.

How do I copy a folder and all the sub folder and files within the folder.

example, I want to copy C:/hello, in folder hello, there is folder hi, in folder hi, there is a file called mike.txt

how do I copy everything in C:/hello?

Thank you
weronpc is offline   Reply With Quote
Old 02-15-2005, 07:42 AM   PM User | #2
Dr. Evil
New Coder

 
Join Date: Nov 2004
Location: Netherlands
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Dr. Evil is an unknown quantity at this point
I don't know all the calls and such in VB, but basically you want to start with a FindFirstFile(), FindNextFile() loop (or whatever the finding functions are in VB) and when you come accross a folder, recurse into that folder and/or create one in the destination folder. If what you hit was a file, copy it to the needed destination. It's kind of hard to explain, and this code sample's in C, but maybe you can get the picture of what you could do from here:

Code:
#include <windows.h>
#include <stdio.h>

int FindFilesWithinDirectory(LPSTR start_dir)
{
	WIN32_FIND_DATA find_data;
	HANDLE find_h;
	DWORD find_ret;
	int ret;
	char *file_name, begin_again[MAX_PATH];
	
	ret = 0;
	find_h = FindFirstFile(start_dir, &find_data);
	if(find_h != NULL)
	{
		printf("Files within %s\n", start_dir);
		do
		{
			if((file_name = strrchr(find_data.cFileName, '\\')) != NULL) file_name++;
			else file_name = find_data.cFileName;
			
			if((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
				(strcmp(file_name, "..")) /*make sure we don't back track*/ && 
				(strcmp(file_name, "."))) /*no point in going into our own directory*/
			{
				strcpy(begin_again, start_dir);
				begin_again[(strlen(begin_again)-3)] = 0; //get rid of previous *.*
				strcat(begin_again, find_data.cFileName);
				strcat(begin_again, "\\*.*");
				
				ret = FindFilesWithinDirectory(begin_again);
			}
			else puts(file_name);
			
			if(ret) break;
			if(!FindNextFile(find_h, &find_data)) find_ret = GetLastError();
			else find_ret = 0;
		}
		while(find_ret != ERROR_NO_MORE_FILES);
		FindClose(find_h);
	}
	else ret = 1;
	
	return ret;
}

int main(int argc, char *argv[])
{
	char begin[10], *error = 0;
	
	strcpy(begin, getenv("SystemDrive"));
	strcat(begin, "\\*.*");
	
	switch(FindFilesWithinDirectory(begin))
	{
		case 0 : break;
		case 1 : error = "Invalid directory or no specified files within such a directory"; break;
		default : error = "Unknown error"; break;
	}
	if(error) puts(error);
	
	return 0;
}
That just simply lists all the files within the system drive, but very little modification is needed to make it copy all the files and folders.

Last edited by Dr. Evil; 02-15-2005 at 07:46 AM..
Dr. Evil is offline   Reply With Quote
Old 02-15-2005, 03:38 PM   PM User | #3
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
To go with the Dr's example, heres a good Recursive example in .NET

Recursive
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:57 AM.


Advertisement
Log in to turn off these ads.