最近,由于工作的需要自己封装了一个对对文件夹的操作的类(CDirectory)。 有了这个类之后我们对文件操作就显得十分的简单,在这里就不废话了。首先来看一下怎么使用这个CDirectory。 CDirectory类实现了对目录的创建、删除、拷贝、取得文件夹的大小、检查目录的存在与否和从指定的文件夹中查找指定的后缀名。
- CDirector DirOperator;
-
- DirOperator. CreateDirectorySerial(TEXT("c://Dir//A//B"));
-
-
- DirOperator. GetDirectorySize(TEXT("c://Dir//A//B"));
-
- if(CheckDirectoryExist(TEXT("c://Dir//A//B")))
- {
- RETAILMSG(TRUE,(TEXT("Directory exist!/r/n")));
- }
- else
- {
- RETAILMSG(TRUE,(TEXT("Directory does not exist!/r/n")));
- }
-
-
- DirOperator. CopyDirectory (TEXT("D://Dir//A//C"),TEXT("c://Dir//A//B")):
-
-
- std::vector<TSTRING> vFilePathList
- DirOperator.FindFileFromDirectory(TEXT("c://Dir//A//B"),TEXT("File.exe"),vFilePathList);
-
- DirOperator.FindFileFromDirectory(TEXT("c://Dir//A//B"),TEXT("*.exe"),vFilePathList);
-
-
- DirOperator. DeleteDirectorye(TEXT("c://Dir"));
- #pragma once
-
- #include <windows.h>
- #include <TChar.h>
- #include <string>
- #include <map>
- #include <vector>
- #include <algorithm>
-
-
-
- #ifdef UNICODE
- #ifndef TSTRING
- #define TSTRING std::wstring
- #endif
- #else
- #ifndef TSTRING
- #define TSTRING std::string
- #endif
- #endif
-
- #ifndef _WIN32_WCE
-
- enum FileDeviceType
- {
- DISK_FIXED,
- DISK_REMOVABLE,
- CDROM
- };
- #endif
-
- class CDirectory
- {
- public:
-
-
-
-
-
-
-
- DWORD GetDirectorySize(const TSTRING &strPath);
-
-
-
-
-
-
-
-
- BOOL CheckDirectoryExist(const TSTRING &strPath);
-
-
-
-
-
-
-
-
-
- BOOL CopyDirectory(const TSTRING &strDestinationPath,const TSTRING &strSourcePath);
-
-
-
-
-
-
-
-
-
- void CreateDirectorySerial(const TSTRING &strPath);
-
-
-
-
-
-
-
-
- void DeleteDirectory(const TSTRING &strPath);
-
-
-
-
-
-
-
-
-
-
- TCHAR *GetCurrentDirectory(TCHAR *pszPath, ULONG ulSize);
-
-
-
-
-
-
-
-
-
-
-
-
- BOOL FindFileFromDirectory(const TSTRING &DirectoryPath,const TSTRING &SuffixName,std::vector<TSTRING> &vFilePathList);
-
- #ifndef _WIN32_WCE
-
-
-
-
-
-
-
-
- void GetDiskPartition(std::multimap<FileDeviceType,std::wstring> &mDiskParition);
- #endif
-
- protected:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int FindString(const TCHAR *szSource, const TCHAR *szFind,const int iBeginPos);
-
-
-
-
-
-
-
-
-
-
-
-
-
- BOOL CheckFileSuffix(const TSTRING &FileName,const TSTRING &SuffixName);
-
-
- public:
- CDirectory();
- virtual ~CDirectory();
- };
- #include "stdafx.h"
- #include "Directory.h"
-
-
- CDirectory::CDirectory()
- {
-
- }
- CDirectory::~CDirectory()
- {
- }
-
- TCHAR *CDirectory::GetCurrentDirectory(TCHAR *pszPath, ULONG ulSize)
- {
- memset(pszPath, 0, sizeof(TCHAR) * ulSize);
-
- TCHAR szBuf[MAX_PATH] = {0};
- GetModuleFileName(NULL,szBuf,sizeof(szBuf)/sizeof(TCHAR));
-
- int ulCount = _tcslen(szBuf);
-
- while(--ulCount >= 0)
- {
- if(szBuf[ulCount] == TEXT('//'))
- {
- break;
- }
- else
- {
- continue;
- }
- }
-
- if(ulSize > (DWORD)ulCount)
- {
- _tcsncpy(pszPath,szBuf,(ulCount + 1));
- }
-
- return pszPath;
- }
-
- void CDirectory::CreateDirectorySerial(const TSTRING &strPath)
- {
-
- TSTRING SourcePath = strPath;
-
- if(strPath.size() >= MAX_PATH)
- return;
-
- if(SourcePath[SourcePath.size() -1 ] == TEXT('//'))
- {
- SourcePath[SourcePath.size() -1 ] = TEXT('/0');
- }
-
- if(CheckDirectoryExist(strPath))
- return;
-
- TSTRING::size_type idx = SourcePath.find(TEXT('//'));
-
- while(idx !=TSTRING::npos)
- {
- TSTRING strCreateDirPath;
- strCreateDirPath.assign(SourcePath,0,idx);
- if(!CheckDirectoryExist(strCreateDirPath))
- {
- CreateDirectory(strCreateDirPath.c_str(),NULL);
- }
-
- idx = SourcePath.find(TEXT('//'),idx + 1);
- }
-
- CreateDirectory(SourcePath.c_str(),NULL);
- }
-
- void CDirectory::DeleteDirectory(const TSTRING &strPath)
- {
- TSTRING strDirPath = strPath;
- #ifdef _WIN32_WCE
- if(strDirPath[0] != TEXT('//'))
- {
- return;
- }
- #endif
- if(strDirPath[strDirPath.size() - 1] == TEXT('//'))
- {
- strDirPath += TEXT("*.*");
- }
- else
- {
- strDirPath += TEXT("//*.*");
- }
-
- WIN32_FIND_DATA fd;
- HANDLE hdFind;
- hdFind = FindFirstFile(strDirPath.c_str(),&fd);
- if(hdFind != INVALID_HANDLE_VALUE)
- {
- do{
-
- if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
- {
-
- TSTRING strNextDir = strPath;
- if(strNextDir[strNextDir.size() -1] != TEXT('//'))
- strNextDir += TEXT("//");
-
- strNextDir += fd.cFileName;
- DeleteDirectory(strNextDir);
- RemoveDirectory(strNextDir.c_str());
- }
- else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- {
-
- TSTRING strPathFile = strPath;
- if(strPathFile[strPathFile.size() - 1] !=TEXT( '//'))
- strPathFile += TEXT("//");
-
- strPathFile += fd.cFileName;
- DeleteFile(strPathFile.c_str());
- }
- }while(FindNextFile(hdFind,&fd));
- }
- FindClose(hdFind);
-
- RemoveDirectory(strPath.c_str());
- }
-
- int CDirectory::FindString(const TCHAR *szSource, const TCHAR *szFind, const int iBeginPos)
- {
- int iLenSource = _tcslen(szSource);
- int iLenFind = _tcslen(szFind);
-
- if(iLenSource - 1 < iBeginPos)
- {
- return -1;
- }
-
- int iCount = 0;
- int iFindCount = 0;
- BOOL bPair = FALSE;
- for(iCount = 0; iCount < iLenSource - iBeginPos; iCount++)
- {
- if(szSource[iCount + iBeginPos] == szFind[iFindCount])
- {
- if(iFindCount == iLenFind - 1)
- {
- bPair = TRUE;
- break;
- }
- iFindCount++;
- }
- else
- {
- iFindCount = 0;
- }
- }
-
- int iFindPos ;
-
- if(bPair == FALSE)
- {
- iFindPos = -1;
- }
- else
- {
- iFindPos = iCount + iBeginPos - iLenFind + 1;
- }
- return iFindPos;
- }
-
- DWORD CDirectory::GetDirectorySize(const TSTRING &strPath)
- {
- DWORD dSize = 0;
-
- TSTRING strDirPath = strPath;
-
- #ifdef _WIN32_WCE
- if(strDirPath[0] != TEXT('//'))
- {
- return FALSE;
- }
- #endif
-
- if(strDirPath[strDirPath.size() - 1] == TEXT('//'))
- {
- strDirPath += TEXT("*.*");
- }
- else
- {
- strDirPath += TEXT("//*.*");
- }
-
- WIN32_FIND_DATA fd;
- HANDLE hdFind;
- hdFind = FindFirstFile(strDirPath.c_str(),&fd);
- if(hdFind != INVALID_HANDLE_VALUE)
- {
- do{
-
- if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
- {
-
- TSTRING strNextDir = strPath;
- if(strNextDir[strNextDir.size() -1] != TEXT('//'))
- strNextDir += TEXT("//");
-
- strNextDir += fd.cFileName;
-
- DWORD dDirectorySize = GetDirectorySize(strNextDir);
- dSize += dDirectorySize;
- }
- else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- {
-
- TSTRING strPathFile = strPath;
- if(strPathFile[strPathFile.size() - 1] != TEXT('//'))
- strPathFile += TEXT("//");
-
- strPathFile += fd.cFileName;
-
- HANDLE hFile = CreateFile(strPathFile.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
- dSize += ::GetFileSize(hFile,NULL);
- CloseHandle(hFile);
- }
- }while(FindNextFile(hdFind,&fd));
- }
- FindClose(hdFind);
- return dSize;
- }
-
- BOOL CDirectory::CheckDirectoryExist(const TSTRING &strPath)
- {
- BOOL bReturn = FALSE;
- if(strPath.size() >= MAX_PATH)
- {
- return FALSE;
- }
- WIN32_FIND_DATA fd;
- HANDLE hdFind = FindFirstFile(strPath.c_str(),&fd);
- if(hdFind != INVALID_HANDLE_VALUE)
- {
- if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
- {
- bReturn = TRUE;
- }
- }
- FindClose(hdFind);
- return bReturn;
- }
-
- BOOL CDirectory::CopyDirectory(const TSTRING &strDestinationPath,const TSTRING &strSourcePath)
- {
- TSTRING strDirPath = strSourcePath;
- BOOL bResult = TRUE;
- #ifdef _WIN32_WCE
- if(strDirPath[0] != '//')
- {
- return FALSE;
- }
- #endif
- if(strDirPath[strDirPath.size() - 1] == '//')
- {
- strDirPath += TEXT("*.*");
- }
- else
- {
- strDirPath += TEXT("//*.*");
- }
-
- WIN32_FIND_DATA fd;
- HANDLE hdFind;
- hdFind = FindFirstFile(strDirPath.c_str(),&fd);
- if(hdFind != INVALID_HANDLE_VALUE)
- {
- do{
- if(!CheckDirectoryExist(strDestinationPath))
- {
- CreateDirectory(strDestinationPath.c_str(),NULL);
-
- }
-
-
- if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
- {
-
- TSTRING strSourceNextDir = strSourcePath;
- TSTRING strDestinationNextDir = strDestinationPath;
-
- if(strSourceNextDir[strSourceNextDir.size() -1] != TEXT('//'))
- {
- strSourceNextDir += TEXT("//");
- }
-
- if(strDestinationNextDir[strDestinationNextDir.size() -1] != TEXT('//'))
- {
- strDestinationNextDir += TEXT("//");
- }
-
- strSourceNextDir += fd.cFileName;
- strDestinationNextDir += fd.cFileName;
-
- CreateDirectory(strDestinationNextDir.c_str(),NULL);
-
- CopyDirectory(strDestinationNextDir,strSourceNextDir);
- }
- else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- {
-
- TSTRING strSourceFilePath = strSourcePath;
- TSTRING strDestinationFilePath = strDestinationPath;
-
- if(strSourceFilePath[strSourceFilePath.size() - 1] != TEXT('//'))
- {
- strSourceFilePath += TEXT("//");
- }
-
- if(strDestinationFilePath[strDestinationFilePath.size() - 1] != TEXT('//'))
- {
- strDestinationFilePath += TEXT("//");
- }
-
- strSourceFilePath += fd.cFileName;
- strDestinationFilePath += fd.cFileName;
-
-
- if(GetFileAttributes(strSourceFilePath.c_str()) & FILE_ATTRIBUTE_READONLY)
- SetFileAttributes(strSourceFilePath.c_str(), FILE_ATTRIBUTE_NORMAL);
-
- if(GetFileAttributes(strDestinationFilePath.c_str()) &FILE_ATTRIBUTE_READONLY)
- SetFileAttributes(strDestinationFilePath.c_str(), FILE_ATTRIBUTE_NORMAL);
-
- if(CopyFile(strSourceFilePath.c_str(),strDestinationFilePath.c_str(),FALSE)==FALSE)
- {
- #ifdef _WIN32_WCE
- RETAILMSG(TRUE,(TEXT("Copy File /" %s /" Failed ! Error Code : 0x%x/r/n"),strSourceFilePath.c_str(),GetLastError()));
- ASSERT(FALSE);
- #else
- TSTRING strInfo;
- strInfo = TEXT("Copy File/" ");
- strInfo += strSourceFilePath;
- strInfo += TEXT("/" Failed !");
- OutputDebugString(strInfo.c_str());
- #endif
- return FALSE;
- }
- }
- }while(FindNextFile(hdFind,&fd));
- }
- else
- {
- bResult = FALSE;
- }
- FindClose(hdFind);
- return bResult;
- }
-
- BOOL CDirectory::FindFileFromDirectory(const TSTRING &DirectoryPath,const TSTRING &SuffixName,std::vector<TSTRING> &vFilePathList)
- {
- if(DirectoryPath.size() >= MAX_PATH)
- {
- return FALSE;
- }
-
- TSTRING FindDirPath = DirectoryPath;
-
- if(FindDirPath[FindDirPath.size() - 1] == TEXT('//'))
- {
- FindDirPath += TEXT("*.*");
- }
- else
- {
- FindDirPath += TEXT("//*.*");
- }
-
- WIN32_FIND_DATA fd;
- HANDLE hdFind;
- hdFind = FindFirstFile(FindDirPath.c_str(),&fd);
- if(hdFind != INVALID_HANDLE_VALUE)
- {
- do{
-
- if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))
- {
-
- TSTRING strFindNextDir = FindDirPath;
- if(strFindNextDir[strFindNextDir.size() -1] != TEXT('//'))
- strFindNextDir += TEXT("//");
-
- strFindNextDir += fd.cFileName;
-
- FindFileFromDirectory(strFindNextDir,SuffixName,vFilePathList);
- }
- else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
- {
-
- TSTRING strFindPathFile = DirectoryPath;
- if(strFindPathFile[strFindPathFile.size() - 1] != TEXT('//'))
- strFindPathFile += TEXT("//");
-
- strFindPathFile += fd.cFileName;
- if(CheckFileSuffix(fd.cFileName,SuffixName) == TRUE)
- {
- vFilePathList.push_back(strFindPathFile);
- }
- }
- }while(FindNextFile(hdFind,&fd));
- }
- FindClose(hdFind);
-
- return TRUE;
- }
-
- BOOL CDirectory:: CheckFileSuffix(const TSTRING &FileName,const TSTRING &SuffixName)
- {
- if(SuffixName.empty())
- {
- return FALSE;
- }
-
- if(FileName.empty())
- {
- return FALSE;
- }
-
- if(SuffixName[0] != TEXT('*'))
- {
- return (FileName.compare(SuffixName) == 0) ? TRUE : FALSE;
- }
- else
- {
- if(FileName.rfind(&SuffixName[1]) != TSTRING::npos)
- return TRUE;
- }
- return FALSE;
- }
-
- #ifndef _WIN32_WCE
- void CDirectory::GetDiskPartition(std::multimap<FileDeviceType,std::wstring> &mDiskParition)
- {
- HANDLE hDevice;
- std::wstring strBaseDisk = TEXT("////.//");
-
- TCHAR BaseNumber = TEXT('A');
-
-
- for(int i = 0; i<26;i++)
- {
- std::wstring strDiskNumber;
- strDiskNumber += (BaseNumber + i);
- strDiskNumber += TEXT(':');
-
-
- std::wstring strDiskName = strBaseDisk;
- strDiskName +=strDiskNumber;
-
-
- hDevice = CreateFile(strDiskName.c_str(), GENERIC_READ,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,NULL);
-
- if(hDevice != INVALID_HANDLE_VALUE)
- {
- strDiskNumber += TEXT("//");
-
- if(DRIVE_CDROM == GetDriveType(strDiskNumber.c_str()))
- {
- mDiskParition.insert(make_pair(CDROM,strDiskNumber));
-
- }else if(DRIVE_REMOVABLE == GetDriveType(strDiskNumber.c_str()))
- {
-
- mDiskParition.insert(make_pair(DISK_REMOVABLE,strDiskNumber));
-
- }
- else if(DRIVE_FIXED == GetDriveType(strDiskNumber.c_str()))
- {
-
- mDiskParition.insert(make_pair(DISK_FIXED,strDiskNumber));
-
- }
-
- }
- CloseHandle(hDevice);
- }
- }
- #endif
|