当然,你也可以按自己想要的修改,代码来自AI
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
int stringToInt(char *s)
{
int res = 0;
int flag = 0;
while(*s != '\0' && (*s < '0' || *s > '9'))s++;
while(*s != '\0' && *s >= '0' && *s <= '9')
{
flag = 1;
res *= 10;
res += (*s - '0');
s++;
}
if(flag)
{
return res;
}
return -1;
}
void main(void)
{
printf("'''\n");
printf("\n");
printf(" ┏┓ ┏┓+ +\n");
printf(" ┏┛┻━━━┛┻┓ + +\n");
printf(" ┃ ┃ \n");
printf(" ┃ ━ ┃ ++ + + +\n");
printf(" ████━████ ┃+\n");
printf(" ┃ ┃ +\n");
printf(" ┃ ┻ ┃\n");
printf(" ┃ ┃ + +\n");
printf(" ┗━┓ ┏━┛\n");
printf(" ┃ ┃ \n");
printf(" ┃ ┃ + + + +\n");
printf(" ┃ ┃\n");
printf(" ┃ ┃ + 神兽保佑\n");
printf(" ┃ ┃ 代码无bug \n");
printf(" ┃ ┃ + \n");
printf(" ┃ ┗━━━┓ + +\n");
printf(" ┃ ┣┓\n");
printf(" ┃ ┏┛\n");
printf(" ┗┓┓┏━┳┓┏┛ + + + +\n");
printf(" ┃┫┫ ┃┫┫\n");
printf(" ┗┻┛ ┗┻┛+ + + +\n");
printf("\n");
printf("'''\n");
printf("It's my honor to see you again!\n");
const char imageSuffix[4][10]=
{
".jpg",
".jpg",
".bmp",
".png",
};
DIR *dir;
struct dirent *ent;
int fileCount = 0;
char oldFileName[500][256];
char newFileName[256];
int imageSuffixIndex;
//int needSort = 0;
int startIndex = 0;
dir = opendir(".");
printf("Please input 1or2or3 : 1:jpg ; 2:bmp ; 3:png ;\n");
scanf("%d",&imageSuffixIndex);
//printf("Do you need sort? : 1:yes ; 0:no;\n");
//scanf("%d",&needSort);
printf("Please input index,range : 0~N\n");
scanf("%d",&startIndex);
if(imageSuffixIndex <= 3)
{
fileCount = startIndex;
int len = 0;
if(dir== NULL)
{
perror("opendir");
system("pause");
return ;
}
len=0;
while ((ent = readdir(dir))!= NULL) {
const char *extension = strrchr(ent->d_name, '.');
if (extension && strcmp(extension, imageSuffix[imageSuffixIndex]) == 0) {
sprintf(oldFileName[len], "%s", ent->d_name);
len++;
}
}
closedir(dir);
//if(needSort)
//{
for(int i=0;i<len;i++)
{
int t = stringToInt(oldFileName[i]);
if(t != -1)
{
char tempstr[256];
int j = 0;
int m = 0;
while(oldFileName[i][m] != '\0' && (oldFileName[i][m] < '0' || oldFileName[i][m] > '9'))
{
tempstr[j] = oldFileName[i][m];
j++;
m++;
}
tempstr[j++] = t/10000+'0';t %= 10000;
tempstr[j++] = t/1000+'0';t %= 1000;
tempstr[j++] = t/100+'0';t %= 100;
tempstr[j++] = t/10+'0';t %= 10;
tempstr[j++] = t+'0';
while(oldFileName[i][m] != '\0' && oldFileName[i][m] >= '0' && oldFileName[i][m] <= '9')m++;
while(oldFileName[i][m] != '\0')
{
tempstr[j] = oldFileName[i][m];
j++;
m++;
}
tempstr[j] = '\0';
printf("rename:%s -> %s\n",oldFileName[i],tempstr);
rename(oldFileName[i], tempstr);
}
}
//}
dir = opendir(".");
if(dir== NULL)
{
perror("opendir");
system("pause");
return ;
}
len = 0;
while ((ent = readdir(dir))!= NULL) {
const char *extension = strrchr(ent->d_name, '.');
if (extension && strcmp(extension, imageSuffix[imageSuffixIndex]) == 0) {
sprintf(oldFileName[len], "%s", ent->d_name);
len++;
}
}
for(int i=0;i<len;i++)
{
sprintf(newFileName, "%d%s", fileCount,imageSuffix[imageSuffixIndex]);
rename(oldFileName[i], newFileName);
printf("%s -> %s\n",oldFileName[i],newFileName);
fileCount++;
}
closedir(dir);
}
else
{
printf("Error!\n");
}
system("pause");
}