1. 判斷乙個目錄是否存在
#include "windows.h"
引數: strpath: 目錄的完整路徑,注意不要以'/'結尾
返回值: 如果為目錄,返回真,否則返回假
bool folderexist(cstring strpath)
findclose(hfind);
return rvalue;
}2. 判斷檔案或目錄是否存在
引數:檔案或目錄的完整名字(帶路徑),可以是檔名,也可以是目錄名
返回值: 如果存在,返回真,否則返回假。
bool fileexist(cstring strfilename)
3. 建立乙個目錄
bool createfolder(cstring strpath)
4. 檔案大小:
引數: 檔案名字, 注意,如果給的是目錄(資料夾),該函式返回值不會遞迴計算目錄下所有檔案大小。所以該函式只適 用於檔案大小的統計。
返回值: 檔案大小。單位為byte。
dword getfilesize(cstring filepath)
5. 計算資料夾的大小
引數:資料夾的完整路徑。該函式不使用與檔案
返回值: 資料夾的大小,單位為byte。
int64 getfoldersize(cstring strdirpath)
cstring strfilepath;
int64 dwdirsize = 0;
strfilepath += strdirpath;
strfilepath += "//*.*";
cfilefind finder;
bool bfind = finder.findfile(strfilepath);
while (bfind)
else}}
finder.close();
return dwdirsize;
由於該函式涉及到遞迴呼叫,因此如果是超大大的資料夾,或者資料夾下的子資料夾特別多,
則很有可能造成堆疊溢位。本人測試過系統目錄d和e,均沒有發生溢位。因此在一般情況下
可以使用。由於返回值為int64,int64表示的磁碟空間是相當大的,也沒有溢位的可能。
6. 列出某目錄下的所有檔案(不遞迴列出)
#include
#include
#include
#include
#include
#include
typedef std::basic_string_tstring; //寬字串
typedef std::list<_tstring> _tslist; //字串鍊錶
返回檔名的鍊錶。
filepath 目錄的完整路徑,不帶//
filefilterlist 副檔名列表,可以是多種型別的組合,比如說.txt;.xls;.doc
isordered 是否對檔名排序
_tslist searchfile(lpctstr filepath, lpctstr filefilterlist = _t(".*" ), bool isordered = true)
assert(filepath != null);
tchar buffer[max_path];
#if _msc_ver > 1310
/* 1310 for microsoft visual c++ .net 2003. 1310 represents /version 13 and a 1.0 point release. the visual c++ 2005 compiler version is 1400, the number.
_tcscpy_s(buffer, filepath); //_tcscpy_s is a micro for strcpy_s and strwcpy_s
#else
_tcscpy(buffer,filepath); //
#endif
_tslist filenamelist; // initial length is 100
win32_find_data finddata;
handle searchhandle = invalid_handle_value;
size_t length= _tcslen(filepath);
if (buffer[length-1] != _t('//'))
_tcscat_s(buffer,_t("//*")); // 向字串結尾新增/*, 用來查詢所有檔案
if ( (searchhandle = ::findfirstfile(buffer, &finddata)) != invalid_handle_value )
while (::findnextfile(searchhandle, &finddata) != 0)
if ( !(finddata.dwfileattributes & file_attribute_directory) ) // 為檔案
if ( !_tcsicmp(filefilterlist, _t(".*"))) // 將所有檔案輸出到鍊錶
filenamelist.push_back(finddata.cfilename);
else
//get file filter list string, a example, file filter may be ".txt;.xls;.doc"
_tstring filterstring = filefilterlist;
_tstring filename(finddata.cfilename);
_tstring::size_type index = filename.find_last_of(_t('.'));
if (index == _tstring::npos) // 檔案沒有副檔名,跳過
continue;
else
_tstring extname = filename.substr(index+1); //取得檔案的副檔名
_tstring::size_type exist;
exist = filterstring.find(extname);
if (exist != _tstring::npos) //判斷檔案的副檔名是否在副檔名列表裡
filenamelist.push_back(finddata.cfilename);
::findclose( searchhandle );
if (isordered) //如果要求排序,對鍊錶進行排序
filenamelist.sort(); //list的排序採用的一般是merge sort
return filenamelist;
}測試**:
lpctstr s = _t("c://temp");
lpctstr s1 = _t(".txt; .xls");
_tslist filename = searchfile(s,s1);
copy( filename.begin(),
filename.end(),
ostream_iterator<_tstring, _tstring::value_type >(wcout, _t("/n") )
由於函式返回的是list,因此有筆不菲的拷貝開銷。我也不確定rvo(返回值)是否會被執行,所以如果list很大很大的話,這確實是很糟糕的。解決方法是把list作為引用引數傳進去。這樣就省了一次拷貝的開銷。
void searchfile(_tslist& list, lpctstr filepath, lpctstr filefilterlist = _t(".*" ), bool isordered = true)
以上**均通過visual studio 2008編譯,測試執行。
常見檔案 目錄 路徑操作函式
bool winapi deletefile in lpctstr lpfilename 刪除乙個檔案 int shfileoperation lpshfileopstruct lpfileop 刪除很多檔案 bool winapi removedirectory in lpctstr lppath...
路徑 檔案 目錄 I O常見操作
public static bool isfilesequal string filename1,string filename2 問題5 如何獲取指定目錄的基本資訊 解決方案 可以使用directoryinfo類的相關屬性和方法 directoryinfo.exists 獲取指定目錄是否存在 di...
VC 檔案操作
1 檔案的查詢 當對乙個檔案操作時,如果不知道該檔案是否存在,就要首先進行查詢。mfc中有乙個專門用來進行檔案查詢的類 cfilefind 使用它可以方便快捷地進行檔案的查詢。下面這段 演示了這個類的最基本使用方法。cstring strfiletitle cfilefind finder bool...