C++批量修改文件后缀名(提供多种方法)
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 namechar 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; //修改扩展名为.jpegstr += ".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_);
}
完整源码下载地址:传送门
C++批量修改文件后缀名(提供多种方法)相关推荐
- 计算机如何更改后缀文件名,如何批量修改文件后缀名(任何文件的扩展名)?
无需软件批量修改文件后缀名?怎么通过命令行批量修改文件后缀名?有时候由于文件后缀名格式不同,有的时候我们需要对文件扩展名进行修改,或者文件扩展名丢失,需要添加.如果数量少的文件那还简单直接修改就好了. ...
- 如何批量修改文件后缀名?(批量修改文件的扩展名)
无需软件批量修改文件后缀名?怎么通过命令行批量修改文件后缀名?有时候由于文件后缀名格式不同,有的时候我们需要对文件扩展名进行修改,或者文件扩展名丢失,需要添加.如果数量少的文件那还简单直接修改就好了. ...
- linux sed给空文件首行插入_linux下批量修改文件后缀名以及合并多行
写在前面 感谢乔兄送测的公司,让我终于所有营业 part1讲下怎么解决小问题 part2分享谈判过程 part3mark张兄的奶茶 PART1 linux下批量修改文件后缀名以及合并多行 近期师兄在做 ...
- java例程练习(批量修改文件后缀名)
/*** 文件后缀名修改器* 说明:UI部分* @author Yours风之恋* * 创建时间:2012-8-8**/ public class UserInterface {public stat ...
- 玩点不一样的,如何使用MATLAB实现批量修改文件后缀名,文件名,批量复制文件
1. 自动批量修改文件后缀名,有时非常有用. 对于文件名没有要求,不用是序列文件名.比如需要将abaqus的inp文件修改成txt文件,便于使用 textscan和 textread等函数操作. c ...
- php 批量改文件名后缀名_PHP 批量修改文件后缀名
跳至/** * 批量修改文件后缀名 * @param $path 文件夹路径 * @param $sext 原文件后缀名 ($sext=all说明整个目录的所有文件) * @param $dext 目 ...
- windows10批量修改文件后缀名
windows10批量修改文件后缀名 步骤 1. 在windows开始栏中打开命令提示符, 也可以用快捷键:win+R ,输入cmd,回车 2. cd到你需要修改后缀名的文件夹目录下,输入命令:ren ...
- 怎么批量修改html文件后缀,如何批量修改文件后缀名 超详细介绍
如何批量修改文件后缀名 超详细介绍.比如我们保持图片的时候,不是我们要的jpg格式的,需要一个一个去修改她的后缀名.现在小编就教大家快速的,批量的,安全的修好多数的文件后缀名.希望能帮大家平时的工作. ...
- 批量修改文件后缀名(脚本)
一键批量修改文件后缀名(脚本) 输入法是英语模式输入!!! 输入法是英语模式输入!!! 输入法是英语模式输入!!! 1.在你需要更改的 ...
最新文章
- MySQL重置root用户密码的方法
- JSON:如果你愿意一层一层剥开我的心,你会发现...这里水很深——深入理解JSON...
- 开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试
- oracle启动服务和监听命令
- 线性代数---线性方程组
- 端上智能——深度学习模型压缩与加速
- 添加中文数据出现问号_怪物猎人世界绚辉龙和冥赤龙的出现时间规律一览
- 手机User-Agent大全(Android爬虫)
- 华为Mate 30 Pro最新渲染图曝光:六摄造型 越看越顺眼了?
- 数字资产价值巨大,GMQGroup深入布局挖掘数字财富
- 趣达学院学习有奖活动!
- Oracle:grouping和rollup
- 在linux内核中修改TCP MSS值
- 简繁体unicode、gbk对照表
- android 京东平板布局,京东商城上线Android Pad客户端 完美布局移动终端
- 奶爸日记19 - 湖边芦苇
- OracleDataAdapter.Fill()处于无限等待中 【已解决】
- 如何写出一份优秀的简历和求职信?
- String字符串截取方式
- 【GlobalMapper精品教程】050:点线面缓冲区分析案例