Ответ 1
Ваши косые черты в неправильном направлении. В окнах вы должны использовать обратные косые черты. Например.
string rootFolderPath = @"F:\model_RCCMREC\";
string destinationPath = @"F:\model_RCCMrecTransfered\";
Ребята Я пытаюсь переместить все файлы, заканчивающиеся на _DONE, в другую папку.
Я пробовал
//take all files of main folder to folder model_RCCMrecTransfered
string rootFolderPath = @"F:/model_RCCMREC/";
string destinationPath = @"F:/model_RCCMrecTransfered/";
string filesToDelete = @"*_DONE.wav"; // Only delete WAV files ending by "_DONE" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach (string file in fileList)
{
string fileToMove = rootFolderPath + file;
string moveTo = destinationPath + file;
//moving file
File.Move(fileToMove, moveTo);
Но при выполнении этих кодов я получаю сообщение об ошибке.
The given path format is not supported.
Где я ошибся?
Ваши косые черты в неправильном направлении. В окнах вы должны использовать обратные косые черты. Например.
string rootFolderPath = @"F:\model_RCCMREC\";
string destinationPath = @"F:\model_RCCMrecTransfered\";
Массив имен файлов, возвращаемых с System.IO.Directory.GetFiles()
, включает их полный путь. (См. http://msdn.microsoft.com/en-us/library/07wt70x2.aspx). Это означает, что добавление исходных и целевых каталогов в значение file
не будет тем, что вы ожидаете. В итоге вы получите значения F:\model_RCCMREC\F:\model_RCCMREC\something_DONE.wav
в fileToMove
. Если вы установите точку останова на строке File.Move()
, вы можете посмотреть на значения, которые вы передаете, что может помочь отладить такую ситуацию.
Вкратце, вам нужно определить относительный путь от rootFolderPath
к каждому файлу, чтобы определить правильный путь назначения. Взгляните на класс System.IO.Path
(http://msdn.microsoft.com/en-us/library/system.io.path.aspx) для методов, которые помогут. (В частности, для построения путей вам следует рассмотреть Path.Combine()
, а не +
.)
Попробуйте выполнить функцию ниже. Это прекрасно работает.
Функция:
public static void DirectoryCopy(string strSource, string Copy_dest)
{
DirectoryInfo dirInfo = new DirectoryInfo(strSource);
DirectoryInfo[] directories = dirInfo.GetDirectories();
FileInfo[] files = dirInfo.GetFiles();
foreach (DirectoryInfo tempdir in directories)
{
Console.WriteLine(strSource + "/" +tempdir);
Directory.CreateDirectory(Copy_dest + "/" + tempdir.Name);// creating the Directory
var ext = System.IO.Path.GetExtension(tempdir.Name);
if (System.IO.Path.HasExtension(ext))
{
foreach (FileInfo tempfile in files)
{
tempfile.CopyTo(Path.Combine(strSource + "/" + tempfile.Name, Copy_dest + "/" + tempfile.Name));
}
}
DirectoryCopy(strSource + "/" + tempdir.Name, Copy_dest + "/" + tempdir.Name);
}
FileInfo[] files1 = dirInfo.GetFiles();
foreach (FileInfo tempfile in files1)
{
tempfile.CopyTo(Path.Combine(Copy_dest, tempfile.Name));
}
}