[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;
}