1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #pragma warning(disable:4996) //VS 编译声明?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h> #include <time.h>
struct _finddata_t;
char* get_name(void) //不定长输入地址 { char *name, *temp; int j = 1;
name = (char*)malloc(sizeof(char)*(j + 1)); if (NULL == name) { exit(1); }
while ((name[j - 1] = getchar()) != '\n') { j++; temp = (char*)malloc(j + 1); if (NULL == temp) { exit(1); } name[j - 1] = '\0'; strcpy(temp, name); free(name);
name = (char*)malloc(sizeof(char)*(j + 1)); if (NULL == name) { exit(1); } strcpy(name, temp); free(temp); }
name[j - 1] = '\0';
return(name); }
char* get_address(void) { char * file_loc; printf("##### 输入文件夹地址(./****/) #####\n"); //文件地址 file_loc = get_name(); return file_loc; }
void file_search(char*file_loc, int layer, char* file_type) //file_search { /*################### 将文件夹内文件信息输出 ###################*/ struct _finddata_t fileinfo; //文件存储信息结构体 long fHandle; //保存文件句柄 int i = 0; //文件数记录器 time_t Time_create, Time_write, Time_access; //时间格式
int Layer; //子文件夹层 char *curr; int loc_len, type_len;
loc_len = strlen(file_loc); type_len = strlen(file_type);
curr = (char*)malloc(loc_len + type_len+1); if (NULL == curr) { exit(1); }
strcpy(curr, file_loc); strcat(curr, file_type); //printf("file_add test output: %s\n", curr);
if ((fHandle = _findfirst(curr , &fileinfo)) != -1L) { do { if (strcmp(fileinfo.name, "..") == 0) continue; if (strcmp(fileinfo.name, ".") == 0) continue; for (Layer = 0; Layer < layer; Layer++) printf("\t");
if ((_A_SUBDIR == fileinfo.attrib)) // 是目录 { printf("[Dir]:\t%s\n", fileinfo.name); char* curr_n; curr_n = (char*)malloc(loc_len + 1 + strlen(fileinfo.name)); if (NULL == curr_n) { exit(1); }
strcpy(curr_n, file_loc); strcat(curr_n, fileinfo.name); strcat(curr_n, "/"); file_search(curr_n, layer + 1, file_type); // 递归遍历子目录 } else { Time_create = time(&fileinfo.time_create); Time_write = time(&fileinfo.time_write); Time_access = time(&fileinfo.time_access);
//i++; printf("[File]:\t找到文件:%-s\t文件大小:%-d KB\n", fileinfo.name, (fileinfo.size) / 1000); for (Layer = 0; Layer < (layer + 1); Layer++) printf("\t"); printf("创建日期:%-s\n", ctime(&fileinfo.time_create)); for (Layer = 0; Layer < (layer + 1); Layer++) printf("\t"); printf("修改日期:%-s\n", ctime(&fileinfo.time_write)); for (Layer = 0; Layer < (layer + 1); Layer++) printf("\t"); printf("访问日期:%-s\n", ctime(&fileinfo.time_access)); //printf("[File]:\t%s\n",fileinfo.name); }
} while (_findnext(fHandle, &fileinfo) == 0); }
_findclose(fHandle); //关闭文件
//printf("文件数量:%d\n", i); }
int main(void) { printf("##### Program initiating #####\n");
char* filetype,*fileloc; printf("##### 输入要查找的文件类型(*.xxxx) #####\n"); //文件类型 filetype = get_name();
fileloc = get_address();
file_search(fileloc,0,filetype);
system("pause"); }
|