2013-10-31 19 views
16

Các bạn đang cố di chuyển tất cả các tệp kết thúc bằng _DONE vào một thư mục khác.Di chuyển các tệp từ một thư mục này sang thư mục khác C#

tôi đã cố gắng

//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); 

Nhưng trên thực hiện các mã tôi nhận được một câu nói lỗi.

The given path's format is not supported. 

Tôi đã gặp sự cố ở đâu?

+0

Tôi không chắc liệu tệp truyền trong cửa sổ có hỗ trợ '_' – Rohit

Trả lời

15

Dấu gạch chéo của bạn sai hướng. Trên cửa sổ, bạn nên sử dụng dấu gạch chéo ngược. Ví dụ.

string rootFolderPath = @"F:\model_RCCMREC\"; 
string destinationPath = @"F:\model_RCCMrecTransfered\"; 
+0

Ý của bạn là" Trên cửa sổ bạn không nên sử dụng dấu gạch chéo "? – Skylark

+0

Tôi đã làm. Cảm ơn bạn đã sửa ngữ pháp của tôi từ 4 năm trước. Tôi chắc chắn việc làm rõ bổ sung này sẽ hữu ích cho người khác. – codemonkeh

+0

Kinda lạ với rất nhiều lưu lượng truy cập nó đã không được ghi nhận trước đây. – Skylark

6

Mảng tên tệp được trả lại từ System.IO.Directory.GetFiles() bao gồm đường dẫn đầy đủ của chúng. (Xem http://msdn.microsoft.com/en-us/library/07wt70x2.aspx) Điều này có nghĩa là việc thêm các thư mục nguồn và đích vào giá trị file sẽ không phải là những gì bạn mong đợi. Bạn sẽ kết thúc với các giá trị như F:\model_RCCMREC\F:\model_RCCMREC\something_DONE.wav trong fileToMove. Nếu bạn đặt điểm ngắt trên đường dây File.Move(), bạn có thể xem các giá trị bạn đang chuyển, điều này có thể giúp gỡ lỗi một tình huống như thế này.

Tóm lại, bạn sẽ cần phải xác định đường dẫn tương đối từ rootFolderPath cho mỗi tệp để xác định đường dẫn đích thích hợp. Hãy xem qua lớp System.IO.Path (http://msdn.microsoft.com/en-us/library/system.io.path.aspx) để biết các phương pháp sẽ hữu ích. (Cụ thể, bạn nên xem xét Path.Combine() thay vì + để xây dựng đường dẫn.)

+0

cảm ơn bạn, Xong;) –

+0

đây là câu trả lời đúng. – richardwhatever

0

Vui lòng thử chức năng dưới đây. Điều này hoạt động tốt.

Chức năng:

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)); 

     } 
} 
0

tôi đã thực hiện nó theo cách này:

if (Directory.Exists(directoryPath)) 
{ 
    foreach (var file in new DirectoryInfo(directoryPath).GetFiles()) 
    { 
     file.MoveTo([email protected]"{newDirectoryPath}\{file.Name}"); 
    } 
} 

tập tin là một loại lớp FileInfo. Nó đã có một phương thức được gọi là MoveTo() mà có một đường dẫn đích.

Các vấn đề liên quan