|
|
The (static) method in the tile (Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory) does the same thing and optionally provides the Windows Copy File Dialog with it.
Going to do something about this?
|
|
|
|
|
no comment.
|
|
|
|
|
<code>
private static void CopyDirectory(string trgDir, string srcDir)
{
string[] allFiles = Directory.GetFiles(srcDir, "*.*", SearchOption.AllDirectories);
foreach (string srcFile in allFiles) {
string targetFile = trgDir + srcFile.Substring(srcDir.Length);
string targetDir = Path.GetDirectoryName(targetFile);
if (Directory.Exists(targetDir)) {
Directory.CreateDirectory(targetDir);
}
File.Copy(srcFile, targetFile);
}
}
</code>
|
|
|
|
|
|
If you writing C# please use build-in tools like: Path.Combine()
you can change it here:
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
this is very disturbing to see code like this. Strings compare when you have build-in and (!) multyplatform command. So sad...
"I have not failed.
I've just found 10,000 ways that won't work."
- Thomas Alva Edison (1847-1931)
|
|
|
|
|
Looks like you are a BIG programmer?
|
|
|
|
|
You would like to check if the destination is a sub directory of the source. Otherwise you'll get an infinite loop, which will fill up your disk completely in minutes. This is one solution: (sorry I renamed the variable names to company standards)
public static void CopyDirectory(string src, string dst)
{
String[] files;
if (dst[dst.Length - 1] != Path.DirectorySeparatorChar)
dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(dst))
Directory.CreateDirectory(dst);
files = Directory.GetFileSystemEntries(src);
foreach (string element in files)
{
if (Directory.Exists(element))
{
if (element + Path.DirectorySeparatorChar != dst)
{
CopyDirectory(element, dst + Path.GetFileName(element));
}
}
else
{
File.Copy(element, dst + Path.GetFileName(element), true);
}
}
}
|
|
|
|
|
Umm, The check of same directory is necessary, but I think check the destinate folder is not necessary.
if (!Directory.Exists(dst))
Directory.CreateDirectory(dst);
|
|
|
|
|
Thanks for the convinient func !
I applied try/catch + ret value indicates if the copy succeed.
here is the updated func :
///
/// Copy directory structure recursively
///
public bool copyDirectory(string Src, string Dst)
{
String[] Files;
bool bSucess = true;
if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
Dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(Dst))
Directory.CreateDirectory(Dst);
Files = Directory.GetFileSystemEntries(Src);
foreach (string Element in Files)
{
try
{
// Sub directories
if (Directory.Exists(Element))
copyDirectory(Element, Dst + Path.GetFileName(Element));
// Files in directory
else
File.Copy(Element, Dst + Path.GetFileName(Element), true);
}
catch (Exception)
{
bSucess = false;
}
}
return bSucess;
}
|
|
|
|
|
Its such a common requirement im sure, but the .NET API doesnt support it. I googled for this functionality and hit this topic.
The code just works perfectly and I didnt even bother to understand how it was doing what it is doing. It seamlessly integrated into my program.
Thanks
Sreenath
|
|
|
|
|
I am working on a project that requires me to pick folders out of a source folder, and copy them to a destination. With both examples of the code for the copy, i'm getting the content of the folders i want copied into the root of the destiation without the folder being created.
for example
the path I am passing is src = \\test\share\nc11 and dest = \\test\dest\
what I'm getting is the contents of nc11 dumped into the root of dest
I want to copy nc11 to \\test\dest\
|
|
|
|
|
How can i exclude files .. i wonder because some hidden open file by someone else can't copy to mirror
|
|
|
|
|
This is precisely for what I was searching.
|
|
|
|
|
Hey's it's not that complex! Just use getfiles and getdirectories
<br />
<br />
private void CopyFolder( string source, string dest )<br />
{<br />
if (!dest.EndsWith("\\")) dest += "\\";<br />
<br />
foreach (string file in Directory.GetFiles( source ))<br />
{<br />
File.Copy(file, dest + Path.GetFileName(file), true);<br />
}<br />
<br />
foreach (string folder in Directory.GetDirectories( source ))<br />
{<br />
string subFolder = Path.GetFileName(folder);<br />
Directory.CreateDirectory(dest + "\\" + subFolder);<br />
CopyFolder(folder, dest + "\\" + subFolder);<br />
}<br />
}<br />
|
|
|
|
|
Here is similar but more portable:
public static void CopyDirectory(string source, string destination, bool overwrite)
{
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
DirectoryInfo dirInfo = new DirectoryInfo(source);
foreach (FileInfo fileInfo in dirInfo.GetFiles())
fileInfo.CopyTo(Path.Combine(destination, fileInfo.Name), overwrite);
foreach (DirectoryInfo subDirectoryInfo in dirInfo.GetDirectories())
CopyDirectory(subDirectoryInfo.FullName, Path.Combine(destination, subDirectoryInfo.Name), overwrite);
}
B.A.N.Z.A.I. ! !! !!!
|
|
|
|
|
Quick and easy little method to do exactly what I needed. Thanks.
Ignore the cocky buttheads who constantly have to come in and criticize why you did it this way or how you should add this functionality to make it better.
There are only 10 types of people in this world....those that understand binary, and those that do not.
|
|
|
|
|
Thanks a lot.
This piece of code was no way meant to impress people. Just helpful...
R. LOPES
Just programmer.
|
|
|
|
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace Project1
{
public class Util
{
public Util()
{
}
///
/// Filesystem
///
public class FileSystem
{
// Copy directory structure recursively
public static void copyDirectory(string Src,string Dst, string[] SrcFilterPattern)
{
ArrayList FileCol = null;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
FileCol = new ArrayList();
for (int i = 0; i < SrcFilterPattern.Length; i ++)
{
if (SrcFilterPattern[i].CompareTo("") == 0)
{
FileCol.AddRange(Directory.GetFileSystemEntries(Src));
}
FileCol.AddRange(Directory.GetFileSystemEntries(Src, SrcFilterPattern[i]));
}
foreach(string Element in FileCol)
{
// Sub directories
if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element), SrcFilterPattern);
// Files in directory
else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst, string fileFilterPattern)
{
try
{
Regex r = new Regex("(;|,)"); // Split on hyphens.
string[] fileFilters = r.Split(fileFilterPattern);
r = null;
copyDirectory(sDir, sDst, fileFilters);
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst)
{
try
{
copyDirectory(sDir, sDst, "");
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
static void Main(string[] args)
{
try
{
//copyDirectory(@"c:\temp",@"c:\temp\new", "*e.exe;*l.xml");
copyDirectory(@"c:\temp",@"c:\temp\new");
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
}
catch(Exception Ex)
{
Console.Error.WriteLine(Ex.Message);
}
}
}
}
}
|
|
|
|
|
Good addition indeed.
R. LOPES
Just programmer.
|
|
|
|
|
I modified it and post the modified part here:
because I tried to use it in my project, but failed(Directory.GetFileSystemEntries(Src, SrcFilterPattern[i]) got nothing at all.)
public static void CopyDirectory(string Src,string Dst, string[] SrcFilterPattern)
{
ArrayList FileCol = null;
ArrayList DirCol = null;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
FileCol = new ArrayList();
DirCol=new ArrayList();
for (int i = 0; i < SrcFilterPattern.Length; i ++)
{
if (SrcFilterPattern[i].CompareTo("") == 0)
{
FileCol.AddRange(Directory.GetFileSystemEntries(Src));
}
FileCol.AddRange(Directory.GetFiles(Src, SrcFilterPattern[i]));
}
DirCol.AddRange(Directory.GetDirectories(Src));
//Copy Files
foreach(string Element in FileCol)
{
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
//Copy SubDirectory
foreach(string element in DirCol)
{
CopyDirectory(element,Dst+Path.GetFileName(element), SrcFilterPattern);
}
}
http://hardrock.cnblogs.com
http://www.steedsoft.com
|
|
|
|
|
can you give me the example that uses the class file you have modified?
thanks
tirumal
|
|
|
|
|
Nothing special, just use like this way(I used it in my Batch ReaderEnable project):
<code>FileSystem.CopyDirectory(srcFolder,destFolder, "*.pdf");</code>
the full version class is:
<code>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace com.rubypdf
{
/// <summary>
///Simple C#/.NET tip to copy an entire directory tree to another directory
/// Function to copy a directory to another place (nothing fancy)
/// orginal from http://www.codeproject.com/cs/files/copydirectoriesrecursive.asp
/// Modified by Steven Lee(http://www.rubypdf.com)
/// </summary>
public class FileSystem
{
/// <summary>
// Copy directory structure recursively
/// </summary>
/// <summary>
/// Copy Directory recursively with given Filter Patterns
/// </summary>
/// <param name="Src">Source Dir</param>
/// <param name="Dst">Dest Dir</param>
/// <param name="SrcFilterPattern">String Array of Filter Pattern</param>
public static void CopyDirectory(string Src,string Dst, string[] SrcFilterPattern)
{
ArrayList FileCol = null;
ArrayList DirCol = null;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
FileCol = new ArrayList();
DirCol=new ArrayList();
for (int i = 0; i < SrcFilterPattern.Length; i ++)
{
if (SrcFilterPattern[i].CompareTo("") == 0)
{
FileCol.AddRange(Directory.GetFileSystemEntries(Src));
}
FileCol.AddRange(Directory.GetFiles(Src, SrcFilterPattern[i]));
}
DirCol.AddRange(Directory.GetDirectories(Src));
//Copy Files
foreach(string Element in FileCol)
{
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
//Copy SubDirectory
foreach(string element in DirCol)
{
CopyDirectory(element,Dst+Path.GetFileName(element), SrcFilterPattern);
}
}
/// <summary>
///Copy Directory recursively with a given Filter Pattern
/// </summary>
/// <param name="sDir">Source Dir</param>
/// <param name="sDst">Dest Dir</param>
/// <param name="fileFilterPattern">Filter Pattern string</param>
public static void CopyDirectory(string sDir,string sDst, string fileFilterPattern)
{
try
{
Regex r = new Regex("(;|,)"); // Split on hyphens.
string[] fileFilters = r.Split(fileFilterPattern);
r = null;
CopyDirectory(sDir, sDst, fileFilters);
}
catch (System.Exception excpt)
{
//Console.WriteLine(excpt.Message);
throw excpt;
}
}
/// <summary>
/// Copy directory structure recursively
/// </summary>
/// <param name="sDir">Source Dir</param>
/// <param name="sDst">Dest Dir</param>
public static void CopyDirectory(string sDir,string sDst)
{
try
{
CopyDirectory(sDir, sDst, "");
}
catch (System.Exception excpt)
{
//Console.WriteLine(excpt.Message);
throw excpt;
}
}
static void Main(string[] args)
{
try
{
//copyDirectory(@"c:\temp",@"c:\temp\new", "*e.exe;*l.xml");
copyDirectory(@"c:\temp",@"c:\temp\new");
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
}
catch(Exception Ex)
{
Console.Error.WriteLine(Ex.Message);
}
}
}
}
</code>
http://www.rubypdf.com
|
|
|
|
|
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text.RegularExpressions;
namespace Project1
{
public class Util
{
public Util()
{
}
///
/// Filesystem
///
public class FileSystem
{
// Copy directory structure recursively
public static void copyDirectory(string Src,string Dst, string[] SrcFilterPattern)
{
ArrayList FileCol = null;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
FileCol = new ArrayList();
for (int i = 0; i < SrcFilterPattern.Length; i ++)
{
if (SrcFilterPattern[i].CompareTo("") == 0)
{
FileCol.AddRange(Directory.GetFileSystemEntries(Src));
}
FileCol.AddRange(Directory.GetFileSystemEntries(Src, SrcFilterPattern[i]));
}
foreach(string Element in FileCol)
{
// Sub directories
if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element), SrcFilterPattern);
// Files in directory
else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst, string fileFilterPattern)
{
try
{
Regex r = new Regex("(;|,)"); // Split on hyphens.
string[] fileFilters = r.Split(fileFilterPattern);
r = null;
copyDirectory(sDir, sDst, fileFilters);
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
// Copy directory structure recursively
public static void copyDirectory(string sDir,string sDst)
{
try
{
copyDirectory(sDir, sDst, "");
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
static void Main(string[] args)
{
try
{
//copyDirectory(@"c:\temp",@"c:\temp\new", "*e.exe;*l.xml");
copyDirectory(@"c:\temp",@"c:\temp\new");
// Directory.Delete(@"c:\MySrcDirectory") to mimic a Directory.Move behaviour
}
catch(Exception Ex)
{
Console.Error.WriteLine(Ex.Message);
}
}
}
}
}
|
|
|
|
|
First of all its a great article and it saved me tons of time.
My task was to back up a huge folder on backup server. On production server I have a backup server's mapped drive as "s". Now everytime I try to backup the directory, I get an error message "Could not find a part of the path "s:\".
Any ideas how to take care of this issue?
Thanks
|
|
|
|
|