按顺序c#或c+重命名多个文件+;

按顺序c#或c+重命名多个文件+;,c#,c++,rename,C#,C++,Rename,如何重命名多个文件,如下所示: file.txt , anotherfile.txt , log.txt 变成这样: file1.txt , file2.txt , file3.txt >可以使用< /P> 当然,您可以通过编程方式进行: string[] files = new string[] {"file.txt" , "anotherfile.txt" , "log.txt"}: int index = 0; string Dest; foreach (string Source

如何重命名多个文件,如下所示:

file.txt , anotherfile.txt , log.txt
变成这样:

file1.txt , file2.txt , file3.txt
<如何在C或C++中实现这一点?在C语言中,

< P> >可以使用< /P> 当然,您可以通过编程方式进行:

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); 
}
在c中,您可以使用

当然,您可以通过编程方式进行:

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); 
}
用作:

用作:


如果您使用的是基于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

如果您使用的是基于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

在C++中,你最终将使用

std::rename(frompath, topath);
执行动作。TR2提案N1975涵盖此功能。但是,在那之前,在不久的将来使用boost::rename,在最终安置之前的批准后使用tr2::rename


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

< P> C++,你最终会使用< /P>
std::rename(frompath, topath);
执行动作。TR2提案N1975涵盖此功能。但是,在那之前,在不久的将来使用boost::rename,在最终安置之前的批准后使用tr2::rename


循环并使用您想要的任何名称。不知道你是否想添加数字,因为当前的问题是1, 2, 2。< /P>你需要用C语言还是C++来做?这可以在一个shell脚本中轻松地完成,所需的工作量要小得多。如果不需要在C或C++中运行,你是在运行Linux还是Windows?C++没有一个文件名和目录的概念,所以你需要一个依赖于平台的解决方案(或者Boost)。你需要用C语言还是C++来做?这可以在一个shell脚本中轻松地完成,所需的工作量要小得多。如果不需要在C或C++中运行,你是在运行Linux还是Windows?C++没有一个文件名和目录的概念,所以你需要一个依赖于平台的解决方案(或者Boost)。嗨,谢谢你们的回答,但我想我会和纳瓦兹一起去。谢谢你们的回答,但我想我会和纳瓦兹一起去