copy directory

P

Puja

hi all,

how can I copy directory c:\test to a directory c:\backup with all files,
folders and subfolders that are contained within test.

please advice!

thanks
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Puja said:
how can I copy directory c:\test to a directory c:\backup with all files,
folders and subfolders that are contained within test.

Are you asking for a XCOPY method written in C# ?

Arne
 
V

VJ

need to write a recurisive function i.e a function that calls itself. Give
sometime, if you need a outline function I can give it..!

VJ
 
V

VJ

If you wanted to copy from a directory to another here is a code..

private bool CopyDirectory(string strSrceDir , string strDestDir )
{
try
{
DirectoryInfo SourceDir = new DirectoryInfo(strSrceDir);
DirectoryInfo DestDir = new DirectoryInfo(strDestDir);

if ( !DestDir.Exists )
DestDir.Create();

foreach( FileInfo ChildFile in SourceDir.GetFiles() )
{
ChildFile.CopyTo(Path.Combine(DestDir.FullName, ChildFile.Name), true);
}

foreach( DirectoryInfo SubDir in SourceDir.GetDirectories() )
{
if ( !SubDir.Exists )
SubDir.Create();
bool retVal;
retVal = CopyDirectory(SubDir.FullName,Path.Combine(DestDir.FullName,
SubDir.Name));
if (!retVal)
return false;
}

return true;

}
catch (Exception ex)
{
//log any error...
throw;
}
}
 
P

Puja

Thanks for the reply,

Arne, I don't know about XCopy funtion but am looking for some code which can do this.

VJ, If you could give me, that would be great.

thanks
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Puja said:
Arne, I don't know about XCopy funtion but am looking for some code
which can do this.

It is a DOS/Windows command line command.

Code is something like:

private void XCopy(string dir1, string dir2)
{
string[] files = Directory.GetFiles(dir1);
foreach (string f in files) {
File.Copy(f, dir2 + f.Substring(dir1.Length), true);
}
string[] dirs = Directory.GetDirectories(dir1);
foreach (string d in dirs) {
XCopy(d, dir2 + d.Substring(dir1.Length));
}
}

I can not remember if it creates destination dirs that does
not exist. If not insert a Directory.Create call in the
method.

Arne
 
V

VJ

Its there already..

VJ
Thanks for the reply,

Arne, I don't know about XCopy funtion but am looking for some code which can do this.

VJ, If you could give me, that would be great.

thanks
 
J

Jon Skeet [C# MVP]

VJ said:
If you wanted to copy from a directory to another here is a code..

I'm intrigued by your boolean return value. The method will only return
false if a "deeper" call to it returns false... so when will that
happen?
 
V

VJ

It will happen at any point a copy of a file or dir fails, and the copy will
stop at that point.

No, am I missing something, would like to learn.!

VJ
 
J

Jon Skeet [C# MVP]

VJ said:
It will happen at any point a copy of a file or dir fails, and the copy will
stop at that point.
No, am I missing something, would like to learn.!

How? Where will it ever return false unless a nested call returns
false? If (as I believe) that's the only way it can return false, no
"leaf" call can ever return false, which means that no "higher" call
can return false either.

They can throw exceptions, of course, but that's not the same as
returning false.

If you want to give a counterexample, try to construct a series of
calls which will result in it returning false...
 
W

Willy Denoyette [MVP]

VJ said:
If you wanted to copy from a directory to another here is a code..

Beware that doing as you showed, won't preserve the security attributes (DACL's and SACL's)
of the input folders and files.
Many have been bitten by this!

Willy.
 
W

Willy Denoyette [MVP]

Puja said:
hi all,

how can I copy directory c:\test to a directory c:\backup with all files, folders and
subfolders that are contained within test.

please advice!

thanks

run the xcopy.exe utillity.

Willy.
 
V

VJ

Yes sure this one we thought of, and have leverage...
John good point, will try, thanks again for sharing the information.
Appreciate it.

VJ
 
M

MiloTheGreat

private static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
{
foreach (DirectoryInfo dir in source.GetDirectories())
CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name));
foreach (FileInfo file in source.GetFiles())
file.CopyTo(Path.Combine(target.FullName, file.Name));
}


http://01792.org/blog/post/2010/10/06/Recursive-file-and-folder-copy.aspx
hi all,

how can I copy directory c:\test to a directory c:\backup with all files,
folders and subfolders that are contained within test.

please advice!

thanks
Are you asking for a XCOPY method written in C# ?

Arne
It is a DOS/Windows command line command.

Code is something like:

private void XCopy(string dir1, string dir2)
{
string[] files = Directory.GetFiles(dir1);
foreach (string f in files) {
File.Copy(f, dir2 + f.Substring(dir1.Length), true);
}
string[] dirs = Directory.GetDirectories(dir1);
foreach (string d in dirs) {
XCopy(d, dir2 + d.Substring(dir1.Length));
}
}

I can not remember if it creates destination dirs that does
not exist. If not insert a Directory.Create call in the
method.

Arne
On Thursday, March 22, 2007 2:16 AM Jon Skeet [C# MVP] wrote:

I'm intrigued by your boolean return value. The method will only return
false if a "deeper" call to it returns false... so when will that
happen?
On Thursday, March 22, 2007 12:53 PM Jon Skeet [C# MVP] wrote:

How? Where will it ever return false unless a nested call returns
false? If (as I believe) that's the only way it can return false, no
"leaf" call can ever return false, which means that no "higher" call
can return false either.

They can throw exceptions, of course, but that's not the same as
returning false.

If you want to give a counterexample, try to construct a series of
calls which will result in it returning false...
 

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