很久很久以前的一个小程序------批量文件的重命名
这个小程序是以前为了做测试时用的,它的功能很简单,就是将一堆命名无序的文件,按照你的需求进行顺序排列:如1.jpg,2.jpg,3.jpg...等。
原理很简单:
1.先扫描一个文件夹的所有文件,将文件名放于一个数组中。
int getPicFileNames(char* searchDir)
{
_finddata_t tempFileInfo;
long searchHandle;
char suffix[4];
//assert.
if(searchDir==NULL)
{
fprintf(stderr,"in parseDir,pass in filePath is null/n");
return -1;
}
fprintf(stdout,"%s /n",searchDir);
//change dir
_chdir(searchDir);
//get search handle
searchHandle=_findfirst("*",&tempFileInfo);
if(searchHandle==-1)//if no file in this directory
{
printf("no such directory.");
return -1;
}
else
{
gTotalFileNum=0;
//fprintf(stdout,"read in bmp files./n");
do
{
//IF CURRENT FILE IS A DIRECTORY
if((tempFileInfo.attrib&_A_SUBDIR))
{
}
else //IF IT IS A REAL FILE,PARSE IT
{
if(strlen(tempFileInfo.name)<=3)
continue;
else
{
suffix[0]=tolower(tempFileInfo.name[strlen(tempFileInfo.name)-3]);
suffix[1]=tolower(tempFileInfo.name[strlen(tempFileInfo.name)-2]);
suffix[2]=tolower(tempFileInfo.name[strlen(tempFileInfo.name)-1]);
suffix[3]='/0';
if(!(strcmp(suffix,"gif")==0||strcmp(suffix,"jpg")==0||strcmp(suffix,"png")==0||strcmp(suffix,"mp3")==0||strcmp(suffix,"wma")==0))
//filter the file not belong to bmp
continue;
}
strcpy(picFileSuffix[gTotalFileNum],suffix);
strcpy(gBMPFiles [gTotalFileNum],tempFileInfo.name);
gTotalFileNum++;
}
}
while(_findnext(searchHandle, &tempFileInfo)!=-1);
//search the next item, file or directory
}
_findclose(searchHandle);//VERY IMPORTANT.
return 0;
}
2.将刚才扫描到的文件,按先后顺序,把一个原文件的所有字节输出到另一个文件中(COPY),并重命名文件名。
int renameFileNames()
{
int i;
long j,len;
_chdir("..//");
//------out file list-----
if((outfileNameList=fopen("outfileNameList.txt","wb"))==NULL)
{
printf("open outfileNameList error./n");
return -1;
}
for(i=0;i<gTotalFileNum;i++)
{
printf("now process:%s /n",gBMPFiles[i]);
//-----INPUT SESSION-------------------
_chdir(rawPicDocuAdd);
strcpy(inputFileName,gBMPFiles[i]);
//open original file
if((inputFile =fopen(inputFileName ,"rb"))==NULL)
{
printf("open in file error./n");
continue;
}
j=0;
while(fscanf(inputFile,"%c",&cachePool[j])!=EOF)
{
j++;
}
len=j;
fclose(inputFile);
//--------------------------------------
//------OUPUT SESSION-------------------
_chdir("..//");
_chdir(finePicDocuAdd);
strcpy(outputFileName,outFileNameSuffix);
strcat(outputFileName,intToStr(i));
strcat(outputFileName,".");
strcat(outputFileName,picFileSuffix[i]);
fprintf(outfileNameList,"%s/r/n",outputFileName);
//open original file
if((outputFile =fopen(outputFileName,"wb"))==NULL)
{
printf("open out file error./n");
continue;
}
for(j=0;j<len;j++)
{
fprintf(outputFile,"%c",cachePool[j]);
}
fclose(outputFile);
_chdir("..//");
//--------------------------------------
}
fclose(outfileNameList);
return 0;
}
几点说明
1)程序是用VC6.0编译的。
2)未经命名的文件应放于变量rawPicDocuAdd所对应的文件夹中,而按序命名的文件则放于finePicDocuAdd中,
3)被扫描的文件的格式是可以自定义的,参程序的118行。
4)cachePool是用来临时存放未经整理文件的二进制的数据的(感觉这个有些多余
)。
5)outFileNameSuffix指的是输出文件名的前缀,你可以修改它,如0809等。
6)内存有些小泄漏,你发现了吗?
嘿嘿,源码下载时间到了:
http://www.topven.com/labs/Cpp/080908/pic_name_chg_app.rar
emilmatthew@126.com
08/09/08
08/09/08
这个小程序是以前为了做测试时用的,它的功能很简单,就是将一堆命名无序的文件,按照你的需求进行顺序排列:如1.jpg,2.jpg,3.jpg...等。

原理很简单:
1.先扫描一个文件夹的所有文件,将文件名放于一个数组中。
int getPicFileNames(char* searchDir)
{
_finddata_t tempFileInfo;
long searchHandle;
char suffix[4];
//assert.
if(searchDir==NULL)
{
fprintf(stderr,"in parseDir,pass in filePath is null/n");
return -1;
}
fprintf(stdout,"%s /n",searchDir);
//change dir
_chdir(searchDir);
//get search handle
searchHandle=_findfirst("*",&tempFileInfo);
if(searchHandle==-1)//if no file in this directory
{
printf("no such directory.");
return -1;
}
else
{
gTotalFileNum=0;
//fprintf(stdout,"read in bmp files./n");
do
{
//IF CURRENT FILE IS A DIRECTORY
if((tempFileInfo.attrib&_A_SUBDIR))
{
}
else //IF IT IS A REAL FILE,PARSE IT
{
if(strlen(tempFileInfo.name)<=3)
continue;
else
{
suffix[0]=tolower(tempFileInfo.name[strlen(tempFileInfo.name)-3]);
suffix[1]=tolower(tempFileInfo.name[strlen(tempFileInfo.name)-2]);
suffix[2]=tolower(tempFileInfo.name[strlen(tempFileInfo.name)-1]);
suffix[3]='/0';
if(!(strcmp(suffix,"gif")==0||strcmp(suffix,"jpg")==0||strcmp(suffix,"png")==0||strcmp(suffix,"mp3")==0||strcmp(suffix,"wma")==0))
//filter the file not belong to bmp
continue;
}
strcpy(picFileSuffix[gTotalFileNum],suffix);
strcpy(gBMPFiles [gTotalFileNum],tempFileInfo.name);
gTotalFileNum++;
}
}
while(_findnext(searchHandle, &tempFileInfo)!=-1);
//search the next item, file or directory
}
_findclose(searchHandle);//VERY IMPORTANT.
return 0;
}
2.将刚才扫描到的文件,按先后顺序,把一个原文件的所有字节输出到另一个文件中(COPY),并重命名文件名。
int renameFileNames()
{
int i;
long j,len;
_chdir("..//");
//------out file list-----
if((outfileNameList=fopen("outfileNameList.txt","wb"))==NULL)
{
printf("open outfileNameList error./n");
return -1;
}
for(i=0;i<gTotalFileNum;i++)
{
printf("now process:%s /n",gBMPFiles[i]);
//-----INPUT SESSION-------------------
_chdir(rawPicDocuAdd);
strcpy(inputFileName,gBMPFiles[i]);
//open original file
if((inputFile =fopen(inputFileName ,"rb"))==NULL)
{
printf("open in file error./n");
continue;
}
j=0;
while(fscanf(inputFile,"%c",&cachePool[j])!=EOF)
{
j++;
}
len=j;
fclose(inputFile);
//--------------------------------------
//------OUPUT SESSION-------------------
_chdir("..//");
_chdir(finePicDocuAdd);
strcpy(outputFileName,outFileNameSuffix);
strcat(outputFileName,intToStr(i));
strcat(outputFileName,".");
strcat(outputFileName,picFileSuffix[i]);
fprintf(outfileNameList,"%s/r/n",outputFileName);
//open original file
if((outputFile =fopen(outputFileName,"wb"))==NULL)
{
printf("open out file error./n");
continue;
}
for(j=0;j<len;j++)
{
fprintf(outputFile,"%c",cachePool[j]);
}
fclose(outputFile);
_chdir("..//");
//--------------------------------------
}
fclose(outfileNameList);
return 0;
}
几点说明
1)程序是用VC6.0编译的。
2)未经命名的文件应放于变量rawPicDocuAdd所对应的文件夹中,而按序命名的文件则放于finePicDocuAdd中,
3)被扫描的文件的格式是可以自定义的,参程序的118行。
4)cachePool是用来临时存放未经整理文件的二进制的数据的(感觉这个有些多余

5)outFileNameSuffix指的是输出文件名的前缀,你可以修改它,如0809等。
6)内存有些小泄漏,你发现了吗?

嘿嘿,源码下载时间到了:

http://www.topven.com/labs/Cpp/080908/pic_name_chg_app.rar