简体   繁体   English

以序列C#或C ++重命名多个文件

[英]rename multiple file in a sequence c# or c++

How can I rename multiple files like this: 我该如何重命名多个文件:

file.txt , anotherfile.txt , log.txt

into something like this : 变成这样的东西:

file1.txt , file2.txt , file3.txt

How can I do this in c# or in c++ ? 如何在C#或C ++中做到这一点?

Use File.Move Method as: 使用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++;
}

And if f is a fullpath, then you can do this instead: 如果f是全路径,则可以改为执行以下操作:

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

This would work in you're using an sh-based shell: 这将在您使用基于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

The script in action: 运行中的脚本:

[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

In c++, you will eventually use 在c ++中,您最终将使用

std::rename(frompath, topath);

to perform the action. 执行动作。 TR2 proposal N1975 covers this function. TR2建议N1975涵盖了此功能。 However, until then, use boost::rename for the immediate future, and tr2::rename for the period after approval before final placement. 但是,在此之前,将boost :: rename用于近期,将tr2 :: rename用于批准之后的最终放置期间。

Loop through and use whatever names you want. 循环浏览并使用您想要的任何名称。 Don't quite know if you're trying to add numbers, because the current question says 1, 2, 2. 不太知道您是否要添加数字,因为当前问题显示1、2、2。

In c# you can use File.Move(source, dest) 在C#中,您可以使用File.Move(source,dest)

Of course you can do it programatically: 当然,您可以通过编程方式做到这一点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM