zoukankan      html  css  js  c++  java
  • 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
    ",path);
                        printf("文件夹下的文件数:%d
    ",fileNum);
                        printf("要删除的文件数量:%d
    ",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
    ",cmd);
                                        system(cmd);
                                        printf("fileName: %s is remove !
    ",  temp->fileInfo.fileName); 
                                        temp = temp->next;  
                                         count++;
                                     }  
                                      if(count==delNum) break;    
                            }
                        int fileNum2 = getFileList(path);
                        printf("删除后文件的数量:path:%d
    ",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 :
    ");  
        while(temp)  
        {  
            printf("	 %d permission: %s
    ",   temp->index, temp->fileInfo.permission); 
            printf("	 linkNum: %s
    ",  temp->fileInfo.linkNum); 
            printf("	 owner: %s
    ",  temp->fileInfo.owner); 
            printf("	 group: %s
    ",  temp->fileInfo.group); 
            printf("	 size: %s
    ",  temp->fileInfo.size); 
            printf("	 month: %s
    ",  temp->fileInfo.month); 
            printf("	 day: %s
    ",  temp->fileInfo.day); 
            printf("	 time: %s
    ",  temp->fileInfo.time); 
            printf("	 fileName: %s
    ",  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
  • 相关阅读:
    CocoaPods 的简单快速安装方法
    macOS Catalina new Shell,解决 The default interactive shell is now zsh
    Mac入门--通过homebrew下载过慢问题
    Mac下安装Android Studio
    Mac更新catalina之后有道词典闪退的解决办法
    mac系统下安装Java开发环境(一)——JDK安装
    聊天案例
    ios中常用k线
    ubuntu连接蓝牙鼠标
    image_transport
  • 原文地址:https://www.cnblogs.com/airduce/p/9104108.html
Copyright © 2011-2022 走看看