很久很久以前的一个小程序------批量文件的重命名

介绍了一个简单的批量文件重命名工具,该工具能够扫描指定文件夹内的文件,并按顺序重新命名,支持多种常见文件格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很久很久以前的一个小程序------批量文件的重命名

emilmatthew@126.com
08/09/08

这个小程序是以前为了做测试时用的,它的功能很简单,就是将一堆命名无序的文件,按照你的需求进行顺序排列:如1.jpg,2.jpg,3.jpg...等。 嘻悠猴QQ表情大全

原理很简单:
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是用来临时存放未经整理文件的二进制的数据的(感觉这个有些多余 嘻悠猴QQ表情大全 )。
5)outFileNameSuffix指的是输出文件名的前缀,你可以修改它,如0809等。
6)内存有些小泄漏,你发现了吗? 嘻悠猴QQ表情大全

嘿嘿,源码下载时间到了: 嘻悠猴QQ表情大全
http://www.topven.com/labs/Cpp/080908/pic_name_chg_app.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值