下面是具體的C語言程式實現批次更改檔名:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<dirent.h>
void change_str(char *str, char *a, char *b)
{
char *p, q[100] = {0};
p = strstr(str, a);
p += strlen(a);
strcpy(q, p);
*(p - strlen(a)) = 0;
strcat(str, b);
strcat(str, q);
}
int main()
const char dir[512] = "D:\\work\\工作\\三區\\work\\北京市_三區_有色冶金設計研究院\\北京市_三區_有色冶金設計研究院11號樓\\";
const a = "心_";
const b = "心";
char filename[512] = {0}, *p, name_temp[512] = {0};
char new_name[512] = {0}, old_name[512] = {0};
char tmp[512] = "COPY ";
char name_tmp[512] = {0};
DIR *directory_pointer;
struct dirent *entry;
strcpy(new_name, dir);
strcpy(old_name, dir);
if((directory_pointer=opendir(dir))==NULL)
printf( "Error opening \n ");
else
while((entry=readdir(directory_pointer))!=NULL)
if(p = strstr(entry-> d_name, a))
strcpy(name_temp, entry -> d_name);
change_str(name_temp, a, b);
strcat(old_name, entry -> d_name);
strcat(new_name, name_temp);
if(rename(old_name, new_name) == 0)
printf("%s\n", name_temp);
strcpy(name_tmp, "\0");
system("PAUSE");
下面是具體的C語言程式實現批次更改檔名:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<dirent.h>
void change_str(char *str, char *a, char *b)
{
char *p, q[100] = {0};
p = strstr(str, a);
p += strlen(a);
strcpy(q, p);
*(p - strlen(a)) = 0;
strcat(str, b);
strcat(str, q);
}
int main()
{
const char dir[512] = "D:\\work\\工作\\三區\\work\\北京市_三區_有色冶金設計研究院\\北京市_三區_有色冶金設計研究院11號樓\\";
const a = "心_";
const b = "心";
char filename[512] = {0}, *p, name_temp[512] = {0};
char new_name[512] = {0}, old_name[512] = {0};
char tmp[512] = "COPY ";
char name_tmp[512] = {0};
DIR *directory_pointer;
struct dirent *entry;
strcpy(new_name, dir);
strcpy(old_name, dir);
if((directory_pointer=opendir(dir))==NULL)
printf( "Error opening \n ");
else
{
while((entry=readdir(directory_pointer))!=NULL)
{
if(p = strstr(entry-> d_name, a))
{
strcpy(name_temp, entry -> d_name);
change_str(name_temp, a, b);
strcat(old_name, entry -> d_name);
strcat(new_name, name_temp);
if(rename(old_name, new_name) == 0)
printf("%s\n", name_temp);
}
strcpy(new_name, dir);
strcpy(old_name, dir);
strcpy(name_tmp, "\0");
}
}
system("PAUSE");
}