⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dirorfile.cpp

📁 win32下
💻 CPP
字号:
// 11111Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "DirOrFile.h"
#include <direct.h>


CDirOrFile::CDirOrFile()
{

}

CDirOrFile::~CDirOrFile()
{
}


//递归查找源目录及其子目录中,每个文件
int CDirOrFile::SearchAllFileInPath_(string& strRootDir)
{
	if( strRootDir[strRootDir.size() - 1] != '\\' )
    {
	   strRootDir += "\\";
    }

	string strTmpPath;
	string strFindPath = strRootDir;
	strFindPath += "*.*";

	HANDLE hSearch = NULL;
	WIN32_FIND_DATA  findFileData;
	string strTipInfo;

	::ZeroMemory(&findFileData, sizeof(WIN32_FIND_DATA));
	hSearch = ::FindFirstFile(strFindPath.c_str(), &findFileData);
	if (hSearch != INVALID_HANDLE_VALUE)
	{
		BOOL bSearchFinished  = TRUE;

		while (bSearchFinished)
		{
			bSearchFinished = ::FindNextFile(hSearch, &findFileData);
			if (bSearchFinished)
			{
				if ( (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
					lstrcmp(findFileData.cFileName, ".")  != 0 &&
					lstrcmp(findFileData.cFileName, "..") != 0  
					)   //对于当前为. ..
				{
					strTmpPath = strRootDir;
					strTmpPath += findFileData.cFileName;

					this->SearchAllFileInPath_(strTmpPath);

					continue;
				}
				else if ( (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
					( lstrcmp(findFileData.cFileName, ".")  == 0 ||
					lstrcmp(findFileData.cFileName, "..")  == 0  ) )
				{
					continue;
				}

				//------------------------------------
				//处理文件
				strTmpPath = strRootDir;
				strTmpPath += findFileData.cFileName;

				////////
			}
		}
	}

	if (hSearch != INVALID_HANDLE_VALUE)
	{
		::FindClose(hSearch);
	}

	return 0;
}


//删除某个目录下的所有的子目录及其本身
int CDirOrFile::DeleteDirInPath_(string& strRootDir)
{
	if( strRootDir[strRootDir.size() - 1] != '\\' )
    {
	   strRootDir += "\\";
    }

	string strTmpPath;
	string strFindPath = strRootDir;
	strFindPath += "*.*";

	HANDLE hSearch = NULL;
	WIN32_FIND_DATA  findFileData;

	::ZeroMemory(&findFileData, sizeof(WIN32_FIND_DATA));
	hSearch = ::FindFirstFile(strFindPath.c_str(), &findFileData);
	if (hSearch != INVALID_HANDLE_VALUE)
	{
		BOOL bSearchFinished  = TRUE;

		while (bSearchFinished)
		{
			bSearchFinished = ::FindNextFile(hSearch, &findFileData);
			if (bSearchFinished)
			{
				if ( (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
					lstrcmp(findFileData.cFileName, ".")  != 0 &&
					lstrcmp(findFileData.cFileName, "..") != 0  
					)   //对于当前为. ..
				{
					strTmpPath = strRootDir;
					strTmpPath += findFileData.cFileName;

					//MessageBox(strTmpPath.c_str());
					DeleteDirInPath_(strTmpPath); 

					continue;
				}
				else if ( (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
					( lstrcmp(findFileData.cFileName, ".")  == 0 ||
					lstrcmp(findFileData.cFileName, "..")  == 0  ) )
				{
					continue;
				}
			}
		}
	}

	if (hSearch != INVALID_HANDLE_VALUE)
	{
		::FindClose(hSearch);
	}

	if  (!RemoveDirectory(strRootDir.c_str()))   
	   return  0;

	return 0;
}

bool CDirOrFile::SelectOnePath(string & strNewPathToSel, HWND hParWnd)
{
	strNewPathToSel.clear();

	BROWSEINFO bi;           //控制Browse对话框上属性的结构体 
	LPITEMIDLIST ItemID; 

	char sDir[MAX_PATH];
	char FolderName[MAX_PATH]; 

	memset(&bi, 0, sizeof(BROWSEINFO))
	memset(sDir, 0, MAX_PATH); 

	bi.hwndOwner      = hParWnd;
	bi.pszDisplayName = FolderName;                 //存放路径的缓冲区 
	bi.lpszTitle      = "选择扫描目录";            //对话框标题 
	bi.ulFlags        = BIF_RETURNONLYFSDIRS;       //只显示文件系统文件夹 

	ItemID = SHBrowseForFolder(&bi);
	if (ItemID == NULL) 
	{
		return false;
	}

	SHGetPathFromIDList(ItemID, sDir); 
	GlobalFreePtr(ItemID); 

	if (sDir[0] == '\0') 
	{
		return false;
	}

	strNewPathToSel = sDir;
	if (strNewPathToSel[strNewPathToSel.length() - 1] != '\\')
		strNewPathToSel += "\\";     //添加路径符

	return true;
}

//----------------------------------------
//得到某文件的路径名
//
//strFile - 文件名(包含路径)
//strPath - 返回文件对应的路径
//strName - 返回文件名
//strExt  - 返回文件扩展名
//----------------------------------------
void CPathFileHandler::SplitFileName(const string& strFile, string& strPath, string& strName, string & strExt)
{
	strPath.clear();

	if (strFile.empty())
		return;

	TCHAR szDirve[_MAX_DRIVE] = {0};
	TCHAR szDir[_MAX_DIR] = {0};
	TCHAR szFName[_MAX_FNAME] = {0};
	TCHAR szExt[_MAX_EXT] = {0};

	_splitpath(strFile.c_str(), szDirve, szDir, szFName, szExt);

	strPath = szDirve;
	strPath += szDir;

	strName = szFName;

	strExt = szExt;
	for (int i = 0; i < strExt.length(); i++) //转换为大写
	{
		strExt[i] = toupper(strExt[i]);
	}
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -