RTFolder.cpp
上传用户:qhonly
上传日期:2013-06-10
资源大小:487k
文件大小:3k
源码类别:

界面编程

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "RTFolder.h"
  3. CRTFolder::CRTFolder(void)
  4. {
  5. }
  6. CRTFolder::~CRTFolder(void)
  7. {
  8. }
  9. BOOL CRTFolder::MakeDirs(LPCTSTR lpPathName)
  10. {
  11. if(CreateDirectory(lpPathName,NULL))
  12. return TRUE;
  13. char buf[MAX_PATH];
  14. GetFilePath(lpPathName,buf);
  15. if(!MakeDirs(buf))
  16. return FALSE;
  17. if(CreateDirectory(lpPathName,NULL))
  18. return TRUE;
  19. else
  20. return FALSE;
  21. }
  22. void CRTFolder::GetFilePath(LPCTSTR lpFileName, LPSTR lpPathBuf)
  23. {
  24. ZeroMemory(lpPathBuf,MAX_PATH);
  25. strcpy(lpPathBuf,lpFileName);
  26. int nLen = (int)strlen(lpFileName) - 1;
  27. while(nLen >= 0)
  28. {
  29. if(lpPathBuf[nLen] == '\')
  30. {
  31. lpPathBuf[nLen] = NULL;
  32. break;
  33. }
  34. nLen --;
  35. }
  36. }
  37. BOOL CRTFolder::MoveTo(LPCTSTR lpSrcDir, LPCTSTR lpDestDir)
  38. {
  39. char SrcPathName[MAX_PATH];
  40. if(!GetFileName(lpSrcDir,SrcPathName))return FALSE;
  41. char DestFolder[MAX_PATH];
  42. ZeroMemory(DestFolder,MAX_PATH);
  43. strcpy(DestFolder,lpDestDir);
  44. strcat(DestFolder,"\");
  45. strcat(DestFolder,SrcPathName);
  46. return MoveFile(lpSrcDir,DestFolder);
  47. }
  48. BOOL CRTFolder::DeleteFolder(LPCTSTR lpFolder)
  49. {
  50. WIN32_FIND_DATA wfd;
  51. char FolderFind[MAX_PATH];
  52. ZeroMemory(FolderFind,MAX_PATH);
  53. strcpy(FolderFind,lpFolder);
  54. strcat(FolderFind,"\*.*");
  55. HANDLE hFind = FindFirstFile(FolderFind,&wfd);
  56. if(hFind != NULL)
  57. {
  58. do
  59. {
  60. if((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  61. {
  62. if(wfd.cFileName[0] == '.')continue;
  63. char NextFolder[MAX_PATH];
  64. ZeroMemory(NextFolder,MAX_PATH);
  65. strcpy(NextFolder,lpFolder);
  66. strcat(NextFolder,"\");
  67. strcat(NextFolder,wfd.cFileName);
  68. DeleteFolder(NextFolder);
  69. }
  70. else
  71. {
  72. char NextFile[MAX_PATH];
  73. ZeroMemory(NextFile,MAX_PATH);
  74. strcpy(NextFile,lpFolder);
  75. strcat(NextFile,"\");
  76. strcat(NextFile,wfd.cFileName);
  77. DeleteFile(NextFile);
  78. }
  79. }while(FindNextFile(hFind,&wfd));
  80. FindClose(hFind);
  81. }
  82. return RemoveDirectory(lpFolder);
  83. }
  84. BOOL CRTFolder::GetFileName(LPCTSTR lpFileName, char lpBuf[])
  85. {
  86. char *ptr = NULL;
  87. int nLen = (int)strlen(lpFileName) - 1;
  88. if(lpFileName[nLen] == '\')nLen --;
  89. while(nLen > 0)
  90. {
  91. if(lpFileName[nLen] == '\')
  92. {
  93. ptr = (char*)(lpFileName + nLen + 1); 
  94. break;
  95. }
  96. nLen --;
  97. }
  98. if(nLen == 0)return FALSE;
  99. strcpy(lpBuf,ptr);
  100. return TRUE;
  101. }
  102. BOOL CRTFolder::ReName(LPCTSTR lpOldPath, LPCTSTR NewPath)
  103. {
  104. char BasePath[MAX_PATH];
  105. ZeroMemory(BasePath,MAX_PATH);
  106. GetFilePath(lpOldPath,BasePath);
  107. strcat(BasePath,"\");
  108. strcat(BasePath,NewPath);
  109. if(0 == rename(lpOldPath,BasePath))
  110. return TRUE;
  111. else
  112. return FALSE;
  113. }