linux 下转换指定目录下所有文件字符编码
2015-04-22 13:46
281 查看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
void listDir(char *path, char *cur_dir)
{
DIR *pDir ; //定义一个DIR类的指针
struct dirent *ent ; //定义一个结构体 dirent的指针,dirent结构体见上
int i=0 ;
char childpath[512]; //定义一个字符数组,用来存放读取的路径
char command[512];
char file_name[512];
struct stat statbuf;
pDir=opendir(path); // opendir方法打开path目录,并将地址付给pDir指针
memset(childpath,0,sizeof(childpath)); //将字符数组childpath的数组元素全部置零
memset(file_name,0,sizeof(file_name));
memset(command,0,sizeof(command));
while((ent=readdir(pDir))!=NULL) //读取pDir打开的目录,并赋值给ent, 同时判断是否目录为空,不为空则执行循环体
{
sprintf(file_name, "%s/%s", path, ent->d_name);
if(ent->d_type == DT_DIR) //读取 打开目录的文件类型 并与 DT_DIR进行位与运算操作,即如果读取的d_type类型为DT_DIR (=4 表示读取的为目录)
{
if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0||strcmp(ent->d_name,"images")==0)
//如果读取的d_name为 . 或者.. 表示读取的是当前目录符和上一目录符, 用contiue跳过,不进行下面的输出
continue;
sprintf(childpath,"%s/%s",path,ent->d_name); //如果非. ..则将 路径 和 文件名d_name 付给childpath, 并在下一行prinf输出
printf("childpath:%s\n",childpath);
listDir(childpath, cur_dir); //递归读取下层的字目录内容, 因为是递归,所以从外往里逐次输出所有目录(路径+目录名),
//然后才在else中由内往外逐次输出所有文件名
}
else //如果读取的d_type类型不是 DT_DIR, 即读取的不是目录,而是文件,则直接输出 d_name, 即输出文件名
{
printf("file name : %s\n",file_name);
sprintf(command, "touch %s.tmp", file_name);
printf("%s\n",command);
system(command);
#if 1
memset(command,0,sizeof(command));
//printf("iconv input:%s/%s\n",cur_dir, file_name );
sprintf(command, "iconv -f gbk -t utf8 %s/%s -o %s/%s.tmp",cur_dir, file_name, cur_dir, file_name);// cur_dir, file_name);
//printf("command ===%s\n", command);
system(command);
memset(command,0,sizeof(command));
sprintf(command, "mv %s/%s.tmp %s/%s", cur_dir, file_name, cur_dir, file_name);
system(command);
//execute iconv command :iconv -f gbk -t utf8 ./test.java
#endif
}
}
}
int main(int argc,char *argv[])
{
char cur[512];
memset(cur,0,sizeof(cur));
getcwd(cur, 512);
listDir(argv[1], cur); //第一个参数为 想要转换的 linux 目录 例如,当前目录为 ./
return 0;
}
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
void listDir(char *path, char *cur_dir)
{
DIR *pDir ; //定义一个DIR类的指针
struct dirent *ent ; //定义一个结构体 dirent的指针,dirent结构体见上
int i=0 ;
char childpath[512]; //定义一个字符数组,用来存放读取的路径
char command[512];
char file_name[512];
struct stat statbuf;
pDir=opendir(path); // opendir方法打开path目录,并将地址付给pDir指针
memset(childpath,0,sizeof(childpath)); //将字符数组childpath的数组元素全部置零
memset(file_name,0,sizeof(file_name));
memset(command,0,sizeof(command));
while((ent=readdir(pDir))!=NULL) //读取pDir打开的目录,并赋值给ent, 同时判断是否目录为空,不为空则执行循环体
{
sprintf(file_name, "%s/%s", path, ent->d_name);
if(ent->d_type == DT_DIR) //读取 打开目录的文件类型 并与 DT_DIR进行位与运算操作,即如果读取的d_type类型为DT_DIR (=4 表示读取的为目录)
{
if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0||strcmp(ent->d_name,"images")==0)
//如果读取的d_name为 . 或者.. 表示读取的是当前目录符和上一目录符, 用contiue跳过,不进行下面的输出
continue;
sprintf(childpath,"%s/%s",path,ent->d_name); //如果非. ..则将 路径 和 文件名d_name 付给childpath, 并在下一行prinf输出
printf("childpath:%s\n",childpath);
listDir(childpath, cur_dir); //递归读取下层的字目录内容, 因为是递归,所以从外往里逐次输出所有目录(路径+目录名),
//然后才在else中由内往外逐次输出所有文件名
}
else //如果读取的d_type类型不是 DT_DIR, 即读取的不是目录,而是文件,则直接输出 d_name, 即输出文件名
{
printf("file name : %s\n",file_name);
sprintf(command, "touch %s.tmp", file_name);
printf("%s\n",command);
system(command);
#if 1
memset(command,0,sizeof(command));
//printf("iconv input:%s/%s\n",cur_dir, file_name );
sprintf(command, "iconv -f gbk -t utf8 %s/%s -o %s/%s.tmp",cur_dir, file_name, cur_dir, file_name);// cur_dir, file_name);
//printf("command ===%s\n", command);
system(command);
memset(command,0,sizeof(command));
sprintf(command, "mv %s/%s.tmp %s/%s", cur_dir, file_name, cur_dir, file_name);
system(command);
//execute iconv command :iconv -f gbk -t utf8 ./test.java
#endif
}
}
}
int main(int argc,char *argv[])
{
char cur[512];
memset(cur,0,sizeof(cur));
getcwd(cur, 512);
listDir(argv[1], cur); //第一个参数为 想要转换的 linux 目录 例如,当前目录为 ./
return 0;
}
相关文章推荐
- vb-复制ie临时文件夹下所有mp3文件到指定目录,并且将utf8编码转换过来
- Linux环境下列出指定目录下的所有文件
- linux面试题:删除一个目录下的所有文件,但保留一个指定文件
- Linux目录扫描:打印指定目录下的所有文件及目录
- Linux下如何遍历指定目录下的所有文件并删除指定天数之前创建的文件
- linux 删除目录下所有指定文件
- Linux下面删除指定目录下非指定文件的所有文件
- Linux监控系统磁盘使用比例,当使用率达到指定比例,删除指定目录下的所有文件的shell脚本
- Linux下如何遍历指定目录下的所有文件并删除指定天数之前创建的文件
- Linux查找当前目录及其子目录下所有包含指定字符串的文件
- Linux下使用find查找文件内容,跳过所有目录名成和指定子目录的方法
- linux 下pyhton 删除指定目录下所有的重复内容的文件
- Linux下替换指定的目录及其子目录下所有文件中的某些内容(3种方法)
- Linux-删除一个目录下的所有文件,但保留一个指定文件
- [Linux C]列出指定目录下的所有文件(夹)
- linux命令---删除一个目录下的所有文件,但保留一个指定文件
- Linux或者MAC下查找指定目录下的所有指定名字的文件并删除的命令
- Linux下检测指定目录下的所有打开的文件(包括子目录)并按照原有的目录结构保存一个副本
- linux面试题:删除一个目录下的所有文件,但保留一个指定文件
- linux 命令 改变指定目录以及其子目录下的所有文件的拥有者和群组