看板 C_and_CPP 關於我們 聯絡資訊
※ 引述《david741002 (MILK BABY)》之銘言: : 請問一下,我該如何取得我資料夾理檔案的資訊 : 如:修改時間、檔案大小、檔案名稱 : 使用CV2008 ++ 謝謝! 看過精華區後 發現小弟的方法似乎還沒po過.. #include <stdio.h> #include <string.h> #include <io.h> #include <direct.h> #include <time.h> #define _MAX_FNAME 2000 void EnumAllFileTree(char *path); void main() { char path[_MAX_FNAME] = "D:\\Course"; char filter[_MAX_FNAME] = "*.*"; char filename[_MAX_FNAME]; char attrib[_MAX_FNAME]; struct _finddata_t file; long hFile; struct tm *tmCreate, *tmWrite, *tmAccess; chdir(path); hFile = _findfirst(filter, &file); if(hFile!=-1){ do{ // 取得檔名 sprintf(filename, "%s\\%s", path, file.name); printf("%s\n", filename); memset((void*)attrib, 0, _MAX_FNAME); strcpy(attrib, "\tattrib:[ "); // 檔案屬性 if(file.attrib & _A_ARCH) strcat(attrib, "檔案 "); if(file.attrib & _A_HIDDEN) strcat(attrib, "隱藏 "); if(file.attrib & _A_NORMAL) strcat(attrib, "普通 "); if(file.attrib & _A_RDONLY) strcat(attrib, "唯讀 "); if(file.attrib & _A_SUBDIR) strcat(attrib, "資料夾 "); if(file.attrib & _A_SYSTEM) strcat(attrib, "系統 "); strcat(attrib, "]"); printf("%s\n", attrib); // 取得大小, 資料夾取得為0 printf("\tfilesize:%u\n",file.size); // 取得access, create, write 時間, 文字輸出 // printf("\tcreate:%s", ctime(&file.time_create)); // printf("\twrite:%s", ctime(&file.time_write)); // printf("\taccess:%s", ctime(&file.time_access)); // 取得access, create, write, 轉為 tm // 此處只抓 createtime tmCreate = localtime(&file.time_create); printf("\t%d年%d月%d日,%d時%d分%d秒, 星期%d\n", tmCreate->tm_year+1900, tmCreate->tm_mon+1, tmCreate->tm_mday, tmCreate->tm_hour, tmCreate->tm_min, tmCreate->tm_sec, tmCreate->tm_wday); }while(_findnext(hFile, &file)==0); _findclose(hFile); } // 下面這個要很久唷... // 把 D 槽底下所有檔案都抓出來.. EnumAllFileTree("D:\\"); } void EnumAllFileTree(char *path) { chdir(path); char filter[_MAX_FNAME] = "*.*"; char filename[_MAX_FNAME]; char attrib[_MAX_FNAME]; struct _finddata_t file; long hFile; hFile = _findfirst(filter, &file); if(hFile!=-1){ do{ // 先跳過 "這一層" 與 "上一層" if(!strcmp(file.name, ".") || !strcmp(file.name,"..")){ continue; } sprintf(filename, "%s\\%s", path, file.name); printf("%s\n", filename); // 查到資料夾,進去全部找一次 if(file.attrib & _A_SUBDIR) { printf("%s <folder>\n", filename); EnumAllFileTree(filename); } }while(_findnext(hFile, &file)==0); _findclose(hFile); } } -- 我期待 我等待 肩狹骨上的翅膀早些長出來 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.177.76.142