#include <cstdlib>
#include <iostream>
#include <dirent.h>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
DIR *dirp=NULL;
struct dirent *dp = NULL;
char doing_file[1024];
dirp = opendir("F:/Dev-Cpp/changename");
FILE *doing_fd;
if(dirp == NULL)
{
printf("打开文件失败!/n/n");
system("PAUSE");
return EXIT_SUCCESS;
}
while( (dp = readdir(dirp)) != NULL )
{
// printf("..readdir:%s/n",dp->d_name);
sprintf(doing_file,"%s",dp->d_name);
string filename_old(doing_file); //改名前的文件名
string filename_new(doing_file); //改名后的文件
string::size_type point = filename_new.rfind("bmp");
if(point != string::npos)
{
filename_new.replace(point,3,"jpg");
if(rename(filename_old.c_str(),filename_new.c_str()) == 0)
{
printf("改名成功完成/n/n");
}
else
perror("rename");
}
}
closedir(dirp);
system("PAUSE");
return EXIT_SUCCESS;
}