c 语言 批量修改文件名,C++批量修改文件名, 大佬勿喷

[C++] 纯文本查看 复制代码/**

* 枚举文件夹下所有文件并将之重命名

* @param char* rootDir 文件目录

* @param char* searchName 通配符

* @param char* extName 文件后缀

* @param int cntFileNo 当前编号

*/

int setFileNameBySearch(char* rootDir, char* searchName, char* extName, int cntFileNo)

{

// 文件句柄

long file_handle;

// 文件信息结构体

struct _finddata_t fileinfo;

file_handle = _findfirst(searchName, &fileinfo);

if(file_handle == -1) return cntFileNo;

char file_tmp_name[255] = "";

char file_cnt_name[255] = "";

strcpy(file_tmp_name, rootDir);

strcpy(file_cnt_name, rootDir);

strcat(file_cnt_name, fileinfo.name);

int tmp = cntFileNo, j;

char tmp_pic[255] = "";

if(tmp >= 10)

{

int tmp_len = 0;

while(tmp)

{

tmp_pic[tmp_len ++] = (tmp % 10 + '0');

tmp /= 10;

}

for(j = 0; j <= (tmp_len - 1) / 2; j ++)

{

char tmp_ch = tmp_pic[j];

tmp_pic[j] = tmp_pic[tmp_len - 1 - j];

tmp_pic[tmp_len - 1 - j] = tmp_ch;

}

}

else

tmp_pic[0] = tmp + '0';

// 拼接文件名

strcat(tmp_pic, extName);

strcat(file_tmp_name, tmp_pic);

// 复制文件

rename(file_cnt_name, file_tmp_name);

cntFileNo += 1;

while(!_findnext(file_handle, &fileinfo))

{

tmp =cntFileNo;

memset(tmp_pic, 0, sizeof(tmp_pic));

memset(file_tmp_name, 0, sizeof(file_tmp_name));

memset(file_cnt_name, 0, sizeof(file_cnt_name));

strcpy(file_tmp_name, rootDir);

strcpy(file_cnt_name, rootDir);

strcat(file_cnt_name, fileinfo.name);

if(tmp >= 10)

{

int tmp_len = 0;

while(tmp)

{

tmp_pic[tmp_len ++] = (tmp % 10 + '0');

tmp /= 10;

}

for(j = 0; j <= (tmp_len - 1) / 2; j ++)

{

char tmp_ch = tmp_pic[j];

tmp_pic[j] = tmp_pic[tmp_len - 1 - j];

tmp_pic[tmp_len - 1 - j] = tmp_ch;

}

}

else

tmp_pic[0] = tmp + '0';

// 拼接文件名

strcat(tmp_pic, extName);

strcat(file_tmp_name, tmp_pic);

// 复制文件

rename(file_cnt_name, file_tmp_name);

cntFileNo += 1;

}

_findclose(file_handle);

return cntFileNo;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值