1. 程式人生 > >C++實現遍歷指定檔案或資料夾

C++實現遍歷指定檔案或資料夾

//************************************************************************
// 函式名稱:    	ReadFilenameFromFolder
// 訪問許可權:    	public 
// 建立日期:		2016/12/26
// 創 建 人:		
// 函式說明:		讀取一個目錄下所有的Dcm檔名
// 函式引數: 	std::string foldername 資料夾所在的資料夾路徑
// 返 回 值:   	bool
//************************************************************************
bool CDcmRead::ReadFilenameFromFolder(std::string foldername)
{
	if (!this->GetFolderName(foldername))	//判斷路徑可用性
	{
		std::cout << "輸入檔案路徑錯誤無法獲取" << std::endl;
		return false;
	}

	foldername = this->m_szInitDir;
	if (!this->BrowseFolder(foldername.c_str()))
	{
		std::cout << "遍歷資料夾失敗" << std::endl;
		return false;
	}

	return true;
}

//************************************************************************
// 函式名稱:    	BrowseFolder
// 訪問許可權:    	public 
// 建立日期:		2016/12/26
// 創 建 人:		
// 函式說明:		遍歷資料夾,提取出目錄下和子目錄的所有dcm檔名
// 函式引數: 	const char * foler_name	資料夾名稱
// 返 回 值:   	bool
//************************************************************************
bool CDcmRead::BrowseFolder(const char* foler_name)
{
	_chdir(foler_name); 

	//首先查詢dir中符合要求的檔案  
	long hFile;
	_finddata_t fileinfo;
	if ((hFile = _findfirst("*.dcm", &fileinfo)) != -1)//這裡的 *.dcm 是我感興趣的檔案字尾名
	{
		do
		{
			//檢查是不是目錄  
			//如果不是,則進行處理  
			if (!(fileinfo.attrib & _A_SUBDIR))
			{
				char filename[_MAX_PATH];
				strcpy(filename, foler_name);
				strcat(filename, fileinfo.name);
#ifdef DEBUG
				std::cout << filename << std::endl;
#endif // DEBUG
				this->filepath.push_back(filename);
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
	//查詢dir中的子目錄  
	//因為在處理dir中的檔案時,派生類的ProcessFile有可能改變了  
	//當前目錄,因此還要重新設定當前目錄為dir。  
	//執行過_findfirst後,可能系統記錄下了相關資訊,因此改變目錄  
	//對_findnext沒有影響。  
	_chdir(foler_name);
	if ((hFile = _findfirst("*.*", &fileinfo)) != -1)
	{
		do
		{
			//檢查是不是目錄,如果是,再檢查是不是 . 或 ..   
			//如果不是,進行迭代  
			if ((fileinfo.attrib & _A_SUBDIR))
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp
					(fileinfo.name, "..") != 0)
				{
					char subdir[_MAX_PATH];
					strcpy(subdir, foler_name);
					strcat(subdir, fileinfo.name);
					strcat(subdir, "\\");
#ifdef DEBUG
					std::cout << "找到子資料夾: " << subdir << std::endl;
#endif // DEBUG
					if (!this->BrowseFolder(subdir))
						return false;
				}
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}

	return true;
}

//************************************************************************
// 函式名稱:    	GetFolderName
// 訪問許可權:    	public 
// 建立日期:		2016/12/26
// 創 建 人:		
// 函式說明:		規整化目錄
// 函式引數: 	std::string foldername	傳入的資料夾路徑
// 返 回 值:   	bool
//************************************************************************
bool CDcmRead::GetFolderName(std::string foldername)
{
	//先把dir轉換為絕對路徑  
	if (_fullpath(m_szInitDir, foldername.c_str(), _MAX_PATH) == NULL)
	{
		std::cout << "GetFolderName:獲取輸入路徑的絕對路徑失敗!" << std::endl;
		return false;
	}

	//判斷目錄是否存在  
	if (_chdir(m_szInitDir) != 0)
	{
		std::cout << "GetFolderName:輸入的路徑不存在!" << std::endl;
		return false;
	}

	//如果目錄的最後一個字母不是'\',則在最後加上一個'\'  
	int len = strlen(m_szInitDir);
	if (m_szInitDir[len - 1] != '\\')
		strcat(m_szInitDir, "\\");

	return true;
}
程式遞迴呼叫,將所有指定的檔案字尾檔案儲存在filepath這個類成員變數中。