对一批HCT文件文件名进行批量修改 要求是将文件名修改为3DB0001、3DB0002、。。。。3DB0100
使用的是_finddata_t函数
#include "afx.h"
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
vector
long handle;
struct _finddata_t FileInfo;
string path("C:\Users\m1575\Desktop\Bag\3DB0015\Data\"); //文件所在路径
string file_path; //用户手动输入路径
cout << "请输入正确的文件路径:"<< endl;
cin>> file_path;
if (path > file_path)
{
printf("输入的文件路径太小了n");
}
else if(path < file_path)
{
printf("输入的文件路径太大了n");
}
else
{
printf("输入的文件路径正确n");
} //判断用户输入的路径和给定的路径是否一致
string fullpath = path+"*.HCT"; // 加上后缀名 HCT
handle = _findfirst(fullpath.c_str(), &FileInfo);
if (handle==-1)
{
cout << "没有找到HCT文件" << endl;
printf("%sn", FileInfo.name);
return 0;
} //找第一个HCT文件
cout << handle << endl; //handle值 ,这一行可有可无
int index = 1;
int width = 3;
char t_str1[10];
_itoa_s(index, t_str1,10); //将整数转化为string
string str(t_str1);
string strnew(width-str.length(),'0');
str = strnew + str; //str的值为“001”
string s("3DB0015_0");
string oldname = file_path + FileInfo.name;
string newname = file_path + s+str+".HCT";
//这一部分是为了让文件名从0001、0002递增到0100
rename(oldname.c_str(), newname.c_str()); //调用重命名函数rename
while (_findnext(handle, &FileInfo) == 0)
{
index++;
_itoa_s(index, t_str1,10);
string str(t_str1);
string strnew(width - str.length(), '0');
str = strnew + str;
string oldname = file_path + FileInfo.name;
string newname = file_path + s + str + ".HCT";
rename(oldname.c_str(), newname.c_str());
if (rename(oldname.c_str(), newname.c_str()) < 0)
{
printf("重命名失败n");
}
else
{
printf("重命名成功n");
}
}_findclose(handle);
system("pause");
return 0;
}



