How to copy sub-folder from folder into another folder? - c#

I am working on C#.net console application in app i have two folders 1)D:\Working Projects\Alticore\AssetXML\LIS, 2)D:\Working Projects\Alticore\AssetXMLProcessed.
Now i want to copy only sub-folder(i.e LIS) from D:\Working Projects\Alticore\AssetXML\LIS to D:\Working Projects\Alticore\AssetXMLProcessed.
That is xaclty like this "D:\Working Projects\Alticore\AssetXMLProcessed/LIS".
Any solution to this problem I'll be appreciate.

Under Windows XP, it would be thus:
move "c:\documents and settings\%USERNAME%\desktop\TZClock" "C:\documents and settings\%USERNAME%\Start Menu\Programs\TZClock"
On Windows 7, it is the following (though I'm not in a position to test this right now):
move "c:\users\%USERNAME%\desktop\TZClock" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\TZClock"
you can execute this process or another thing create new file copy all data.

There is one more option what you can do is
there is
File.Copy(src, dest) method in the System.IO namespace you can also go with that.

This should do the job
public static void Copy(String srcPath, String destPath)
{
DirectoryInfo srcDirectory = new DirectoryInfo(srcPath);
if (!srcDirectory.Exists) return;
// Creates LIS directory
destPath = Path.Combine(Path.Combine(destPath, srcDirectory.Name));
Directory.CreateDirectory(destPath);
// Creates all sub directories from srcPath to your destPath
foreach (String dirPath in Directory.GetDirectories(srcPath, "*", SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(srcPath, destPath));
// Copies all files from all sub directories from srcPath to your destPath
foreach (String copyPath in Directory.GetFiles(srcPath, "*.*", SearchOption.AllDirectories))
File.Copy(copyPath, copyPath.Replace(srcPath, destPath), true);
}
Usage:
Copy(#"D:\Working Projects\Alticore\AssetXML\LIS", #"D:\Working Projects\Alticore\AssetXMLProcessed")
If you don't want to copy sub folders or its files remove not needed foreach.
By the way it will override copied files.

Related

Delete Files containing specific Text in Directory and Subdirectories

How to delete Files there names containing a specific string in a Directory and also all Subdirectories?
Given Filenames like:
EA myown EURJPY M15 3015494.mq5
EA myown EURJPY M15 3015494.ex5
EA self EURJPY M15 3098111 fine.mq5
EA self EURJPY M15 3098111 fine.ex5
Given Folderstructures like:
D:\TEMP\MYTEST
D:\TEMP\MYTEST\EURJPY
D:\TEMP\MYTEST\EURJPY\EURJPY_M15
Example: I want to delete ALL Files in all Subdirectories containing this String:
3015494
These Files are copied more than one time down of the Root-Folder "D:\TEMP\MYTEST" and also copied into the Subdirectories.
I try to write a little function for this. But i can delete Files into a given Folder, but not down into Subfolders ...
Last Code from me:
// call my function to delete files ...
string mypath = #"D:\TEMP\MYTEST\";
string myfilecontains = #"xx";
DeleteFile(mypath, true, myfilecontains);
// some code i found here and should delete just Files,
// but only works in Root-Dir.
// Also will not respect my need for Filename contains Text
public static bool DeleteFile(string folderPath, bool recursive, string FilenameContains)
{
//Safety check for directory existence.
if (!Directory.Exists(folderPath))
return false;
foreach (string file in Directory.GetFiles(folderPath))
{
File.Delete(file);
}
//Iterate to sub directory only if required.
if (recursive)
{
foreach (string dir in Directory.GetDirectories(folderPath))
{
//DeleteFile(dir, recursive);
MessageBox.Show(dir);
}
}
//Delete the parent directory before leaving
//Directory.Delete(folderPath);
return true;
}
What i have to change in this Code for my needs?
Or is there a complete different code something more helpfull?
I hope you have some good ideas for me to catch the trick.
DirectoryInfo dir = new DirectoryInfo(mypath);
// get all the files in the directory.
// SearchOptions.AllDirectories gets all the files in subdirectories as well
FileInfo[] files = dir.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo file in files)
{
if (file.Name.Contains(myfilecontains))
{
File.Delete(file.FullName);
}
}
This is similar to hossein's answer but in his answer if the directory name contains the value of myfilecontains that file will get deleted as well which I would think you don't want.
//get the list of files in the root directory and all its subdirectories:
string mypath = #"D:\TEMP\MYTEST\";
string myfilecontains = #"xx";
var files = Directory.GetFiles(mypath, "*", SearchOption.AllDirectories).ToList<string>();
//get the list of file for remove
var forDelete = files.Where(x => x.Contains(myfilecontains));
//remove files
forDelete.ForEach(x => { File.Delete(x); });
hope this helps!

How to copy and paste a whole directory to a new path recursively?

I want to move a directory as a copy/paste routine, by keeping its structure as it is. I am not looking only for the files within all subfolders in a directory then copy/paste them (as this solution), instead I want to clone the whole thing and keep its structure as it is (Tree -> subfolders and files), exactly like a copy and paste routine.
So I found this function that copies a folder full of files to a new path:
Folder -> File(s)
The function behaves as known as the copy/paste routine. It takes SourcePath, DestinationPath and boolean value to OverWriteExisting. Nice and small but too bad it wasn't marked as the actual answer of that question there (recommend a rate).
But what if I want to move a whole directory? In other words, what if I have a folder that has folders of folders of folders of files and etc? And maybe it is unknown the file structure tree size like this:
Folder -> Folder(s) -> ... -> Folder(s) -> File(s)
I am using the below routine to copy/paste a folder that has folders. But here I know that I only have one level of folders so only one foreach loop is required:
foreach (var Folder in DestinationFolder) // here I know that I have only one level of folders to reach the files
{
CopyDirectory(FolderPath, DestinationPath, false); // use that function to copy the files
}
This above function serves this directory structure:
Folder -> Folder(s) -> File(s)
I tried this and it didn't do what I want. I only retrieve all files while it searches all the subfolders. Which is not what I want. I want to keep the subfolders and the original structure as it is. Here I get four files instead of the directory structed as it is, subfolders and their subfolders, subfolders, files. Only four because it removes duplicates which I do not want this to happen because I need all of them.
Here is my current structure(but my question is global to any directory):
Folder -> Folders -> Folders + Files
Here is what the below code does in the new path:
NewFolder -> AllFilesFoundInAnySubfolder
dialog.FileName = dialog.FileName.Replace(".xml", ""); // get the destination path
DirectoryInfo dirInfo = new DirectoryInfo(dialog.FileName);
if (dirInfo.Exists == false)
Directory.CreateDirectory(dialog.FileName);
List<String> EverythingInTheDirectory = Directory
.GetFiles(FileStructure.baseSessionPath + "\\" + SelectedSession.Name, "*.*", SearchOption.AllDirectories).ToList(); // source
foreach (string file in EverythingInTheDirectory)
{
FileInfo mFile = new FileInfo(file);
// to remove name collusion
if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
mFile.MoveTo(dirInfo + "\\" + mFile.Name);
}
How to move the whole directory with unknown size and keep its structure as it is? Not get only the files from a directory and move them!
Here is an example that will recursively clone a directory to another destination directory.
class Program
{
static void Main(string[] args)
{
CloneDirectory(#"C:\SomeRoot", #"C:\SomeOtherRoot");
}
private static void CloneDirectory(string root, string dest)
{
foreach (var directory in Directory.GetDirectories(root))
{
//Get the path of the new directory
var newDirectory = Path.Combine(dest, Path.GetFileName(directory));
//Create the directory if it doesn't already exist
Directory.CreateDirectory(newDirectory);
//Recursively clone the directory
CloneDirectory(directory, newDirectory);
}
foreach (var file in Directory.GetFiles(root))
{
File.Copy(file, Path.Combine(dest, Path.GetFileName(file)));
}
}
}
A slight variation on code by #hawkstrider .
private static void CloneDirectory(DirectoryInfo source, DirectoryInfo dest) {
foreach (var source_subdir in source.EnumerateDirectories()) {
var target_subdir = new DirectoryInfo(Path.Combine(dest.FullName, source_subdir.Name));
target_subdir.Create();
CloneDirectory(source_subdir, target_subdir);
}
foreach (var source_file in source.EnumerateFiles()) {
var target_file = new FileInfo(Path.Combine(dest.FullName, source_file.Name));
source_file.CopyTo(target_file.FullName, true);
}
}
EDIT:
As #EtiennedeMartel correctly states, the Create() doesn't need the Exists check. ms-docs
Also Enumerations FTW!

Read all files in directory sub folders

I have a folder - "C:\scripts"
Within "scripts" I have several sub folders e.g. - "C:\scripts\subfolder1" "C:\scripts\subfolder2" etc, that contain html files.
I am trying to use the following code -
foreach (string file in Directory.EnumerateFiles(#"C:\scripts","*.html"))
{
string contents = File.ReadAllText(file);
}
However this does not work due to the html files being in the sub folders.
How can I access the html files in the sub folders without having to manually put in the path of each sub folder?
use this overload from DirectoryInfo
var dir = new DirectoryInfo(#"c:\scripts");
foreach(var file in dir.EnumerateFiles("*.html",SearchOption.AllDirectories))
{
}
Directory.EnumerateFiles(#"C:\scripts","*.html",SearchOption.AllDirectories)
Seems to be the right solution for me try it :)
Maybe this works?
foreach (string file in Directory.GetFiles("C:\\Scripts\\", "*.html", SearchOption.AllDirectories))
{
string contents = File.ReadAllText(file);
}
From SearchOption.AllDirectories
Includes the current directory and all its subdirectories in a search
operation. This option includes reparse points such as mounted drives
and symbolic links in the search.
Try like this;
var d = new DirectoryInfo(#"c:\scripts");
foreach(var fin d.EnumerateFiles("*.html", SearchOption.AllDirectories))
{
}

performance of copying directories

I am using C# to copy files from one directory to another directory.
I am using code from msdn but its pretty slow taking a minute or so to
copy a couple of gigs. It only takes seconds in explorer.
http://channel9.msdn.com/Forums/TechOff/257490-How-Copy-directories-in-C
Surly there a faster way..:)
private static void Copy(string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
private static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it's new directory.
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
Using Parallel I was able to copy 6gigs in under a minute faster than explorer and xcopy.
private static void CopyAll(string SourcePath, string DestinationPath)
{
string[] directories = System.IO.Directory.GetDirectories(SourcePath, "*.*", SearchOption.AllDirectories);
Parallel.ForEach(directories, dirPath =>
{
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
});
string[] files = System.IO.Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);
Parallel.ForEach(files, newPath =>
{
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
});
}
What u are using is recursion. It always slows the system.
Use this as it has no recursion.
void CopyAll (string SourcePath, string DestinationPath)
{
//Now Create all of the directories
foreach (string dirPath in Directory.GetDirectories(SourcePath, "*.*",
SearchOption.AllDirectories))
Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
//Copy all the files
foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
SearchOption.AllDirectories))
File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
}
Your issue is probabbly on *overwriteé of the file.
I see that you call
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
where true means overwrite the file if it exists.
Based on this I suppose so that it's a case in your app to overwrite already existing files.
The stuff should go faster if you
first rename destination directory with some temporary unique name
after just copy source to dest (so no overwrite)
if success remove temp directory, if fails remove all copied files and rename temp to its original name.
Should be faster.
Good luck.
I suspect #Oded is correct, and you're comparing a copy to a move.
If you want to ensure that what you're doing is the same as the shell does, you could look into SHFileOperation, or (on Vista or newer) IFileOperation. At least as far as I know, you'd have to use SHFileOperation via P/Invoke. IFileOperation is a COM interface.
There is a big problem with using Parallel.ForEach loops with creating directories, the first thing you need to be aware of, are sub directories nested within other directories, if Parallel creates directories out of order, the code can be thrown due to trying to create directory level 8 while directory level 6 was not even made yet.

Copy Folders in C# using System.IO

I need to Copy folder C:\FromFolder to C:\ToFolder
Below is code that will CUT my FromFolder and then will create my ToFolder.
So my FromFolder will be gone and all the items will be in the newly created folder called ToFolder
System.IO.Directory.Move(#"C:\FromFolder ", #"C:\ToFolder");
But i just want to Copy the files in FromFolder to ToFolder.
For some reason there is no System.IO.Directory.Copy???
How this is done using a batch file - Very easy
xcopy C:\FromFolder C:\ToFolder
Regards
Etienne
This link provides a nice example.
http://msdn.microsoft.com/en-us/library/cc148994.aspx
Here is a snippet
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
there is a file copy.
Recreate folder and copy all the files from original directory to the new one
example
static void Main(string[] args)
{
DirectoryInfo sourceDir = new DirectoryInfo("c:\\a");
DirectoryInfo destinationDir = new DirectoryInfo("c:\\b");
CopyDirectory(sourceDir, destinationDir);
}
static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination)
{
if (!destination.Exists)
{
destination.Create();
}
// Copy all files.
FileInfo[] files = source.GetFiles();
foreach (FileInfo file in files)
{
file.CopyTo(Path.Combine(destination.FullName,
file.Name));
}
// Process subdirectories.
DirectoryInfo[] dirs = source.GetDirectories();
foreach (DirectoryInfo dir in dirs)
{
// Get destination directory.
string destinationDir = Path.Combine(destination.FullName, dir.Name);
// Call CopyDirectory() recursively.
CopyDirectory(dir, new DirectoryInfo(destinationDir));
}
}
Copying directories (correctly) is actually a rather complex task especially if you take into account advanced filesystem techniques like junctions and hard links. Your best bet is to use an API that supports it. If you aren't afraid of a little P/Invoke, SHFileOperation in shell32 is your best bet. Another alternative would be to use the Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory method in the Microsoft.VisualBasic assembly (even if you aren't using VB).
yes you are right.
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx
has provided copy function ..
or you can use another function
http://msdn.microsoft.com/en-us/library/ms127960.aspx
You'll need to create a new directory from scratch then loop through all the files in the source directory and copy them over.
string[] files = Directory.GetFiles(GlobalVariables.mstrReadsWellinPath);
foreach(string s in files)
{
fileName=Path.GetFileName(s);
destFile = Path.Combine(DestinationPath, fileName);
File.Copy(s, destFile);
}
I leave creating the destination directory to you :-)
You're right. There is no Directory.Copy method. It would be a very powerful method, but also a dangerous one, for the unsuspecting developer. Copying a folder can potentionaly be a very time consuming operation, while moving one (on the same drive) is not.
I guess Microsoft thought it would make sence to copy file by file, so you can then show some kind of progress information. You could iterate trough the files in a directory by creating an instance of DirectoryInfo and then calling GetFiles(). To also include subdirectories you can also call GetDirectories() and enumerate trough these with a recursive method.
A simple function that copies the entire contents of the source folder to the destination folder and creates the destination folder if it doesn't exist
class Utils
{
internal static void copy_dir(string source, string dest)
{
if (String.IsNullOrEmpty(source) || String.IsNullOrEmpty(dest)) return;
Directory.CreateDirectory(dest);
foreach (string fn in Directory.GetFiles(source))
{
File.Copy(fn, Path.Combine(dest, Path.GetFileName(fn)), true);
}
foreach (string dir_fn in Directory.GetDirectories(source))
{
copy_dir(dir_fn, Path.Combine(dest, Path.GetFileName(dir_fn)));
}
}
}
This article provides an alogirthm to copy recursively some folder and all its content
From the article :
Sadly there is no built-in function in System.IO that will copy a folder and its contents. Following is a simple recursive algorithm that copies a folder, its sub-folders and files, creating the destination folder if needed. For simplicity, there is no error handling; an exception will throw if anything goes wrong, such as null or invalid paths or if the destination files already exist.
Good luck!
My version of DirectoryInfo.CopyTo using extension.
public static class DirectoryInfoEx {
public static void CopyTo(this DirectoryInfo source, DirectoryInfo target) {
if (source.FullName.ToLower() == target.FullName.ToLower())
return;
if (!target.Exists)
target.Create();
foreach (FileInfo f in source.GetFiles()) {
FileInfo newFile = new FileInfo(Path.Combine(target.FullName, f.Name));
f.CopyTo(newFile.FullName, true);
}
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
diSourceSubDir.CopyTo(nextTargetSubDir);
}
}
}
And use like that...
DirectoryInfo d = new DirectoryInfo("C:\Docs");
d.CopyTo(new DirectoryInfo("C:\New"));

Categories

Resources