【问题标题】:rename multiple file in a sequence c# or c++按顺序重命名多个文件 c# 或 c++
【发布时间】:2011-11-09 21:23:09
【问题描述】:

如何像这样重命名多个文件:

file.txt , anotherfile.txt , log.txt

变成这样:

file1.txt , file2.txt , file3.txt

如何在 c# 或 c++ 中做到这一点?

【问题讨论】:

  • 你需要用c#还是c++来做?这可以很容易地在 shell 脚本中完成,而且工作量要少得多。如果不需要c#或者c++,你是运行linux还是windows?
  • C++ 在语言中没有文件名和目录的概念,因此您需要依赖于平台的解决方案(或 Boost)。
  • man prename 如果你使用的是 linux ;)

标签: c# c++ rename


【解决方案1】:

在c++中,你最终会使用

std::rename(frompath, topath);

执行操作。 TR2 提案 N1975 涵盖了此功能。但是,在那之前,在不久的将来使用 boost::rename,在最终安置之前的批准后的时期使用 tr2::rename。

循环并使用您想要的任何名称。不太清楚您是否要添加数字,因为当前的问题是 1、2、2。

【讨论】:

    【解决方案2】:

    这适用于您使用基于 sh 的 shell:

    #!/bin/sh
    FEXT="txt"      # This is the file extension you're searching for
    FPRE="file"     # This is the base of the new files names file1.txt, file2.txt, etc.
    FNUM=1;         # This is the initial starting number
    
    find . -name "*.${FEXT}" | while read OFN ; do
        # Determine new file name
        NFN="${FPRE}${FNUM}.${FEXT}"
        # Increment FNUM
        FNUM=$(($FNUM + 1))
        # Rename File
        mv "${OFN}" "${NFN}"
    done
    

    正在运行的脚本:

    [james@fractal renfiles]$ touch abc.txt
    [james@fractal renfiles]$ touch test.txt
    [james@fractal renfiles]$ touch "filename with spaces.txt"
    [james@fractal renfiles]$ ll
    total 4
    -rw-rw-r-- 1 james james   0 Sep  3 17:45 abc.txt
    -rw-rw-r-- 1 james james   0 Sep  3 17:45 filename with spaces.txt
    -rwxrwxr-x 1 james james 422 Sep  3 17:41 renfiles.sh
    -rw-rw-r-- 1 james james   0 Sep  3 17:45 test.txt
    [james@fractal renfiles]$ ./renfiles.sh 
    [james@fractal renfiles]$ ll
    total 4
    -rw-rw-r-- 1 james james   0 Sep  3 17:45 file1.txt
    -rw-rw-r-- 1 james james   0 Sep  3 17:45 file2.txt
    -rw-rw-r-- 1 james james   0 Sep  3 17:45 file3.txt
    -rwxrwxr-x 1 james james 422 Sep  3 17:41 renfiles.sh
    

    【讨论】:

      【解决方案3】:

      File.Move Method 用作:

      IEnumerable<FileInfo> files = GetFilesToBeRenamed();
      int i = 1;
      foreach(FileInfo f in files)
      {
          File.Move(f.FullName, string.Format("file{0}.txt", i));
          i++;
      }
      

      如果f 是完整路径,那么您可以这样做:

      File.Move(f.FullName, 
               Path.Combine(f.Directory.ToString(), string.Format("file{0}.txt", i));
      

      【讨论】:

      • 大家好,谢谢大家的回答,但我认为我会选择 Nawaz
      【解决方案4】:

      在c#中你可以使用File.Move(source, dest)

      当然,您可以通过编程方式完成:

      string[] files = new string[] {"file.txt" , "anotherfile.txt" , "log.txt"}:
      int index = 0;
      string Dest;
      foreach (string Source in files)
      {
         if (!Files.Exists(Source )) continue;
         do {
             index++;
             Dest= "file"+i+".txt";
         } while (File.Exists(NewName);
      
         File.Move(Source , Dest); 
      }
      

      【讨论】:

        猜你喜欢
        • 2017-04-10
        • 2020-10-20
        • 2015-01-24
        • 2019-07-15
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        • 2014-04-22
        • 1970-01-01
        相关资源
        最近更新 更多