/*Copyright (C) 2012, schux*/
/*linux环境下:根据已有记录文件,群更改文件名
**记录文件形式如:fdsa^1^我的祖国^2^我的中国心^,标识字符为'^'
**把"1.mp3"更改为"我的祖国.mp3",把"2.mp3"更改为"我的中国心.mp3"
*/
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*打开当前目录,传入文件类型及类型名长度*/
void call( struct dirent *ptr, char *npt, size_t n );
/*在记录文件中取得新的目录所对应要更换的新的文件名,比如目录中是"1.mp3"更换为"我的祖国.mp3"*/
char *newFileName( FILE *fp );
/*取得特定字符object在文件fp中所在的位置*/
long int charPos( FILE *fp, char object );
int main()
{
DIR *dir;
struct dirent *ptr;
char *nptr;
size_t n;
dir = opendir( "./" );/*打开当前目录*/
while( (ptr = readdir(dir)) != NULL )/*当文件名ptr不为空时*/
{
if ( (nptr = strrchr(ptr->d_name, '.')) != NULL )/*当找到字符'.'最后出现的位置时*/
{
n = strlen( nptr );
if ( n >= 2 )/*文件有后缀时,比如:.c, .mp3*/
{
if ( strlen(ptr->d_name) - n >= 1 )/*文件名不为空时*/
{
printf( "oldFileName:%s\n", ptr->d_name );
call( ptr, nptr, n );
}
}/*end if*/
}
}/*end while*/
closedir( dir );
return 0;
}
void call( struct dirent *ptr, char *nptr, size_t n )
{
char pstr[256];/*目录中的文件名,不包括后缀".mp3"*/
char source[256];/*记录文件中的文件名*/
char new[256];/*新的文件名,包括文件后缀*/
long int flag_pos = 0;
size_t p;
FILE *fptr;
/*取得目录文件名*/
p = strlen( ptr->d_name ) - n;
strncpy( pstr, ptr->d_name, p );
pstr[p] = '\0';
if ( (fptr = fopen("local.txt", "rb")) != NULL )
{
rewind( fptr );
while ( !feof(fptr) )
{
if ( fgetc(fptr) == '^' )/*在记录文件中找到标示符'^'*/
{
if ( fread(source, sizeof(char), p, fptr) )
{
source[p] = '\0';
if ( !strcmp( pstr, source ) )/*目录中的文件中与记录文件中的文件名比较相同时*/
{
strcpy( new, newFileName(fptr) );
strcat( new, nptr );
rename( ptr->d_name, new );
printf( "newFileName:%s\n", new );
break;
}
else
{
fseek( fptr, flag_pos++, SEEK_SET );/*比较不相同时文件指针往前移一位*/
}
}/*end if*/
}
}/*end while*/
}/*end if*/
fclose( fptr );
}
char *newFileName( FILE *fp )
{
long int cur_pos;
long int obj_pos;
static char temp[256] = "error";
fseek( fp, 1, SEEK_CUR );/*跳过标示符'^'开始查找下一个标示符'^'*/
cur_pos = ftell( fp );
obj_pos = charPos( fp, '^');
if ( obj_pos != -2 )/*当检查标示符指针读取正确时*/
{
fread( temp, sizeof(char), obj_pos - cur_pos, fp );/*读取记录文件中新的文件名*/
}
temp[obj_pos - cur_pos] = '\0';
return temp;
}
long int charPos( FILE *fp, char object )
{
long int pos = -1;
long int cur_pos;
cur_pos = ftell( fp );
while ( !feof(fp) )
{
if ( fgetc(fp) == object )
{
pos = ftell( fp );
break;
}
}
fseek( fp, cur_pos, SEEK_SET );/*置指针回到原来开始读取的位置*/
return ( pos - 1 );
}
/*linux环境下:根据已有记录文件,群更改文件名
**记录文件形式如:fdsa^1^我的祖国^2^我的中国心^,标识字符为'^'
**把"1.mp3"更改为"我的祖国.mp3",把"2.mp3"更改为"我的中国心.mp3"
*/
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*打开当前目录,传入文件类型及类型名长度*/
void call( struct dirent *ptr, char *npt, size_t n );
/*在记录文件中取得新的目录所对应要更换的新的文件名,比如目录中是"1.mp3"更换为"我的祖国.mp3"*/
char *newFileName( FILE *fp );
/*取得特定字符object在文件fp中所在的位置*/
long int charPos( FILE *fp, char object );
int main()
{
DIR *dir;
struct dirent *ptr;
char *nptr;
size_t n;
dir = opendir( "./" );/*打开当前目录*/
while( (ptr = readdir(dir)) != NULL )/*当文件名ptr不为空时*/
{
if ( (nptr = strrchr(ptr->d_name, '.')) != NULL )/*当找到字符'.'最后出现的位置时*/
{
n = strlen( nptr );
if ( n >= 2 )/*文件有后缀时,比如:.c, .mp3*/
{
if ( strlen(ptr->d_name) - n >= 1 )/*文件名不为空时*/
{
printf( "oldFileName:%s\n", ptr->d_name );
call( ptr, nptr, n );
}
}/*end if*/
}
}/*end while*/
closedir( dir );
return 0;
}
void call( struct dirent *ptr, char *nptr, size_t n )
{
char pstr[256];/*目录中的文件名,不包括后缀".mp3"*/
char source[256];/*记录文件中的文件名*/
char new[256];/*新的文件名,包括文件后缀*/
long int flag_pos = 0;
size_t p;
FILE *fptr;
/*取得目录文件名*/
p = strlen( ptr->d_name ) - n;
strncpy( pstr, ptr->d_name, p );
pstr[p] = '\0';
if ( (fptr = fopen("local.txt", "rb")) != NULL )
{
rewind( fptr );
while ( !feof(fptr) )
{
if ( fgetc(fptr) == '^' )/*在记录文件中找到标示符'^'*/
{
if ( fread(source, sizeof(char), p, fptr) )
{
source[p] = '\0';
if ( !strcmp( pstr, source ) )/*目录中的文件中与记录文件中的文件名比较相同时*/
{
strcpy( new, newFileName(fptr) );
strcat( new, nptr );
rename( ptr->d_name, new );
printf( "newFileName:%s\n", new );
break;
}
else
{
fseek( fptr, flag_pos++, SEEK_SET );/*比较不相同时文件指针往前移一位*/
}
}/*end if*/
}
}/*end while*/
}/*end if*/
fclose( fptr );
}
char *newFileName( FILE *fp )
{
long int cur_pos;
long int obj_pos;
static char temp[256] = "error";
fseek( fp, 1, SEEK_CUR );/*跳过标示符'^'开始查找下一个标示符'^'*/
cur_pos = ftell( fp );
obj_pos = charPos( fp, '^');
if ( obj_pos != -2 )/*当检查标示符指针读取正确时*/
{
fread( temp, sizeof(char), obj_pos - cur_pos, fp );/*读取记录文件中新的文件名*/
}
temp[obj_pos - cur_pos] = '\0';
return temp;
}
long int charPos( FILE *fp, char object )
{
long int pos = -1;
long int cur_pos;
cur_pos = ftell( fp );
while ( !feof(fp) )
{
if ( fgetc(fp) == object )
{
pos = ftell( fp );
break;
}
}
fseek( fp, cur_pos, SEEK_SET );/*置指针回到原来开始读取的位置*/
return ( pos - 1 );
}