1,265
社区成员
发帖
与我相关
我的任务
分享
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
unsigned int Delete(char *pt, unsigned int cn)
{
unsigned int len=strlen(pt);
if(cn>=len){ *pt=0; return len; }
memmove(pt,pt+cn,len+1-cn);
return cn;
}
char *catfnam(char *pth, char *nam, int len, int d)
{
char * pt=malloc(len);
strcpy(pt, pth); pt[d]='/';
strcpy(pt+d+1, nam);
return pt;
}
int DirCross(char *pthnam)
{
/***** 你可以增加一些符号在下面三行 ******/
#define symCount 2
static char chs[symCount][2][3]= { { "(" , ")" } , { "(" , ")" } };
static len[symCount]={ 1, 2 };
/******** 以上供升级方便 ********/
DIR *dir=opendir(pthnam);
if(dir){
char buf[260], *pt, *dpt;
struct dirent *dt;
int i, b, d=strlen(pthnam);
while( ( dt=readdir(dir) ) )
if( strcmp(dt->d_name, ".") && strcmp(dt->d_name, "..") ){
strcpy(buf, dt->d_name );
b=strlen(buf);
for(i=0; i<symCount; i++ )
if( ( pt=strstr(buf, chs[i][0]) ) ){
Delete(pt, len[i]);
if(( pt=strstr(buf, chs[i][1]) ) ){
Delete(pt, len[i] );
pt=catfnam(pthnam, dt->d_name, d+b+4, d );
dpt=catfnam(pthnam,buf, d+b+2, d);
if(rename(pt,dpt)==0)printf("%s rename to %s\n", pt,dpt);
free(pt); free(dpt);
break;
}
}
if(dt->d_type&DT_DIR)DirCross(buf);
// free(dt);
}
}
}
int main(int c,char **pt)
{
if(c>1)DirCross(pt[1]);
return 1;
}