本文主要利用IO库中的文件查找函数_firstfind、_findnext 、_findclose,以及结构体struct _finddata_t完成数据的批量重名和查找,适当的修改应该也可以完成文件的初步分类。
1、首先介绍一下背景:
用过AD采集卡采集数据的朋友可能有过创建批量数据的经历,为了记录当时采样的时间或者为了数据文件命名的不重复,往往我们希望采用系统的时间来命名。在我的AD卡上位软件中,命名格式为年+月+日+时+分+秒+编号(加编号的目的是因为AD卡的数据采集速度较快,1s钟可能产生多个数据,该编号为记录当前秒数采集数据序号)。但是楼主比较粗心,对于上位采集软件没有仔细的检查。导致系统在命名的时候忽略掉了月日时分秒前面可能存在的零例如2013-2-18 5:15,楼主存的文件名为2013218515,这样存会导致使用数据的人员产生对文件的采集时间产生疑惑。因为上面的文件名又可以理解为2013年2月1日8时5分15秒,由于数据量大概有15GB左右,所以采用软件修改。
2、其次介绍一下处理的思路
1、扫描指定目录,获取当前目录的所有文件和子目录.
需要注意的是FAT文件系统在每一个子目录中都创建了一个..和.目录。处理的时候要避免由于这个原因导致递归算法进入死循环。此处扫描主要用到了以下结构。
long hFile;
if ((hFile = _findfirst(p_find_path, &find_file)) == -1L)
{
printf("No files and folder in current directory");
exit(1);
}
else
{
判断文件还是目录;
}
while (!_findnext(hFile,&find_file))
{
}
_findclose(hFile);
2、获取当前目录中需要修改的文件的路径(包括存储的文件夹路径+文件名)。
3、获取当前文件的最后修改时间(
由于AD采集卡创建的文件在第一次写入之后没有进行第二次修改,完全可以认为文件的第一次写入时间为数据的采集时间),修改为特定格式的文件显示名。此处的文件显示采样除了年之外,所有时间都为两位显示例如之前的2013-2-18 5:15被写为201302180515,这样写之后完全不会产生意外。
4、修改文件名
3、贴上源代码,接着写
#include #include #include #include #include #include using namespace std;
#define replace_name 1
#define scan 1
char* creat_newname(struct _finddata_t creat_time, char* file_path);
char* get_oldname(struct _finddata_t creat_time);
char* change_filename(char* path,int num);
char* judge_filename(char* path_name,int num);
//void find_rename(char* p_find_path);
void search_findfile(char* path);
char* file_type = ".dat";
char* file_path = "D:\\数据309-320--\\数据309-320\\";
int correct_statue;
const int MathPath = 200;
void main(int argc,char *argv[])
{
cout << file_path
m_localTime->tm_mon += 1;
j = sprintf(m_char_time1+j,"%s",file_path);
j += sprintf(m_char_time1+j,"%04d",m_localTime->tm_year);
j += sprintf(m_char_time1+j,"%02d",m_localTime->tm_mon);
j += sprintf(m_char_time1+j,"%02d",m_localTime->tm_mday);
j += sprintf(m_char_time1+j,"%02d",m_localTime->tm_hour);
j += sprintf(m_char_time1+j,"%02d",m_localTime->tm_min);
j += sprintf(m_char_time1+j,"%02d",m_localTime->tm_sec);
j += sprintf(m_char_time1+j,"%s","(1)");
j += sprintf(m_char_time1+j,"%s",file_type);
char* cc;
cc = (char*)malloc(sizeof(char)*(j+1));
memset(cc,0,j+1);
for (int i=0;i> 4))
{
/*if (find_file.name[0] == '.')
{
memset(old_path,0,MathPath);
strcat(old_path,find_file.name);
cout << old_path << "当前路径为系统路径" << endl;
}
else
{
//是目录且不是.和..目录
//执行目录操作
memset(old_path,0,MathPath);
strcat(old_path,path);
strcat(old_path,find_file.name);
strcat(old_path,"\\"); //修改为当前目录
cout << old_path << endl;
search_findfile(old_path);
}
*/
if (find_file.name[0] != '.')
{
//是目录且不是.和..目录
//执行目录操作
memset(old_path,0,MathPath);
strcat(old_path,path);
strcat(old_path,find_file.name);
strcat(old_path,"\\"); //修改为当前目录
cout << old_path << endl;
search_findfile(old_path);
}
}
else
{
//是文件,执行文件操作
memset(old_path,0,MathPath);
strcat(old_path,path);
strcat(old_path,find_file.name);
cout << "old path :" << old_path << endl;
newpath_name = creat_newname(find_file,path);
newpath_name = judge_filename(newpath_name,2);
cout << "new path "<< newpath_name << endl;
int correct_statue = rename(old_path,newpath_name); //rename(oldpath,newpath)
if(!correct_statue)
{
cout << "correct the filename from " << old_path << " to" << path << "successs" << endl;
cout << endl << endl;
}
}
}
while (!_findnext(hFile,&find_file))
{
if (1 == (find_file.attrib >> 4))
{
/*if(find_file.name[0] == '.')
{
memset(old_path,0,MathPath);
strcat(old_path,find_file.name);
cout << old_path << "当前路径为系统路径" << endl;
}
else
{
//是目录且不是.和..目录
//执行目录操作
memset(old_path,0,MathPath);
strcat(old_path,path);
strcat(old_path,find_file.name);
strcat(old_path,"\\"); //建立新的目录
cout << old_path << endl;
search_findfile(old_path);
}
*/
if (find_file.name[0] != '.')
{
//是目录且不是.和..目录
//执行目录操作
memset(old_path,0,MathPath);
strcat(old_path,path);
strcat(old_path,find_file.name);
strcat(old_path,"\\"); //建立新的目录
cout << old_path << endl;
search_findfile(old_path);
}
}
else
{
//是文件,执行文件操作
memset(old_path,0,MathPath);
strcat(old_path,path);
strcat(old_path,find_file.name);
cout << "old path :" << old_path << endl;
newpath_name = creat_newname(find_file,path);
newpath_name = judge_filename(newpath_name,2);
cout << "new path "<< newpath_name << endl;
correct_statue = rename(old_path,newpath_name); //rename(oldpath,newpath)
if(!correct_statue)
{
cout << "correct the filename from " << old_path << " to" << newpath_name << "successs" << endl;
cout << endl << endl;
}
}
}
_findclose(hFile);
}