最近碰到一件事,需要把修改.cpp文件修改成.cc文件~~所以就写了这个程序(或许对某些人有用贴上来):
/*******************************************************************
This is a program that can change the type of files in Linux.
Author : LiuJie
Address: University of Science and Technology Beijing
Email : lj_533@163.com
usage : changetype [-a][.oldtype] [.newtype]
if you chose the -a option, you could change the
type of all files to .newtype.
Date : 2006-6-15
File : changetype.c
********************************************************************/
#include
#include
#include
int main( int argc, char *argv[])
{
char strChan[100];
char strChaned[100];
if( argc >1 )
{
sscanf( argv[1], "%s", strChaned );
sscanf( argv[2], "%s", strChan );
if( strChaned[0] == '-' )
{
if( strChaned[1] == 'a' || strChaned[1] == 'A' )
{
strcpy(strChaned, ".");
}
else
{
fprintf( stderr, "usage: changetype [-a][.oldtype] [.newtype]\n");
exit(1);
}
}
else if( strChaned[0] != '.' || strChan[0] != '.' )
{
fprintf( stderr, "usage: changetype [-a][.oldtype] [.newtype]\n");
exit(1);
}
}
else
{
fprintf( stderr, "usage: changetype [-a][.oldtype] [.newtype]\n");
exit(1);
}
DIR * dir;
struct dirent* ptr=NULL;
dir = opendir(".");
char newName[1024];
while ((ptr = readdir(dir)) != NULL)
{
if (ptr->d_name[0] != '.')
{
if(strstr(ptr->d_name,strChaned))
{
/* printf( " %s\n", ptr->d_name);*/
strcpy( newName, ptr->d_name );
strtok(newName,".");
strcat(newName,strChan);
/* printf(" %s\n", newName);
printf(" %s\n", ptr->d_name);*/
if( rename(ptr->d_name, newName) == 0)
{
printf("Good! %s files change to %s files\n",ptr->d_name, newName );
}
else
{
perror("ERROR!\n");
}
}
}
}
return 0;
}
[root@localhost root]#gcc - o changetype changetype.c
[root@localhost root]#./changetype .cpp .cc
Good! BatterySimple.cpp files change to BatterySimple.cc files
Good! RadioSimple.cpp files change to RadioSimple.cc files
Good! CPUSimple.cpp files change to CPUSimple.cc files
阅读(1119) | 评论(0) | 转发(0) |