1つのディレクトリ内のすべてのコンテンツを別のディレクトリにコピーするにはどうしたらよいですか?ディレクトリ内のすべてのファイルをコピーする
答えて
できません。しかし、あなたのような簡潔なコードを使用することができますDirectory.GetFiles(mydir).ToList().ForEach(f => File.Copy(f, otherdir + "\\" f);
これは再帰的には機能しません。 –
することはできません。 Directory
でもDirectoryInfo
も、Copy
メソッドを提供しません。これを自分で実装する必要があります。
void Copy(string sourceDir, string targetDir)
{
Directory.CreateDirectory(targetDir);
foreach(var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)));
foreach(var directory in Directory.GetDirectories(sourceDir))
Copy(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
}
この単純なアプローチのいくつかの問題に注意してください。
MSDNは、この上の指針を持っている - How to:Copy Directories
外部コマンドとして実行しxcopy source_directory\*.* destination_directory
。もちろん、これはWindowsマシンでのみ動作します。
不要な場合は、システムコールを使用しないでください。ここでは、必ずしも必要ではありません。 –
質問**が各ファイルを繰り返して**出てくることを伝える必要があります。 – m0skit0
xcopyは何をやっていると思いますか?彼は簡単な方法があると思ったので、ループしたくなかっただけです。ありません。システムコールは、一般的には簡単ではなく、良い方法でもありません。強くお勧めします! –
あなたは、タスクを簡素化するためにVBのFileSystem.CopyDirectoryメソッドを使用することができます。
using Microsoft.VisualBasic.FileIO;
foo(){
FileSystem.CopyDirectory(directoryPath, tempPath);
}
私はVBが間違っています。そして、人々はあなたがC#ですべて同じことをすることができると言います。 –
Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"resources\html")
.ToList()
.ForEach(f => File.Copy(f, folder + "\\" + f.Substring(f.LastIndexOf("\\"))));
using System.IO;
string sourcePath = @"D:\test";
string targetPath = @"D:\test_new";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
foreach (var srcPath in Directory.GetFiles(sourcePath))
{
//Copy the file from sourcepath and place into mentioned target path,
//Overwrite the file if same file is exist in target path
File.Copy(srcPath, srcPath.Replace(sourcePath, targetPath), true);
}
これは素晴らしい作品!サブディレクトリをコピーするか、すべてのサブディレクトリのすべてのファイルを1つの場所にダンプするだけです。
/// AUTHOR : Norm Petroff
/// <summary>
/// Takes the files from the PathFrom and copies them to the PathTo.
/// </summary>
/// <param name="pathFrom"></param>
/// <param name="pathTo"></param>
/// <param name="filesOnly">Copies all files from each directory to the "PathTo" and removes directory.</param>
static void CopyFiles(String pathFrom, String pathTo, Boolean filesOnly)
{
foreach(String file in Directory.GetFiles(pathFrom))
{
// Copy the current file to the new path.
File.Copy(file, Path.Combine(pathTo, Path.GetFileName(file)), true);
// Get all the directories in the current path.
foreach (String directory in Directory.GetDirectories(pathFrom))
{
// If files only is true then recursively get all the files. They will be all put in the original "PathTo" location
// without the directories they were in.
if (filesOnly)
{
// Get the files from the current directory in the loop.
CopyFiles(directory, pathTo, filesOnly);
}
else
{
// Create a new path for the current directory in the new location.
var newDirectory = Path.Combine(pathTo, new DirectoryInfo(directory).Name);
// Copy the directory over to the new path location if it does not already exist.
if (!Directory.Exists(newDirectory))
{
Directory.CreateDirectory(newDirectory);
}
// Call this routine again with the new path.
CopyFiles(directory, newDirectory, filesOnly);
}
}
}
}
- 1. リスト内のすべてのファイルを一意のディレクトリにコピー
- 2. ディレクトリ内のすべてのファイルの移動/コピー
- 3. すべてのファイルをディレクトリから別のディレクトリにコピー
- 4. wix - ディレクトリ全体とその内容をすべてコピーする
- 5. あるFTPディレクトリから別のFTPディレクトリにすべてのファイルをコピーする
- 6. ディレクトリ内のすべてのフォルダとファイル
- 7. BZ2ディレクトリ内のすべてのファイル
- 8. Mavenでは、target/surefire-reportsディレクトリ内のすべてのファイルを単一のディレクトリにコピーする方法
- 9. WinCEでRAPI経由でディレクトリ内のすべてのファイルを検索/コピー
- 10. ディレクトリのすべてのフォルダにファイルをコピーします。
- 11. ディレクトリ内のすべてのワークブックにExcelシートをコピー
- 12. ファイルリスト内のディレクトリの内部からファイルをコピーする
- 13. Grunt.jsコピーを使用して、ディレクトリから別のディレクトリにすべてのファイルをコピーします。
- 14. ディレクトリ内のファイルのみをコピーします。ディレクトリはコピーしません。
- 15. zipファイル内のディレクトリからファイルをコピーする
- 16. ディレクトリ内のすべての.Rmdファイルのすべてのターゲットをレンダリングするメイクファイル
- 17. ディレクトリ内のすべてのファイルの名前を取得する
- 18. ディレクトリ内のすべてのファイルのリストを取得する(Perl)
- 19. ディレクトリ内のすべてのファイルの数を表示するスクリプト
- 20. ディレクトリとすべてのサブディレクトリ内のすべてのXMLファイルを解析する
- 21. Python Rarディレクトリ内のすべてのファイル、各ファイルの異なるディレクトリ
- 22. フォルダ内のすべてのファイルをコピーするShell API?
- 23. FAT:ディレクトリ内のすべてのファイルを一覧表示する
- 24. Win 32、スクリプト、ディレクトリ内のすべてのファイルを処理する
- 25. ディレクトリ内のすべてのFortranファイルを検索するBashコマンド
- 26. ディレクトリ内のすべてのファイルを実行するvbScript
- 27. ディレクトリ内のすべてのファイルを表示するJenkinsコマンド?
- 28. Laravel File Storageディレクトリ内のすべてのファイルを削除する
- 29. フォルダ内のすべてのディレクトリ/ファイルを取得する
- 30. ディレクトリ内のすべてのファイルを一覧表示するPython
ここを見て:http://stackoverflow.com/questions/206323/how-to-execute-command-line-in-c-get-std-out-resultsとコマンドのためにそこに置く「コピー\ *。* YOURDIR " – fritzone