MFC第二課 文件類型使用技巧

1)文件/文件夾是否存在

添加頭文件:

#include <shlwapi.h>

#pragma comment(lib,"Shlwapi.lib")


PathFileExists(CString strFileName)


2)文件夾

創建文件夾:CreateDirectory()

刪除文件夾:ReMoveDirectory()


3)文件路徑的存儲問題

例如:test\\test.cpp文件

如果需要保存在一個CString類型或者

一個char數組,需要添加多一個\

如下:

char* pdbName = "test\\\test.cpp"

否則運行的查看顯示如下:

test\test.cpp


注意:實際上可以通過/,來避免上述問題的產生


修改文件名稱
CString strOldName= _T("D:\\old.txt"); 
CString strNewName= _T("D:\\new.txt"); 
CFile::Rename(strOldName,strNewName);


需求說明:文件夾中的文件一律替換成小寫

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void TransferName(CString strDirPath)   
{   
    CFileFind finder;
    CString path;
    path.Format(_T("%s/*.*"),strDirPath);
    BOOL bWorking = finder.FindFile(path);
    while(bWorking)
  {
        bWorking = finder.FindNextFile();
        if(finder.IsDirectory() && !finder.IsDots())
    {//處理文件夾
            TransferName(finder.GetFilePath()); //遞歸文件夾
        }
        else
    {//轉換文件名稱大小寫
      if(!finder.IsDots())
      {
        CString strOldName = finder.GetFilePath();
        CString strNewName = finder.GetFilePath();
        strNewName.MakeLower();
        CFile::Rename(strOldName,strNewName);
      }
  
        }
    }
}
 
調用過程:
    CString strDir = _T("d:\\log");
    TransferName(strDir);

其中的關鍵點:

1.

1
if(!finder.IsDots())

 表示當前的工作目錄


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章