如何搜尋目錄之下的檔案 |
尚未結案
|
tim0214
一般會員 發表:4 回覆:2 積分:1 註冊:2005-10-27 發送簡訊給我 |
|
chtai
高階會員 發表:68 回覆:238 積分:116 註冊:2004-05-21 發送簡訊給我 |
這是我以前的程式碼,參數分別是要找的路徑、要找的副檔名、是否要包含子目錄,
會把找到的檔案名稱放到一個 ListView,
程式碼有些冗長,其實還可以更簡潔,給你參考參考。
void TForm1::FindFilesInDirectory(String directory, String extension, bool searchSubdir) { WIN32_FIND_DATA filedata ; // Structure for file data HANDLE filehandle ; // Handle for searching if( extension == "" ) extension = "*.*"; // Pass 1 - Search for the files within the directory. filehandle = FindFirstFile((directory extension).c_str(), &filedata) ; if( filehandle != INVALID_HANDLE_VALUE ) { do { if( (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0 ) { // Display the file name. TListItem *li = ListView1->Items->Add(); li->Caption = directory filedata.cFileName ; // Display the file extension. li->SubItems->Add(ExtractFileExt(filedata.cFileName)); // Display the file size. if( filedata.nFileSizeHigh == 0 ) { int filesize = filedata.nFileSizeLow 1023; if( filesize >= 1073741824 ) // 1024^3 li->SubItems->Add(String(filesize / 1073741824) " GB") ; else if( filesize >= 1048576 ) // 1024^2 li->SubItems->Add(String(filesize / 1048576) " MB") ; else li->SubItems->Add(String(filesize / 1024) " KB") ; // Add filesize to SubItems, for sorting file size purpose. li->SubItems->Add(String(filesize)); } else { // If nFileSizeHigh is non-zero then this is a **HUGE** file. double filesize = (MAXDWORD * filedata.nFileSizeHigh) filedata.nFileSizeLow; if( filesize >= 1073741824.0 ) // 1024^3 li->SubItems->Add(String(filesize / 1073741824.0) " GB") ; else if( filesize >= 1048576.0 ) // 1024^2 li->SubItems->Add(String(filesize / 1048576.0) " MB") ; else li->SubItems->Add(String(filesize / 1024.0) " KB") ; // Add filesize to SubItems, for sorting file size purpose. li->SubItems->Add(String(filesize)); } Application->ProcessMessages() ; } } while(FindNextFile(filehandle, &filedata)) ; FindClose(filehandle) ; } if(searchSubdir) // If user wanna search subdirectories.. { // Pass 2 - Search for all the subdirectories within this directory filehandle = FindFirstFile ((directory "*.*").c_str (), &filedata) ; if( filehandle != INVALID_HANDLE_VALUE) { do { if( (filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && String (filedata.cFileName) != "." && String (filedata.cFileName) != "..") { // Recursive call here FindFilesInDirectory(directory filedata.cFileName "\\", extension, true) ; } Application->ProcessMessages() ; } while(FindNextFile(filehandle, &filedata)) ; FindClose(filehandle) ; } Application->ProcessMessages() ; } }-- http://www.csie.nctu.edu.tw/~chtai/software.php 好用免費軟體的收集網頁
------
My Web: http://nelson.csie.us My Blog: http://blog.nelson.csie.us |
RedSnow
版主 發表:79 回覆:1322 積分:845 註冊:2003-12-15 發送簡訊給我 |
tim0214 您好: 以下是修改自 BCB Help 的範例,您參考一下吧:
#include7 天天敲鍵盤 v 時時按滑鼠 8 |
tim0214
一般會員 發表:4 回覆:2 積分:1 註冊:2005-10-27 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |