簡體   English   中英

通過在舊文件名c#中添加一個遞增的數字來重命名文件

[英]rename file by adding an incrementing number to old file name c#

我正在用C#編寫我的第一個Cocoa應用程序,它假定在文件名開頭添加/添加數字。

用戶僅給出文件夾的路徑(例如,帶有音樂的路徑),並且對於文件夾中包含的每個文件,該程序都想添加遞增數字,例如

001_(old_fileName),
002_(old_fileName), 
..., 
092_(old_fileName)
etc,

直到給定文件夾中的文件末尾(按路徑)。

無法拆分文件名,因為文件名未知(甚至可能包含數字本身)。 我嘗試了幾種可能的解決方案來解決此問題,但都不可行。 發現很少有人問過用c#更改名稱的問題,但沒有結果實際上對我有所幫助。

下面的代碼是我目前剩下的代碼,所有無效的嘗試都首先被注釋,然后被刪除。 通過NSAlert,我看到了文件夾中每個文件的路徑/名稱作為幫助。 我很樂意得到幫助

void RenameFunction()
{
   string sPath = _Path_textBox.StringValue;

   if (Directory.Exists(sPath))
   {
      var txtFiles = Directory.EnumerateFiles(sPath);
      var txt2Files = Directory.GetFiles((sPath));


      string fileNameOnly = Path.GetFileNameWithoutExtension(sPath);
      string extension = Path.GetExtension(sPath);
      string path = Path.GetDirectoryName(sPath);
      string newFullPath = sPath;
      int count = 1;


      while (File.Exists(sPath))//newFullPath))
      {
         string tempFileName = string.Format(count + "_" + fileNameOnly + sPath);
         //count++;
         //string.Format("{0}{0}{0}{1}", fileNameOnly, count++);
         newFullPath = Path.Combine(path, extension + tempFileName);
         count++;
      }

      string[] fileEntities = Directory.GetFiles(newFullPath);  //GetFileSystemEntries(sPath);//GetFiles(sPath);   
      //foreach (var _songName in fileEntities)
      //{
      //    string tempFileName = count + "_" + fileNameOnly + sPath;
      //            //string.Format("{0}{0}{0}{1}", fileNameOnly, count++);
      //        newFullPath = Path.Combine(sPath ,extension + tempFileName);
      //    File.Move(sPath, newFullPath);
      //}
      foreach (var _songName in fileEntities)
      {

         AmountofFiles(_songName);
      }
   }
}

void AmountofFiles(string path)
{
   var alert2 = new NSAlert();
   alert2.MessageText = "mp3";
   alert2.InformativeText = "AMOUNT OF MP3 FILES IS '{1}' : " + path;
   alert2.RunModal();
}

您嘗試使用File.Move嗎? 只是將文件移到相同的路徑,但是使用另一個名稱

File.Move("NameToBeRename.jpg","NewName.jpg");

您共享的代碼有很多不對的地方。 要實現的目標可以使用非常簡單的方法來實現。

您需要做的就是從目錄中檢索具有完整路徑的所有文件名,並一一重命名。

按照下面的代碼演示上述方法。

// Path of the directory.
var directroy = _Path_textBox.StringValue;

if (Directory.Exists(directroy))
{
    //Get all the filenames with full path from the directory.
    var filePaths = Directory.EnumerateFiles(directroy);

    //Variable to append number in front of the file name.
    var count = 1;

    //Iterate thru all the file names 
    foreach (var filePath in filePaths)
    {
        //Get only file name from the full file path.
        var fileName = Path.GetFileName(filePath);
        //Create new path with the directory path and new file name.
        var destLocation = Path.Combine(directory, count + fileName);

        //User File.Move to move file to the same directory with new name.
        File.Move(filePath, destLocation);

        //Increment count.
        count++;
    }
}

希望這可以幫助您解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM