1

環境:Visual Studio 2010、Windowsフォームアプリケーション。

やあ!いくつかのファイルの名前を変更(バッチ処理)したい... 1。私は(約50000ファイル)を持っています:abc.mp3、def.mp3、ghi.mp3欲しい:abc1.mp3、def1.mp3、ghi1.mp3

2.私は(約50000ファイル)を持っています:abc.mp3、def.mp3、ghi.mp3欲しい:1abc.mp3、1def.mp3、1ghi.mp3

似たような...

    FolderBrowserDialog folderDlg = new FolderBrowserDialog();
    folderDlg.ShowDialog();

    string[] mp3Files = Directory.GetFiles(folderDlg.SelectedPath, "*.mp3");
    string[] newFileName = new string[mp3Files.Length];

    for (int i = 0; i < mp3Files.Length; i++)
    {
        string filePath = System.IO.Path.GetDirectoryName(mp3Files[i]);
        string fileExt = System.IO.Path.GetExtension(mp3Files[i]);

        newFileName = mp3Files[i];

        File.Move(mp3Files[i], filePath + "\\" + newFileName[1] + 1 + fileExt);
    }

しかし、このコードは機能しません。ここでエラーが発生しました...newFileName = mp3Files[i]; 正しく変換できません。ありがとう!

4

3 に答える 3

4

最速のオプションは、直接OSの名前変更機能を使用することです。プロセスオブジェクトを使用して、/Cスイッチを使用してシェルCMDを実行します。「ren」コマンドラインの名前変更を使用します。

Process cmd = new Process()
{
    StartInfo = new ProcessStartInfo()
    {
        FileName = "cmd.exe",
        Arguments = @"/C  REN c:\full\path\*.mp3 c:\full\path\1*.mp3"
    }
};

cmd.Start();
cmd.WaitForExit();

//Second example below is for renaming with file.mp3 to file1.mp3 format
cmd.StartInfo.Arguments = @"/C  REN c:\full\path\*.mp3 c:\full\path\*1.mp3";
cmd.Start();
cmd.WaitForExit();
于 2012-09-18T04:43:46.053 に答える
2

代わりにこのコードを試してください:

Directory.GetFiles(folderDlg.SelectedPath, "*.mp3")
    .Select(fn => new
    {
        OldFileName = fn,
        NewFileName = String.Format("{0}1.mp3", fn.Substring(fn.Length - 4))
    })
    .ToList()
    .ForEach(x => File.Move(x.OldFileName, x.NewFileName));
于 2012-09-18T04:52:24.563 に答える
0

コメントで説明されている友人のように、newFileNameを(文字列の配列ではなく)単純な文字列として宣言するか、配列を使用する場合は以下のコードを使用できます。

newFileName[i] = mp3Files[i];

また、forループを使用しているため、文字列の配列ではなく文字列を使用することをお勧めします。

于 2012-09-18T04:47:48.047 に答える