C++批量修改文件后缀名的Qt程序
方法一:
#include "stdafx.h"
#include <stdio.h>
#include <fstream>
#include <Windows.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <io.h>
#include <vector>
#include <direct.h>
#include <stdlib.h>
#include <memory>
using namespace std;
void getFilesAll( string path, vector<string>& files)
{
//获取文件夹下面的所有文件名字
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1)
{
do {
if ((fileinfo.attrib & _A_SUBDIR))
{
if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0)
{
getFilesAll( p.assign(path).append("\\").append(fileinfo.name) , files );
}
}
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name) );
}
}while(_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
void postfix()
{
char *filePath="G:\\image\\";
char strname[60];
vector<string> filesa;
getFilesAll(filePath ,filesa );
for(unsigned int i=0 ; i<filesa.size(); i++)
{
char newname[80]; //store the folder's name
char oldname[80];
strcpy(oldname, filesa[i].c_str());
cout<<filesa[i].c_str()<<endl;
//strcpy(newname, filesa[i].c_str());
sprintf(newname,"G:\\result\\%d.jpg",i);
//sprintf(newname,"%d.jpg",i);
rename(oldname,newname);
if (rename(oldname,newname))
{
cout<<"第"<<i<<"个文件名字从"<<filesa[i].c_str()<<"修改为"<<newname<<"成功"<<endl;
}
cout<<strname<<endl;
}
}
void main()
{
postfix();
system("pause");
}
方法二
#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;
}
方法三
c++修改指定文件夹下所有文件扩展名
#include <io.h>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
//读取文件夹设定
const string path = "D:\\pp\\";
int main(){
_finddata_t file;
long lf;
if((lf = _findfirst((path + "*.*").c_str(), &file))==-1)
cout<<"Not Found!"<<endl;
else{
// cout<<"file name list:"<<endl;
while(_findnext( lf, &file)==0){
{
string str=file.name;
//修改扩展名为.jpeg
str += ".jpeg";
//重命名文件
if (rename((path + string(file.name)).c_str(), (path + str).c_str()) == 0)
{
//cout << "1" << endl;
}
}
}
}
_findclose(lf);
return 0;
}
方法四
Qt应用程序
核心代码
#ifndef FILEOPERATION_H
#define FILEOPERATION_H
#include <stdio.h>
#include <string>
#include <vector>
#include <windows.h>
class FileOperation
{
public:
FileOperation();
FileOperation(std::string dir, std::string suffix);
~FileOperation();
std::string err_msg() const { return err_msg_;}
int error_code() const { return error_code_;}
void set_error_code(int error_code) { error_code_ = error_code;}
void set_err_msg(std::string err_msg) { err_msg_ = err_msg;}
int Count() const { return file_list_->size();}
bool RenameFile();
protected:
void FindFiles();
private:
std::string dirname_;
std::string suffix_;
std::string err_msg_;
std::vector<std::string> *file_list_;
WIN32_FIND_DATAA current_file_;
HANDLE handle_;
int error_code_;
bool FindNext();
};
#endif // FILEOPERATION_H
#include "FileOperation.h"
#include <algorithm>
#include <iterator>
#include <windows.h>
FileOperation::FileOperation() : dirname_("D:/web/new/"), suffix_("html"),
file_list_(new std::vector<std::string>()), error_code_(0)
{
}
FileOperation::FileOperation(std::string dir, std::string suffix) :
dirname_(dir), suffix_(suffix), file_list_(new std::vector<std::string>()), error_code_(0)
{
dirname_+= "/";
}
FileOperation::~FileOperation()
{
delete file_list_;
}
bool FileOperation::RenameFile()
{
this->FindFiles();
for(std::string &file_data : *file_list_) {
//从尾部开始找‘.’,中间的.不修改
// auto name_iter = std::find(file_data.cbegin(), file_data.cend(), '.');
auto name_iter = std::find(file_data.crbegin(), file_data.crend(), '.');
std::string name = std::string(file_data.cbegin(), name_iter.base());
name += suffix_;
name = dirname_ + name;
if ( rename((dirname_ + file_data).c_str(), name.c_str()) != 0) {
error_code_ = -1;
err_msg_ += "文件"+ file_data +"更改失败";
}
}
return true;
}
void FileOperation::FindFiles()
{
LPCSTR dirname = (dirname_ + "*").c_str();
handle_ = ::FindFirstFileA(dirname, ¤t_file_);
if (handle_ != INVALID_HANDLE_VALUE) {
if (FindNext())
while (FindNext()) {
file_list_->push_back(current_file_.cFileName);
}
}
::FindClose(handle_);
}
bool FileOperation::FindNext()
{
return ::FindNextFileA(handle_, ¤t_file_);
}
完整源码下载地址:传送门