Was the best way to create path a copy files

W

William S

I know there a few different and probably betters way to accomplish what I'm
doing.

I've got file a network drive, that I want to copy to my local drive, and I
want to keep the same path (UNC) structure.

For instance, I've got ...
z:\parentfolder\subfolder1\subfolder2\file1.txt
z:\parentfolder\subfolder1\subfolder2\file2.txt
z:\parentfolder\subfolder1\subfolder2\file3.txt

and i want to move everything to my local disk so I'll have

c:\parentfolder\subfolder1\subfolder2\file1.txt
c:\parentfolder\subfolder1\subfolder2\file2.txt
c:\parentfolder\subfolder1\subfolder2\file3.txt


I've written code to recursively get the folder names then apending them to
a string, then written another recursive function to create that folder
structure on my local disk, then with a for loop I copy the file from source
to destination.

This seem like a lot of code, and time to me when I think .NET should
provide some easier methods to accomplish such a trivial task. Maybe I'm
missing something.

Thanks for reading
 
J

joecool1969

I know there a few different and probably betters way to accomplish what I'm
doing.

I've got file a network drive, that I want to copy to my local drive, andI
want to keep the same path (UNC) structure.

For instance, I've got ...
z:\parentfolder\subfolder1\subfolder2\file1.txt
z:\parentfolder\subfolder1\subfolder2\file2.txt
z:\parentfolder\subfolder1\subfolder2\file3.txt

and i want to move everything to my local disk so I'll have

c:\parentfolder\subfolder1\subfolder2\file1.txt
c:\parentfolder\subfolder1\subfolder2\file2.txt
c:\parentfolder\subfolder1\subfolder2\file3.txt

I've written code to recursively get the folder names then apending them to
a string, then written another recursive function to create that folder
structure on my local disk, then with a for loop I copy the file from source
to destination.

This seem like a lot of code, and time to me when I think .NET should
provide some easier methods to accomplish such a trivial task.  Maybe I'm
missing something.

Thanks for reading

It was my impression that a:

Directory.CreateDirectory("c:\parnetfolder\subfolder1\subfolder2");

would create all subfolders if they did not exist.
 
I

Ignacio Machin ( .NET/ C# MVP )

I know there a few different and probably betters way to accomplish what I'm
doing.

I've got file a network drive, that I want to copy to my local drive, andI
want to keep the same path (UNC) structure.

For instance, I've got ...
z:\parentfolder\subfolder1\subfolder2\file1.txt
z:\parentfolder\subfolder1\subfolder2\file2.txt
z:\parentfolder\subfolder1\subfolder2\file3.txt

and i want to move everything to my local disk so I'll have

c:\parentfolder\subfolder1\subfolder2\file1.txt
c:\parentfolder\subfolder1\subfolder2\file2.txt
c:\parentfolder\subfolder1\subfolder2\file3.txt

I've written code to recursively get the folder names then apending them to
a string, then written another recursive function to create that folder
structure on my local disk, then with a for loop I copy the file from source
to destination.

This seem like a lot of code, and time to me when I think .NET should
provide some easier methods to accomplish such a trivial task.  Maybe I'm
missing something.

Thanks for reading

Hi,

You have to do it anyway, going recursive I mean. And if you do not do
it, then somebody else (the framework, the API) will have to do it for
you.

AFAIK there is no a method for that in the framework. Either you do it
(as you did) or maybe you could use xcopy from the OS
 
I

Ignacio Machin ( .NET/ C# MVP )

It was my impression that a:

Directory.CreateDirectory("c:\parnetfolder\subfolder1\subfolder2");

would create all subfolders if they did not exist.

Yes it will
But what about the files???
You still need to recursively navigate the folders to copy the files
 
J

joecool1969

Yes it will
But what about the files???
You still need to recursively navigate the folders to copy the files

I woudl first generate a list of full path's for each source file.

then loop through the file list.

1. use fileinfo class to get the directory the current file is in, or
a Path method to get it.
2. if the target directory does not exist, create it.
3. copy the file.
 
W

William S

Thanks for all the tips

I was given a utility that at one point did the same thing I'm trying to
accomplish today, only it was from the DOS era and written in Pascal, and
no longer works. I thought with the .NET framework there would be some
methods that would make it a bit easier, since it seems I'm using the same
philosophy that was used 15-20 years ago.
 
D

Dude

Thanks for all the tips

I was given a utility that at one point did the same thing I'm trying to
accomplish today,  only it was from the DOS era and written in Pascal, and
no longer works.  I thought with the .NET framework there would be some
methods that would make it a bit easier, since it seems I'm using the same
philosophy that was used 15-20 years ago.

If you are looking for a utility. look into rsync.

http://rsync.samba.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top