WinCE下对文件夹的操作

来源:互联网 发布:java常用中间件 doubo 编辑:程序博客网 时间:2024/04/29 15:02

最近,由于工作的需要自己封装了一个对对文件夹的操作的类(CDirectory)。
有了这个类之后我们对文件操作就显得十分的简单,在这里就不废话了。首先来看一下怎么使用这个CDirectory。
CDirectory类实现了对目录的创建、删除、拷贝、取得文件夹的大小、检查目录的存在与否和从指定的文件夹中查找指定的后缀名。

 

[cpp] view plaincopy
  1. CDirector DirOperator;  
  2. //根据绝对路径创建文件夹,如果绝对路径名有不存在的目录将被创建。如在C盘下有个Dir的文件//夹,但是在Dir子目录文件夹中没有A文件夹通过下面的调用:  
  3. DirOperator. CreateDirectorySerial(TEXT("c://Dir//A//B"));  
  4. //将会为Dir创建一个A的子目录同时也会为为A创建一个B的子目录。  
  5. //获取指定文件夹的大小,将返回B文件夹的大小  
  6. DirOperator. GetDirectorySize(TEXT("c://Dir//A//B"));  
  7. //检查目录是否存在,如果B目录存在将返回TRUE,否则返回FALSE  
  8. if(CheckDirectoryExist(TEXT("c://Dir//A//B")))  
  9. {  
  10.     RETAILMSG(TRUE,(TEXT("Directory exist!/r/n")));  
  11. }  
  12. else  
  13. {  
  14.     RETAILMSG(TRUE,(TEXT("Directory does not exist!/r/n")));  
  15. }  
  16.   
  17. //拷贝文件夹,将C盘下的B目录下的所有文件拷贝到D下的C文件夹下,如果目标路径中有不存在//的文件夹将被创建  
  18. DirOperator. CopyDirectory (TEXT("D://Dir//A//C"),TEXT("c://Dir//A//B")):  
  19.   
  20. //在文件夹中查找文件, vFilePathList中存储了B文件夹下所有File.exe路径名  
  21. std::vector<TSTRING> vFilePathList  
  22. DirOperator.FindFileFromDirectory(TEXT("c://Dir//A//B"),TEXT("File.exe"),vFilePathList);  
  23. // vFilePathList中存储了B文件夹下所有扩展名为“.exe”路径名  
  24. DirOperator.FindFileFromDirectory(TEXT("c://Dir//A//B"),TEXT("*.exe"),vFilePathList);  
  25.   
  26. //删除文件夹,将删除文件下的所有文件  
  27. DirOperator. DeleteDirectorye(TEXT("c://Dir"));  

 

[cpp] view plaincopy
  1. #pragma once  
  2.   
  3. #include <windows.h>  
  4. #include <TChar.h>  
  5. #include <string>  
  6. #include <map>  
  7. #include <vector>  
  8. #include <algorithm>  
  9.   
  10.   
  11.   
  12. #ifdef UNICODE  
  13.     #ifndef TSTRING  
  14.         #define TSTRING std::wstring  
  15.     #endif  
  16. #else  
  17.     #ifndef TSTRING  
  18.         #define TSTRING std::string  
  19.     #endif  
  20. #endif  
  21.   
  22. #ifndef _WIN32_WCE  
  23.   
  24. enum FileDeviceType  // The type of disk partition    
  25. {   
  26.     DISK_FIXED,    
  27.     DISK_REMOVABLE,  
  28.     CDROM  
  29. };  
  30. #endif  
  31.   
  32. class CDirectory  
  33. {  
  34. public:  
  35.     //-------------------------------------------------------------------------------------  
  36.     //Description:  
  37.     //  Get directory's size  
  38.     //  
  39.     //Parameters:  
  40.     //  strPath: [in] The path of directory  
  41.     //-------------------------------------------------------------------------------------  
  42.     DWORD GetDirectorySize(const TSTRING &strPath);  
  43.   
  44.     //-------------------------------------------------------------------------------------  
  45.     //Description:  
  46.     //  Check that the directory exists  
  47.     //  
  48.     //Parameters:  
  49.     //  strPath: [in] The path of directory  
  50.     //-------------------------------------------------------------------------------------  
  51.     BOOL CheckDirectoryExist(const TSTRING &strPath);  
  52.   
  53.     //-------------------------------------------------------------------------------------  
  54.     //Description:  
  55.     //  Copy no empty directory to another directory  
  56.     //  
  57.     //Parameters:  
  58.     //  strDestinationPath: [in] The path of destination directory  
  59.     //  strSourcePath:      [in] The path of source directory  
  60.     //-------------------------------------------------------------------------------------  
  61.     BOOL CopyDirectory(const TSTRING &strDestinationPath,const TSTRING &strSourcePath);  
  62.   
  63.     //-------------------------------------------------------------------------------------  
  64.     //Description:  
  65.     //  Create the directory serial as to the path. For explame: If the path is "harddisk//test//temp",  
  66.     //it will create the directory: "harddisk","test",and "temp".  
  67.     //  
  68.     //Parameters:  
  69.     //  strPath[in]:   The path of the directory to be created  
  70.     //--------------------------------------------------------------------------------------  
  71.      void CreateDirectorySerial(const TSTRING &strPath);  
  72.   
  73.      //-------------------------------------------------------------------------------------  
  74.     //Description:  
  75.     //  Delete the no empty directory  
  76.     //  
  77.     //Parameters:  
  78.     //  strPath: [in] The path of directory  
  79.     //--------------------------------------------------------------------------------------  
  80.      void DeleteDirectory(const TSTRING &strPath);  
  81.   
  82.     //-------------------------------------------------------------------------------------  
  83.     //Description:  
  84.     //  Get the current runing path including '/' in last  
  85.     //  
  86.     //Parameters:  
  87.     //  pszPath: [out] Get the current path  
  88.     //  ulSize: [in] The size of the buffer. For example: szPath[MAX_NUMBER],the size if MAX_NUMBER.  
  89.     //  
  90.     //-------------------------------------------------------------------------------------  
  91.      TCHAR *GetCurrentDirectory(TCHAR *pszPath, ULONG ulSize);  
  92.   
  93.     //---------------------------------------------------------------------  
  94.     //Description:  
  95.     //  Find the file in the directory  
  96.     //  
  97.     //Parameters:  
  98.     //  DirectoryPath:[in] The path of directory.  
  99.     //  SuffixName:  [in] To find the name of the suffix .if SuffixName is like TEXT("file.exe"),  
  100.     //   will find the path of all "file.exe".  
  101.     //  if SuffixName is like TEXT("*.exe"), will find the path of all file's Suffix is TEXT("exe").  
  102.     //  m_vFilePathList: [Out] Storage the path of file which to be found  
  103.     //---------------------------------------------------------------------  
  104.     BOOL FindFileFromDirectory(const TSTRING &DirectoryPath,const TSTRING &SuffixName,std::vector<TSTRING> &vFilePathList);  
  105.   
  106. #ifndef _WIN32_WCE  
  107.     //---------------------------------------------------------------------  
  108.      //Description:  
  109.      // Get disk partition  
  110.      //  
  111.      //Parameters:  
  112.      //  mDiskParition: [Out] Store disk partition'type  
  113.      //  
  114.      //---------------------------------------------------------------------  
  115.      void GetDiskPartition(std::multimap<FileDeviceType,std::wstring> &mDiskParition);  
  116. #endif  
  117.   
  118. protected:  
  119.     //---------------------------------------------------------------------------  
  120.     //Description:  
  121.     //  Find the string in the source string ,and return the position.  
  122.     //  
  123.     //Parameter:  
  124.     //  szSource:[in]   
  125.     //      The source string  
  126.     //  szFind:[in]  
  127.     //      The find string  
  128.     //  iBeginPos:[in]  
  129.     //      The begin finding position  
  130.     //  
  131.     //Return Values:  
  132.     //  If it -1 , couldn't find the string.  
  133.     //  Others , it's the position of the first character of szFind in the source string  
  134.     //  
  135.     //---------------------------------------------------------------------------  
  136.      int FindString(const TCHAR *szSource, const TCHAR *szFind,const int iBeginPos);  
  137.   
  138.      //---------------------------------------------------------------------  
  139.      //Description:  
  140.      // Check the suffix of file name  
  141.      //  
  142.      //Parameters:  
  143.      //  FileName: [in] The file name to check  
  144.      //  SuffixName: [in] Special suffix name  
  145.      //  
  146.      //Return Value:  
  147.      // TRUE -- The file is suitable  
  148.      // FALSE -- The file is not suitable  
  149.      //---------------------------------------------------------------------  
  150.      BOOL CheckFileSuffix(const TSTRING &FileName,const TSTRING &SuffixName);  
  151.   
  152.        
  153. public:  
  154.     CDirectory();  
  155.     virtual ~CDirectory();  
  156. };  

 

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include "Directory.h"  
  3.   
  4.   
  5. CDirectory::CDirectory()  
  6. {  
  7.   
  8. }  
  9. CDirectory::~CDirectory()  
  10. {  
  11. }  
  12.   
  13. TCHAR *CDirectory::GetCurrentDirectory(TCHAR *pszPath, ULONG ulSize)  
  14. {  
  15.     memset(pszPath, 0, sizeof(TCHAR) * ulSize);  
  16.   
  17.     TCHAR szBuf[MAX_PATH] = {0};  
  18.     GetModuleFileName(NULL,szBuf,sizeof(szBuf)/sizeof(TCHAR));  
  19.   
  20.     int ulCount = _tcslen(szBuf);  
  21.   
  22.     while(--ulCount >= 0)  
  23.     {  
  24.         if(szBuf[ulCount] == TEXT('//'))  
  25.         {  
  26.             break;  
  27.         }  
  28.         else  
  29.         {  
  30.             continue;  
  31.         }  
  32.     }  
  33.   
  34.     if(ulSize > (DWORD)ulCount)  
  35.     {  
  36.         _tcsncpy(pszPath,szBuf,(ulCount + 1));  
  37.     }  
  38.   
  39.     return pszPath;  
  40. }  
  41.   
  42. void CDirectory::CreateDirectorySerial(const TSTRING &strPath)  
  43. {  
  44.   
  45.     TSTRING SourcePath = strPath;  
  46.   
  47.     if(strPath.size() >= MAX_PATH)  
  48.         return;  
  49.   
  50.     if(SourcePath[SourcePath.size() -1 ] == TEXT('//'))  
  51.     {  
  52.         SourcePath[SourcePath.size() -1 ] = TEXT('/0');  
  53.     }  
  54.   
  55.     if(CheckDirectoryExist(strPath))  
  56.         return;  
  57.   
  58.     TSTRING::size_type idx = SourcePath.find(TEXT('//'));  
  59.   
  60.     while(idx !=TSTRING::npos)  
  61.     {  
  62.         TSTRING strCreateDirPath;  
  63.         strCreateDirPath.assign(SourcePath,0,idx);  
  64.         if(!CheckDirectoryExist(strCreateDirPath))  
  65.         {  
  66.             CreateDirectory(strCreateDirPath.c_str(),NULL);  
  67.         }  
  68.   
  69.         idx = SourcePath.find(TEXT('//'),idx + 1);  
  70.     }  
  71.   
  72.     CreateDirectory(SourcePath.c_str(),NULL);  
  73. }  
  74.   
  75. void CDirectory::DeleteDirectory(const TSTRING &strPath)  
  76. {  
  77.     TSTRING strDirPath = strPath;  
  78. #ifdef _WIN32_WCE  
  79.     if(strDirPath[0] != TEXT('//'))  
  80.     {  
  81.         return;  
  82.     }  
  83. #endif  
  84.     if(strDirPath[strDirPath.size() - 1] == TEXT('//'))  
  85.     {  
  86.         strDirPath += TEXT("*.*");  
  87.     }  
  88.     else  
  89.     {  
  90.         strDirPath += TEXT("//*.*");  
  91.     }  
  92.   
  93.     WIN32_FIND_DATA fd;  
  94.     HANDLE hdFind;  
  95.     hdFind = FindFirstFile(strDirPath.c_str(),&fd);  
  96.     if(hdFind != INVALID_HANDLE_VALUE)  
  97.     {  
  98.         do{  
  99.             //At winXP system the "." means current directory, the ".."means parent directory.  
  100.             if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))  
  101.             {         
  102.                 //It is directory     
  103.                 TSTRING strNextDir = strPath;  
  104.                 if(strNextDir[strNextDir.size() -1] != TEXT('//'))  
  105.                     strNextDir += TEXT("//");  
  106.   
  107.                 strNextDir += fd.cFileName;   
  108.                 DeleteDirectory(strNextDir);  
  109.                 RemoveDirectory(strNextDir.c_str());  
  110.             }  
  111.             else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)  
  112.             {     
  113.                 //It is file   
  114.                 TSTRING strPathFile = strPath;  
  115.                 if(strPathFile[strPathFile.size() - 1] !=TEXT( '//'))  
  116.                     strPathFile += TEXT("//");  
  117.   
  118.                 strPathFile += fd.cFileName;  
  119.                 DeleteFile(strPathFile.c_str());  
  120.             }  
  121.         }while(FindNextFile(hdFind,&fd));  
  122.     }  
  123.     FindClose(hdFind);  
  124.   
  125.     RemoveDirectory(strPath.c_str());  
  126. }  
  127.   
  128. int CDirectory::FindString(const TCHAR *szSource, const TCHAR *szFind, const int iBeginPos)  
  129. {  
  130.     int iLenSource = _tcslen(szSource);  
  131.     int iLenFind = _tcslen(szFind);  
  132.   
  133.     if(iLenSource - 1 < iBeginPos)  
  134.     {  
  135.         return -1;  
  136.     }  
  137.   
  138.     int iCount = 0;  
  139.     int iFindCount = 0;  
  140.     BOOL bPair = FALSE;  
  141.     for(iCount = 0; iCount < iLenSource - iBeginPos; iCount++)  
  142.     {  
  143.         if(szSource[iCount + iBeginPos] == szFind[iFindCount])  
  144.         {  
  145.             if(iFindCount == iLenFind - 1)  
  146.             {  
  147.                 bPair = TRUE;  
  148.                 break;  
  149.             }  
  150.             iFindCount++;             
  151.         }  
  152.         else  
  153.         {  
  154.             iFindCount = 0;  
  155.         }  
  156.     }  
  157.   
  158.     int iFindPos ;  
  159.   
  160.     if(bPair == FALSE)  
  161.     {  
  162.         iFindPos = -1;  
  163.     }  
  164.     else  
  165.     {  
  166.         iFindPos = iCount + iBeginPos - iLenFind + 1;  
  167.     }  
  168.     return iFindPos;  
  169. }  
  170.   
  171. DWORD CDirectory::GetDirectorySize(const TSTRING &strPath)  
  172. {  
  173.     DWORD dSize = 0;  
  174.     //查找路径下的文件夹和所需文件  
  175.     TSTRING strDirPath = strPath;  
  176.   
  177. #ifdef _WIN32_WCE  
  178.     if(strDirPath[0] != TEXT('//'))  
  179.     {  
  180.         return FALSE;  
  181.     }  
  182. #endif  
  183.   
  184.     if(strDirPath[strDirPath.size() - 1] == TEXT('//'))  
  185.     {  
  186.         strDirPath += TEXT("*.*");  
  187.     }  
  188.     else  
  189.     {  
  190.         strDirPath += TEXT("//*.*");  
  191.     }  
  192.   
  193.     WIN32_FIND_DATA fd;  
  194.     HANDLE hdFind;  
  195.     hdFind = FindFirstFile(strDirPath.c_str(),&fd);  
  196.     if(hdFind != INVALID_HANDLE_VALUE)  
  197.     {  
  198.         do{  
  199.             //At winXP system the "." means current directory, the ".."means parent directory.  
  200.             if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))  
  201.             {             
  202.                 //It is directory     
  203.                 TSTRING strNextDir = strPath;  
  204.                 if(strNextDir[strNextDir.size() -1] != TEXT('//'))  
  205.                     strNextDir += TEXT("//");  
  206.   
  207.                 strNextDir += fd.cFileName;  
  208.   
  209.                 DWORD dDirectorySize = GetDirectorySize(strNextDir);  
  210.                 dSize += dDirectorySize;          
  211.             }  
  212.             else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)  
  213.             {     
  214.                 //It is file   
  215.                 TSTRING strPathFile = strPath;  
  216.                 if(strPathFile[strPathFile.size() - 1] != TEXT('//'))  
  217.                     strPathFile += TEXT("//");  
  218.   
  219.                 strPathFile += fd.cFileName;  
  220.   
  221.                 HANDLE hFile = CreateFile(strPathFile.c_str(),GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);  
  222.                 dSize += ::GetFileSize(hFile,NULL);  
  223.                 CloseHandle(hFile);  
  224.             }  
  225.         }while(FindNextFile(hdFind,&fd));  
  226.     }  
  227.     FindClose(hdFind);  
  228.     return dSize;  
  229. }  
  230.   
  231. BOOL CDirectory::CheckDirectoryExist(const TSTRING &strPath)  
  232. {  
  233.     BOOL bReturn = FALSE;  
  234.     if(strPath.size() >= MAX_PATH)  
  235.     {  
  236.         return FALSE;  
  237.     }  
  238.     WIN32_FIND_DATA fd;  
  239.     HANDLE hdFind = FindFirstFile(strPath.c_str(),&fd);  
  240.     if(hdFind != INVALID_HANDLE_VALUE)  
  241.     {  
  242.         if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)  
  243.         {  
  244.             bReturn = TRUE;  
  245.         }  
  246.     }  
  247.     FindClose(hdFind);  
  248.     return bReturn;  
  249. }  
  250.   
  251. BOOL CDirectory::CopyDirectory(const TSTRING &strDestinationPath,const TSTRING &strSourcePath)   
  252. {  
  253.     TSTRING strDirPath = strSourcePath;  
  254.     BOOL bResult = TRUE;  
  255. #ifdef _WIN32_WCE  
  256.     if(strDirPath[0] != '//')  
  257.     {  
  258.         return FALSE;  
  259.     }  
  260. #endif  
  261.     if(strDirPath[strDirPath.size() - 1] == '//')  
  262.     {  
  263.         strDirPath += TEXT("*.*");  
  264.     }  
  265.     else  
  266.     {  
  267.         strDirPath += TEXT("//*.*");  
  268.     }  
  269.   
  270.     WIN32_FIND_DATA fd;  
  271.     HANDLE hdFind;  
  272.     hdFind = FindFirstFile(strDirPath.c_str(),&fd);  
  273.     if(hdFind != INVALID_HANDLE_VALUE)  
  274.     {  
  275.         do{  
  276.             if(!CheckDirectoryExist(strDestinationPath))  
  277.             {  
  278.                 CreateDirectory(strDestinationPath.c_str(),NULL);  
  279.   
  280.             }  
  281.   
  282.             //At winXP system the "." means current directory, the ".."means parent directory.  
  283.             if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))  
  284.             {         
  285.                 //It is directory     
  286.                 TSTRING strSourceNextDir = strSourcePath;  
  287.                 TSTRING strDestinationNextDir = strDestinationPath;  
  288.   
  289.                 if(strSourceNextDir[strSourceNextDir.size() -1] != TEXT('//'))  
  290.                 {  
  291.                     strSourceNextDir += TEXT("//");  
  292.                 }  
  293.   
  294.                 if(strDestinationNextDir[strDestinationNextDir.size() -1] != TEXT('//'))  
  295.                 {  
  296.                     strDestinationNextDir += TEXT("//");  
  297.                 }  
  298.   
  299.                 strSourceNextDir += fd.cFileName;  
  300.                 strDestinationNextDir += fd.cFileName;  
  301.   
  302.                 CreateDirectory(strDestinationNextDir.c_str(),NULL);  
  303.   
  304.                 CopyDirectory(strDestinationNextDir,strSourceNextDir);  
  305.             }  
  306.             else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)  
  307.             {     
  308.                 //It is file   
  309.                 TSTRING strSourceFilePath = strSourcePath;  
  310.                 TSTRING strDestinationFilePath = strDestinationPath;  
  311.   
  312.                 if(strSourceFilePath[strSourceFilePath.size() - 1] != TEXT('//'))  
  313.                 {  
  314.                     strSourceFilePath += TEXT("//");  
  315.                 }  
  316.   
  317.                 if(strDestinationFilePath[strDestinationFilePath.size() - 1] != TEXT('//'))  
  318.                 {  
  319.                     strDestinationFilePath += TEXT("//");  
  320.                 }  
  321.   
  322.                 strSourceFilePath += fd.cFileName;  
  323.                 strDestinationFilePath += fd.cFileName;  
  324.   
  325.                 //The file is read-only ,cancle the attributes of read-only  
  326.                 if(GetFileAttributes(strSourceFilePath.c_str()) & FILE_ATTRIBUTE_READONLY)   
  327.                     SetFileAttributes(strSourceFilePath.c_str(), FILE_ATTRIBUTE_NORMAL);   
  328.   
  329.                 if(GetFileAttributes(strDestinationFilePath.c_str()) &FILE_ATTRIBUTE_READONLY)   
  330.                     SetFileAttributes(strDestinationFilePath.c_str(), FILE_ATTRIBUTE_NORMAL);  
  331.   
  332.                 if(CopyFile(strSourceFilePath.c_str(),strDestinationFilePath.c_str(),FALSE)==FALSE)  
  333.                 {     
  334. #ifdef _WIN32_WCE  
  335.                     RETAILMSG(TRUE,(TEXT("Copy File /" %s /"  Failed ! Error Code : 0x%x/r/n"),strSourceFilePath.c_str(),GetLastError()));  
  336.                     ASSERT(FALSE);  
  337. #else   
  338.                     TSTRING strInfo;  
  339.                     strInfo = TEXT("Copy File/" ");  
  340.                     strInfo += strSourceFilePath;  
  341.                     strInfo += TEXT("/"  Failed !");  
  342.                     OutputDebugString(strInfo.c_str());  
  343. #endif  
  344.                     return FALSE;  
  345.                 }  
  346.             }  
  347.         }while(FindNextFile(hdFind,&fd));  
  348.     }  
  349.     else  
  350.     {  
  351.         bResult = FALSE;  
  352.     }  
  353.     FindClose(hdFind);  
  354.     return bResult;  
  355. }  
  356.   
  357. BOOL CDirectory::FindFileFromDirectory(const TSTRING &DirectoryPath,const TSTRING &SuffixName,std::vector<TSTRING> &vFilePathList)  
  358. {  
  359.     if(DirectoryPath.size() >= MAX_PATH)  
  360.     {  
  361.         return FALSE;  
  362.     }  
  363.   
  364.     TSTRING FindDirPath = DirectoryPath;  
  365.   
  366.     if(FindDirPath[FindDirPath.size() - 1] == TEXT('//'))  
  367.     {  
  368.         FindDirPath += TEXT("*.*");  
  369.     }  
  370.     else  
  371.     {  
  372.         FindDirPath += TEXT("//*.*");  
  373.     }  
  374.   
  375.     WIN32_FIND_DATA fd;  
  376.     HANDLE hdFind;  
  377.     hdFind = FindFirstFile(FindDirPath.c_str(),&fd);  
  378.     if(hdFind != INVALID_HANDLE_VALUE)  
  379.     {  
  380.         do{  
  381.             //At winXP system the "." means current directory, the ".."means parent directory.  
  382.             if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY && fd.cFileName[0] != TEXT('.'))  
  383.             {         
  384.                 //it must be directory    
  385.                 TSTRING strFindNextDir = FindDirPath;  
  386.                 if(strFindNextDir[strFindNextDir.size() -1] != TEXT('//'))  
  387.                     strFindNextDir += TEXT("//");  
  388.   
  389.                 strFindNextDir += fd.cFileName;  
  390.   
  391.                 FindFileFromDirectory(strFindNextDir,SuffixName,vFilePathList);  
  392.             }  
  393.             else if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)  
  394.             {     
  395.                 //it is file   
  396.                 TSTRING strFindPathFile = DirectoryPath;  
  397.                 if(strFindPathFile[strFindPathFile.size() - 1] != TEXT('//'))  
  398.                     strFindPathFile += TEXT("//");  
  399.   
  400.                 strFindPathFile += fd.cFileName;  
  401.                 if(CheckFileSuffix(fd.cFileName,SuffixName) == TRUE)  
  402.                 {  
  403.                     vFilePathList.push_back(strFindPathFile);  
  404.                 }  
  405.             }  
  406.         }while(FindNextFile(hdFind,&fd));  
  407.     }  
  408.     FindClose(hdFind);  
  409.   
  410.     return TRUE;  
  411. }  
  412.   
  413. BOOL CDirectory:: CheckFileSuffix(const TSTRING &FileName,const TSTRING &SuffixName)  
  414. {  
  415.     if(SuffixName.empty())  
  416.     {  
  417.         return FALSE;  
  418.     }  
  419.   
  420.     if(FileName.empty())  
  421.     {  
  422.         return FALSE;  
  423.     }  
  424.   
  425.     if(SuffixName[0] != TEXT('*'))  
  426.     {     
  427.         return (FileName.compare(SuffixName) == 0) ? TRUE : FALSE;  
  428.     }  
  429.     else  
  430.     {  
  431.         if(FileName.rfind(&SuffixName[1]) != TSTRING::npos)  
  432.             return TRUE;  
  433.     }  
  434.     return FALSE;  
  435. }  
  436.   
  437. #ifndef _WIN32_WCE  
  438. void CDirectory::GetDiskPartition(std::multimap<FileDeviceType,std::wstring> &mDiskParition)  
  439. {  
  440.     HANDLE   hDevice;       
  441.     std::wstring strBaseDisk = TEXT("////.//");  
  442.   
  443.     TCHAR BaseNumber = TEXT('A');  
  444.   
  445.     //The Disk partition from'A' to 'Z'  
  446.     for(int i = 0; i<26;i++)  
  447.     {  
  448.         std::wstring strDiskNumber;  
  449.         strDiskNumber += (BaseNumber + i);  
  450.         strDiskNumber += TEXT(':');  
  451.   
  452.   
  453.         std::wstring strDiskName = strBaseDisk;  
  454.         strDiskName +=strDiskNumber;  
  455.   
  456.   
  457.         hDevice =  CreateFile(strDiskName.c_str(), GENERIC_READ,     
  458.             FILE_SHARE_READ | FILE_SHARE_WRITE,     
  459.             NULL, OPEN_EXISTING,     
  460.             FILE_ATTRIBUTE_NORMAL,NULL);     
  461.   
  462.         if(hDevice != INVALID_HANDLE_VALUE)    
  463.         {  
  464.             strDiskNumber += TEXT("//");  
  465.   
  466.             if(DRIVE_CDROM == GetDriveType(strDiskNumber.c_str()))  
  467.             {  
  468.                 mDiskParition.insert(make_pair(CDROM,strDiskNumber));  
  469.   
  470.             }else if(DRIVE_REMOVABLE == GetDriveType(strDiskNumber.c_str()))  
  471.             {  
  472.   
  473.                 mDiskParition.insert(make_pair(DISK_REMOVABLE,strDiskNumber));  
  474.   
  475.             }  
  476.             else if(DRIVE_FIXED == GetDriveType(strDiskNumber.c_str()))  
  477.             {  
  478.   
  479.                 mDiskParition.insert(make_pair(DISK_FIXED,strDiskNumber));  
  480.   
  481.             }  
  482.   
  483.         }  
  484.         CloseHandle(hDevice);  
  485.     }  
  486. }  
  487. #endif  

 

0 0
原创粉丝点击