積累的VC編程小技巧之文件操作
1.刪除文件夾
// 刪除文件夾及其所有內容
void CBaseDoc::RemoveFolder(const CString &strPathName)
{
CString path = strPathName;
if (path.Right(1) != _T("\\"))
path += _T("\\");
path += _T("*.*");
CFileFind ff;
BOOL res = ff.FindFile(path);
while (res)
{
res = ff.FindNextFile();
// 是文件時直接刪除
if (!ff.IsDots() && !ff.IsDirectory())
DeleteFile(ff.GetFilePath());
else if (ff.IsDots())
continue;
else if (ff.IsDirectory())
{
path = ff.GetFilePath();
// 是目錄時繼續遞歸,刪除該目錄下的文件
RemoveFolder(path);
::RemoveDirectory(path);
}
}
}
2.遍歷整個目錄查找文件
在應用程序的開發過程中,會遇到如何查找某一文件以確定此文件路徑的問題。利用CFileFind類可以比較方便地在當前目錄下進行文件查找,但卻不能對其子目錄中的文件進行搜尋。而實際應用中往往需要對某一整個目錄樹,甚至是整個C盤或D盤驅動器進行文件搜尋。通過實踐,我們在Visual C++ 6.0中編程實現了如何遍歷任意目錄樹,以查找某一特定的文件。
在下面的具體陳述中可以看到,在確定要查找的文件名和要進行搜索的目錄的名稱后,將調用函數Search_Directory進行文件的查找。首先依次查找當前目錄下的每一個實體(文件或是子目錄),如果是某一子目錄,則進入該子目錄并遞歸調用函數Search_Dirctory進行查找,查找完畢之后, 再返回上一級目錄;如果不是子目錄而是某一文件,則判斷其是否就是我們要查找的文件,如果是則輸出其完整的文件路徑。這樣,通過Search_Directory函數的反復遞歸調用,就可以實現對整個目錄,包括子目錄的遍歷搜索。下面將舉例詳細講述如何在VC++中編程實現在整個目錄樹中的文件查找。
1. 在Visual C++ 6.0(VC++ 5.0與之類似)中用默認方式創建了一基于對話框的應用程序Search。在主窗口對話框上放置一命令按鈕,其Caption為“Search File”,ID為ID_BUTTON_SEARCH。單擊此按鈕將完成文件的查找工作。
2. 利用ClassWizard為“Search File”按鈕的BN_CLICKED 事件添加處理函數OnButtonSearch,代碼如下:
#include 〈direct.h〉
#include 〈io.h〉
void CSearchDlg::OnButtonSearch()
{
// TODO: Add your control notification handler code here
char szFilename[80];
// 字符串 szFilename 表示要查找的文件名
strcpy(szFilename,"Mytext.txt");
_chdir("d:\\"); // 進入要查找的路徑(也可為某一具體的目錄)
// 查找文件, 如果查到則顯示文件的路徑全名
Search_Directory(szFilename);
// 為CSearchDlg類的一成員函數
MessageBox(″查找文件完畢!″);
// 顯示查找完畢的信息
}
3. 在CSearchDlg類中增加成員函數Search_Directory,它將完成具體的文件查找工作,代碼如下:
void CSearchDlg::Search_Directory(char* szFilename)
{
long handle;
struct _finddata_t filestruct;
//表示文件(或目錄)的信息
char path_search[_MAX_PATH];
//表示查找到的路徑結果
// 開始查找工作, 找到當前目錄下的第一個實體(文件或子目錄),
// "*"表示查找任何的文件或子目錄, filestruct為查找結果
handle = _findfirst("*", &filestruct);
// 如果handle為-1, 表示當前目錄為空, 則結束查找而返回
if((handle == -1)) return;
// 檢查找到的第一個實體是否是一個目錄(filestruct.name為其名稱)
if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
// 如果是目錄, 則進入該目錄并遞歸調用函數Search_Dirctory進行查找,
// 注意: 如果目錄名的首字符為'.'(即為"."或".."), 則不用進行查找
if( filestruct.name[0] != '.' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
// 查找完畢之后, 返回上一級目錄
_chdir("..");
}
}
else // 如果第一個實體不是目錄, 則檢查是否是要查找的文件
{
// stricmp對兩字符串進行小寫形式的對比, 返回為0表示完全一致
if( !stricmp(filestruct.name, szFilename) )
{
// 先獲得當前工作目錄的全路徑
_getcwd(path_search,_MAX_PATH);
// 再獲得文件的完整的路徑名(包含文件的名稱)
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search); //輸出顯示
}
}
// 繼續對當前目錄中的下一個子目錄或文件進行與上面同樣的查找
while(!(_findnext(handle,&filestruct)))
{
if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '.')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
// 最后結束整個查找工作
}
這樣我們就可以對整個目錄進行遍歷搜索,查找某一特定的文件,并輸出顯示其完整的文件路徑。以上的程序在Visual C++ 6.0中已調試通過。
3.創建多級目錄
BOOL mkdirEx(const char* lpPath)
{
CString pathname = lpPath;
if(pathname.Right(1) != "\")
pathname += "\" ;
int end = pathname.ReverseFind('\');
int pt = pathname.Find('\');
if (pathname[pt-1] == ':')
pt = pathname.Find('\', pt+1);
CString path;
while(pt != -1 && pt<=end)
{
path = pathname.Left(pt+1);
if(_access(path, 0) == -1)
_mkdir(path);
pt = pathname.Find('\', pt+1);
}
return true;
}
4.創建包含多個子目錄的目錄
void CreateAllDirectories(CString strDir)
{
//remove ending / if exists
if(strDir.Right(1)=="\\")
strDir=strDir.Left(strDir.GetLength()-1);
// base case . . .if directory exists
if(GetFileAttributes(strDir)!=-1)
return;
// recursive call, one less directory
int nFound = strDir.ReverseFind('\\');
CreateAllDirectories(strDir.Left(nFound));
// actual work
CreateDirectory(strDir,NULL);
}
RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成