代碼:
#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