打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VC文件目录常见操作

VC里文件操作有很多,本文在这里收录了一些常见的函数,仅供参考。

1. 判断一个目录是否存在

#include"windows.h"

参数: strPath: 目录的完整路径,注意不要以'\'结尾

返回值: 如果为目录,返回真,否则返回假

BOOLFolderExist(CString strPath)
{
WIN32_FIND_DATA wfd;
BOOL rValue = FALSE;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind!=INVALID_HANDLE_VALUE) &&
(wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))
{
rValue = TRUE;
}
FindClose(hFind);
return rValue;
}

2. 判断文件或目录是否存在

参数:文件或目录的完整名字(带路径),可以是文件名,也可以是目录名

返回值: 如果存在,返回真,否则返回假。

BOOL FileExist(CStringstrFileName)
{
CFileFind fFind;
return fFind.FindFile(strFileName);
}

3. 创建一个目录

BOOLCreateFolder(CString strPath)
{
SECURITY_ATTRIBUTES attrib;
/*

设置目录的常见属性

*/
return ::CreateDirectory(strPath, &attrib);
}

4. 文件大小:

参数: 文件名字, 注意,如果给的是目录(文件夹),该函数返回值不会递归计算目录下所有文件大小。所以该函数只适 用于文件大小的统计。

返回值: 文件大小。单位为Byte。

DWORDGetFileSize(CString filepath)
{
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
DWORD fileSize;
CString filename;
filename = filepath;
hFind = FindFirstFile(filename,&fileInfo);
if(hFind != INVALID_HANDLE_VALUE)
fileSize = fileInfo.nFileSizeLow;

FindClose(hFind);
return fileSize ;
}

5. 计算文件夹的大小

参数:文件夹的完整路径。该函数不使用与文件

返回值: 文件夹的大小,单位为byte。

int64GetFolderSize(CString strDirPath)

{

CString strFilePath;

int64 dwDirSize = 0;
strFilePath += strDirPath;
strFilePath += "\\*.*";
CFileFind finder;
BOOL bFind = finder.FindFile(strFilePath);
while (bFind)
{
bFind = finder.FindNextFile();
if (!finder.IsDots())
{
CString strTempPath = finder.GetFilePath();
if (!finder.IsDirectory())
{
dwDirSize += finder.GetLength();
}
else
{
dwDirSize += GetDirSize(strTempPath);
}
}
}
finder.Close();
return dwDirSize;

}

由于该函数涉及到递归调用,因此如果是超大大的文件夹,或者文件夹下的子文件夹特别多,

则很有可能造成堆栈溢出。本人测试过系统目录D和E,均没有发生溢出。因此在一般情况下

可以使用。由于返回值为int64,int64表示的磁盘空间是相当大的,也没有溢出的可能。

6. 列出某目录下的所有文件(不递归列出)

#include

#include

#include

#include

#include

#include

typedefstd::basic_string _tstring; //宽字符串

typedefstd::list<_tstring> _tslist; //字符串链表

/*

返回文件名的链表。

filepath 目录的完整路径,不带\\

filefilterlist 文件扩展名列表,可以是多种类型的组合,比如说.txt;.xls;.doc

isordered 是否对文件名排序

*/

_tslistSearchFile(LPCTSTR filepath, LPCTSTR filefilterlist = _T(".*" ), boolisordered = true)

{

assert(filepath !=NULL);

TCHAR buffer[MAX_PATH];

#if _MSC_VER > 1310

/* 1310 for MicrosoftVisual C++ .NET 2003. 1310 represents \version 13 and a 1.0 point release. TheVisual 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_DATAfinddata;

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 liststring, a example, file filter may be ".txt;.xls;.doc"

_tstring filterstring =filefilterlist;

_tstringfilename(finddata.cFileName);

_tstring::size_typeindex = 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作为引用参数传进去。这样就省了一次拷贝的开销。

voidSearchFile(_tslist& list, LPCTSTR filepath, LPCTSTR filefilterlist =_T(".*" ), bool isordered = true)

以上代码均通过visual studio 2008编译,测试运行。

 


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
判断目录是否存在
建立多级目录 的函数源代码
长短路径转换
[转载]VC中判断目录,文件是否存在,创建目录,求目录或
C++下遍历文件夹
C/C++中判断某一文件或目录是否存在
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服