C语言删除文件夹下的目录

前几天使用Android反编译工具反编译了360 的软件,Xml文件不可以用,但是每个xml都对应的编译出了一个.txt文件

恢复的办法就是删除.xml然后重命名.txt文件,文件很多 打算写个小工具,想着尝试用C 语言写下,中间还是遇到了些问题

记录下来了.

/*
 ============================================================================
 Name        : studyc.c
 Author      : popo
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
//显示删除失败原因
void show_error(const char *file_name )
{
    switch(errno)
    {
        case ENOTEMPTY:
            printf("Given path is not a directory, the directory is not empty, or the directory is either the current working directory or the root directory.\n");
            break;
        case ENOENT:
            printf("%s Path is invalid.\n",file_name);
            break;
        case EACCES:
            printf("%s had been opend by some application, can't delete.\n", file_name);
            break;
    }
}
int main(void) {
	printf("\n");
	DIR * dir;
	char fileName[300];
	char * fileType;
	int len;
	int delrs;
	int i;
	struct dirent * sonDir;
	if((dir=opendir("E:\\软件\\360MobileSafe_2.9.7beta\\res\\layout\\"))!=NULL){
//	if((dir=opendir("./"))!=NULL){
		printf("dir=%s\n",dir->dd_name);

		while((sonDir =readdir(dir))!=NULL){
			printf("sonDir=%s\n",sonDir->d_name);

			for(i=sonDir->d_namlen-1;i>=0;i--){
				if(sonDir->d_name[i]=='.'){
					//指针真是个好东西 不用再内存拷贝了
					fileType=&(sonDir->d_name[i]);
					break;
				}
			}
			//有后缀
			if(i>=0){
				if(strcmp(".txt",fileType)==0){
					//需要使用绝对路径删除 我刚开始删除的时候用的相对路径 结果不成功
					//使用show_error 后发现是路径不可用  才恍然大悟
					strcpy(fileName,dir->dd_name);
					fileName[strlen(fileName)-1]='\0';
					strcat(fileName,sonDir->d_name);
					delrs =remove(fileName);
					//删除失败打印错误信息
					if(delrs==-1)
						show_error(fileName);
					printf("we should delete the %s res=%d\n",fileName,delrs);

				}else{
					printf("we can not delete the %s \n",fileName);
				}
			}
		}
	}else{
		printf("file open fail");
	}
//	getchar();
	return 0;
}

这里这是删除文件部分 继续完成 重命名部分已经 递归完成子目录功能


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值