pdf是扫描文件,文件名顺序就是扫描先后顺序,新文件名存放在csv文件中(不能是utf8的那个csv文件,我还写了个utf8的格式转换函数,但是还是在新的地方又出现了乱码)。
把exe放到要处理的文件夹中,双击就可以执行替换。
一些碎碎念,我忘了二维char数组怎么用了,结果用结构体代替了发现更顺手了。utf8的转换函数没有删,但是也没有调用。
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include "read.h"
using namespace std;
struct PDF {
char name[100];
};
PDF pdf[500], csv[500];
bool cmp(PDF a, PDF b) {
int ans = strcmp(a.name, b.name);
if (ans < 0)
return true;
else
return false;
}
string UTF8ToGB(const char* str) //处理中文乱码异常
{
string result;
WCHAR* strSrc;
LPSTR szRes;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i + 1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
//获得临时变量的大小
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new CHAR[i + 1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
result = szRes;
delete[]strSrc;
delete[]szRes;
return result;
}
int readpdf() {
int sum;
char buffer[255];
_getcwd(buffer, 255); //读取当前位置目录
strcat(buffer, "\\*.pdf");
const char* to_search = buffer;
// cout << buffer << endl;
intptr_t handle; //用于查找的句柄
struct _finddata_t fileinfo; //文件信息的结构体
handle = _findfirst(to_search, &fileinfo); //第一次查找
if (-1 == handle)return -1;
// printf("%s\n", fileinfo.name); //打印出找到的文件的文件名
sum = 0;
strcpy(pdf[sum++].name, fileinfo.name);
// printf("%s\n", pdf[sum - 1].name);
while (!_findnext(handle, &fileinfo)) //循环查找其他符合的文件,知道找不到其他的为止
{
// printf("%s\n", fileinfo.name);
strcpy(pdf[sum++].name, fileinfo.name);
// printf("%s\n", pdf[sum - 1].name);
}
_findclose(handle); //别忘了关闭句柄
// printf("sum=%d\n", sum);
sort(pdf, pdf + sum, cmp);
return sum;
}
int readcsv() {
int num = 0;
string line, newline;
char buffer[255], filename[255];
_getcwd(buffer, 255); //读取当前位置目录
strcat(buffer, "\\*.csv");
const char* to_search = buffer;
intptr_t handle; //用于查找的句柄
struct _finddata_t fileinfo;
handle = _findfirst(to_search, &fileinfo); //第一次查找
if (-1 == handle) {
printf("不存在csv文件\n");
return -1;
}
// printf("%s\n", fileinfo.name); //打印出找到的文件的文件名
strcpy(filename, fileinfo.name);
ifstream fin(filename); //打开文件流操作
while (getline(fin, line)) //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
{
// newline = UTF8ToGB(line.c_str()).c_str();
// cout << "原始字符串:" << newline << endl; //整行输出
strcpy(csv[num].name, line.c_str());
strcat(csv[num].name, ".pdf");
num++;
// printf("%s\n", csv[num - 1].name);
}
// printf("num = %d\n", num);
fin.close();
return num;
}
void replacename(int num) {
char oldName[100], newName[100];
string s1, s2;
// s1 = "SKM_454e22112113390.pdf";
// s2 = "ZLH2-3221-EQ-STCC-AGD-0188 E-0381设备管口与相连管线坐标不符";
// rename(s1.c_str(), s2.c_str());
// cout << s1 << " " << s2 << endl;
for (int i = 0; i < num; i++) {
strcpy(oldName, pdf[i].name);
strcpy(newName, csv[i].name);
rename(oldName, newName);
printf("%s 替换为 %s\n", oldName, newName);
}
}
int main()
{
int sum, num;
sum = readpdf(); //读取当前目录下的pdf文件,存储在pdf中,并返回文件个数
printf("pdf读取完毕,共:%d个\n", sum);
num = readcsv(); //读取csv文件中的新文件名,存储在csv中,并返回行数
printf("csv读取完毕,共:%d行\n", num);
if (num != sum) {
printf("csv行数和pfd数量不匹配\n");
printf("csv = %d pdf = %d \n", num, sum);
system("pause");
return -1;
}
// printf("%c %d\n", csv[0].name[0], csv[0].name[0]);
replacename(num);
system("pause");
return 0;
}
又改了改做了个修改图片文件名的
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include "read.h"
using namespace std;
struct Picture {
char name[100];
char lastname[10];
};
Picture picture[500], csv[500];
bool cmp(Picture a, Picture b) {
int ans = strcmp(a.name, b.name);
if (ans < 0)
return true;
else
return false;
}
string UTF8ToGB(const char* str) //处理中文乱码异常
{
string result;
WCHAR* strSrc;
LPSTR szRes;
//获得临时变量的大小
int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
strSrc = new WCHAR[i + 1];
MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);
//获得临时变量的大小
i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new CHAR[i + 1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
result = szRes;
delete[]strSrc;
delete[]szRes;
return result;
}
bool namecheck(char* name) {
// printf("%d %s\n", strlen(name), name);
char str3[10], str4[10];
int len = strlen(name);
str3[0] = name[len - 3];
str3[1] = name[len - 2];
str3[2] = name[len - 1];
str3[3] = '\0';
str4[0] = name[len - 4];
str4[1] = name[len - 3];
str4[2] = name[len - 2];
str4[2] = name[len - 1];
str4[3] = '\0';
if ((strcmp(str3, "jpg") == 0) || (strcmp(str3, "JPG") == 0) ||
(strcmp(str3, "png") == 0) || (strcmp(str3, "PNG") == 0) ||
(strcmp(str3, "bmp") == 0) || (strcmp(str3, "BMP") == 0)) {
return true;
}
else if ((strcmp(str4, "jpeg") == 0) || (strcmp(str4, "JPEG") == 0)) {
return true;
}
return false;
}
int readpicture() {
int sum, len;
char buffer[255];
char bufferjpg[255], bufferJPG, bufferpng[255], bufferPNG[255];
char bufferjpeg[255], bufferJPEG[255], bufferbmp[255], bufferBMP[255];
_getcwd(buffer, 255); //读取当前位置目录
strcat(buffer, "\\*");
const char* to_search = buffer;
// cout << buffer << endl;
intptr_t handle; //用于查找的句柄
struct _finddata_t fileinfo; //文件信息的结构体
handle = _findfirst(to_search, &fileinfo); //第一次查找
if (-1 == handle)return -1;
// printf("%s\n", fileinfo.name); //打印出找到的文件的文件名
sum = 0;
if (namecheck(fileinfo.name)) {
len = strlen(fileinfo.name);
if (fileinfo.name[len - 4] == '.') {
picture[sum].lastname[0] = fileinfo.name[len - 3];
picture[sum].lastname[1] = fileinfo.name[len - 2];
picture[sum].lastname[2] = fileinfo.name[len - 1];
picture[sum].lastname[3] = '\0';
}
else if (fileinfo.name[len - 5] == '.') {
picture[sum].lastname[0] = fileinfo.name[len - 4];
picture[sum].lastname[1] = fileinfo.name[len - 3];
picture[sum].lastname[2] = fileinfo.name[len - 2];
picture[sum].lastname[3] = fileinfo.name[len - 1];
picture[sum].lastname[4] = '\0';
}
strcpy(picture[sum++].name, fileinfo.name);
len = strlen(fileinfo.name);
}
// printf("%s\n", pdf[sum - 1].name);
while (!_findnext(handle, &fileinfo)) //循环查找其他符合的文件,知道找不到其他的为止
{
// printf("%s\n", fileinfo.name);
if (namecheck(fileinfo.name)) {
len = strlen(fileinfo.name);
if (fileinfo.name[len - 4] == '.') {
picture[sum].lastname[0] = fileinfo.name[len - 3];
picture[sum].lastname[1] = fileinfo.name[len - 2];
picture[sum].lastname[2] = fileinfo.name[len - 1];
picture[sum].lastname[3] = '\0';
}
else if (fileinfo.name[len - 5] == '.') {
picture[sum].lastname[0] = fileinfo.name[len - 4];
picture[sum].lastname[1] = fileinfo.name[len - 3];
picture[sum].lastname[2] = fileinfo.name[len - 2];
picture[sum].lastname[3] = fileinfo.name[len - 1];
picture[sum].lastname[4] = '\0';
}
strcpy(picture[sum++].name, fileinfo.name);
// printf("%s\n", pdf[sum - 1].name);
}
}
_findclose(handle); //别忘了关闭句柄
// printf("sum=%d\n", sum);
sort(picture, picture + sum, cmp);
return sum;
}
int readcsv() {
int num = 0;
string line, newline;
char buffer[255], filename[255];
_getcwd(buffer, 255); //读取当前位置目录
strcat(buffer, "\\*.csv");
const char* to_search = buffer;
intptr_t handle; //用于查找的句柄
struct _finddata_t fileinfo;
handle = _findfirst(to_search, &fileinfo); //第一次查找
if (-1 == handle) {
printf("不存在csv文件\n");
return -1;
}
// printf("%s\n", fileinfo.name); //打印出找到的文件的文件名
strcpy(filename, fileinfo.name);
ifstream fin(filename); //打开文件流操作
while (getline(fin, line)) //整行读取,换行符“\n”区分,遇到文件尾标志eof终止读取
{
strcpy(csv[num].name, line.c_str());
// strcat(csv[num].name, ".pdf");
num++;
// printf("%s\n", csv[num - 1].name);
}
// printf("num = %d\n", num);
fin.close();
return num;
}
void replacename(int num) {
char oldName[100], newName[100];
for (int i = 0; i < num; i++) {
strcat(csv[i].name, ".");
strcat(csv[i].name, picture[i].lastname);
strcpy(oldName, picture[i].name);
strcpy(newName, csv[i].name);
rename(oldName, newName);
printf("%s 替换为 %s\n", oldName, newName);
}
}
int main()
{
int sum, num;
sum = readpicture(); //读取当前目录下的图片文件,存储在picture中,并返回文件个数
printf("图片读取完毕,共:%d个\n", sum);
num = readcsv(); //读取csv文件中的新文件名,存储在csv中,并返回行数
printf("csv读取完毕,共:%d行\n", num);
if (num != sum) {
printf("csv行数和图片数量不匹配\n");
printf("csv = %d picture = %d \n", num, sum);
system("pause");
return -1;
}
// printf("%c %d\n", csv[0].name[0], csv[0].name[0]);
replacename(num);
system("pause");
return 0;
}
工程文件放这里了
https://download.csdn.net/download/mandiheyanyu/87136010
部分参考链接
C++学习笔记(一):中文字符的处理——批量读取和修改文件夹下文件名,以及WCHAR_T/WSTRING与CHAR/STRING不得不说的故事
https://www.freesion.com/article/4331115182/
C++修改文件名
https://zhuanlan.zhihu.com/p/296757589