c语言删除linux指定文件夹下指定数量的文件

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 1024

typedef struct file_info{
        char  permission[50];
        char linkNum[10] ;
        char owner[50];
        char group[50];
        char  size[20];
        char month[50];
        char day[50];
        char time[50];
        char fileName[100];
        } FILE_INFO;
        
struct list{  
    int index;
    FILE_INFO fileInfo;      /* 链表中包含的元素 */  
    struct list *next;  /* 指向下一个链表的指针 */  
    };
/* 定义一个链表头部 */  
static struct list *list_head = NULL;  

    void rmFile(char * path,int delNum);
int getFileList(char * dir);
static void list_add(struct list **head, struct list *list);
static void list_print(struct list **head);
static void list_free(struct list **head);
int main(){
    char path[]="/home/woxin/test2";//文件夹
    int delNum = 3;//要删除的数量
    rmFile(path,delNum);
}

    void rmFile(char * path,int delNum){
                    char str[100] = {0};
                    int flag = 0;
                    int fileNum = getFileList(path);
                    struct list *temp ;
                    temp= list_head;  
                    printf("要删除的文件夹:%s\n",path);
                    printf("文件夹下的文件数:%d\n",fileNum);
                    printf("要删除的文件数量:%d\n",delNum);
                    int k = 0;
                    int count=0;
                    for(;k<delNum;k++){
                        temp = temp->next;  //**********************这里虽然写出来了,但是不明白为什么要加这一句。*******************
                                 if(temp){  
                                    char cmd[100] = "rm -rf ";
                                    strcat(cmd,path);
                                    strcat(cmd,"/");
                                    strcat(cmd,temp->fileInfo.fileName);
                                    printf("执行的命令:%s\n",cmd);
                                    system(cmd);
                                    printf("fileName: %s is remove !\n",  temp->fileInfo.fileName); 
                                    temp = temp->next;  
                                     count++;
                                 }  
                                  if(count==delNum) break;    
                        }
                    int fileNum2 = getFileList(path);
                    printf("删除后文件的数量:path:%d\n",fileNum2);
                    list_free(&list_head);
        }

    int getFileList(char * dir){
            //ls -rlct
            FILE *f;
            char cmd[]="ls -rlct ";
            strcat(cmd,dir);
            char buffer[MAXLEN];
            char null[20];
            FILE* pipe = popen(cmd, "r");    
            if (!pipe)  return -1;    
            int i=0;
            for(;fgets(buffer, sizeof(buffer), pipe)!=NULL;i++){
                FILE_INFO  file;
                sscanf(buffer,"%s %s %s %s %s %s %s %s %s",file.permission,file.linkNum,file.owner,file.group,file.size,file.month,file.day,file.time,file.fileName);
                struct list * mylist = NULL;
                mylist = malloc(sizeof(struct list));  
                mylist->fileInfo = file;
                mylist->index = i;
                list_add(&list_head, mylist);
                //free(mylist);
                }
            pclose(pipe);   
            return i-1 ;
    }
//添加一个节点
static void list_add(struct list **head, struct list *list) {  
   struct list *temp;  
    /* 判断链表是否为空 */  
    if(NULL == *head) {  
        /* 为空 */  
        *head = list;  
        (*head)->next = NULL;  
    }else{  
        /* 不为空 */  
        temp = *head;  
        while(temp)  
        {  
            if(NULL == temp->next)  
            {  
                temp->next = list;  
                list->next = NULL;  
            }  
            temp = temp->next;  
        }  
    }  
}
//打印所有节点
static void list_print(struct list **head) {     
    struct list *temp;  
    temp = *head;  
    printf("list information :\n");  
    while(temp)  
    {  
        printf("\t %d permission: %s\n",   temp->index, temp->fileInfo.permission); 
        printf("\t linkNum: %s\n",  temp->fileInfo.linkNum); 
        printf("\t owner: %s\n",  temp->fileInfo.owner); 
        printf("\t group: %s\n",  temp->fileInfo.group); 
        printf("\t size: %s\n",  temp->fileInfo.size); 
        printf("\t month: %s\n",  temp->fileInfo.month); 
        printf("\t day: %s\n",  temp->fileInfo.day); 
        printf("\t time: %s\n",  temp->fileInfo.time); 
        printf("\t fileName: %s\n",  temp->fileInfo.fileName); 
        temp = temp->next;  
    }  
}
//释放所有节点
static void list_free(struct list **head) { 
      struct list *temp = *head;
       while(temp) { 
           struct list *p = temp->next;
           free(temp);
           temp=NULL;
           temp = p;
         }
}

打印结果:

要删除的文件夹:/home/mytest/test2
文件夹下的文件数:1400
要删除的文件数量:3
执行的命令:rm -rf /home/mytest/test2/S0000044.LOG
fileName: S0000044.LOG is remove !
执行的命令:rm -rf /home/mytest/test2/S0000039.LOG
fileName: S0000039.LOG is remove !
执行的命令:rm -rf /home/mytest/test2/S0000037.LOG
fileName: S0000037.LOG is remove !
删除后文件的数量:path:1397

 

posted @ 2018-05-29 11:21  一梦、  阅读(2879)  评论(0编辑  收藏  举报