头文件
ubase-fs.h:
#pragma once
//Windows Api
#include <windows.h>
//C Standard Library
#include <tchar.h>
//C++ Standard Library
#include <string>
#include <vector>
typedef std::basic_string<TCHAR> tstring;
bool FsCreateDirectory(const tstring& Dir);
bool FsIsFile(const tstring& Path);
bool FsIsDirectory(const tstring& Path);
bool FsEnumDirectory(const tstring& Dir, std::vector<tstring>& Files);
bool FsEnumDirDirectory(const tstring& Dir, std::vector<tstring>& Dirs);
源文件
ubase-fs.cpp:
#include "ubase-fs.h"
tstring StrPathToDir(const tstring& path)
{
try {
if (path.empty())
return _T("");
int offset = (int)tstring::npos;
wchar_t end = path[path.size() - 1];
if (end == _T('\\') || end == _T('/'))
offset = (int)path.size() - 2;
return path.substr(0, path.find_last_of(_T("\\/"), offset));
}
catch (...) {
return _T("");
}
}
bool FsCreateDirectory(const tstring& dir)
{
if (FsIsDirectory(dir))
return true;
if (FsIsFile(dir))
return false;
if (CreateDirectory(dir.c_str(),NULL))
return true;
bool result = true;
std::vector<tstring> subdirs;
tstring sub = dir;
tstring str;
do {
str = sub;
subdirs.push_back(sub);
sub = StrPathToDir(str);
} while (str.compare(sub) != 0);
int k = 0;
for (int i=(int)subdirs.size()-1; i>=0; i--) {
if (FsIsDirectory(subdirs[i]) || FsIsFile(subdirs[i])) {
k = i;
continue;
}
if (!CreateDirectory(subdirs[i].c_str(),NULL)) {
//fail rollback
for (int j=i; j<k; j++)
RemoveDirectory(subdirs[j].c_str());
result = false;
break;
}
}
return result;
}
bool FsEnumDirectory(const tstring& Dir, std::vector<tstring>& Files)
{
if (!FsIsDirectory(Dir))
return false;
bool result = false;
WIN32_FIND_DATA fd;
tstring Path = Dir + _T("\\*.*");
HANDLE hFind = FindFirstFile(Path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (tstring(fd.cFileName).compare(_T(".")) == 0
|| tstring(fd.cFileName).compare(_T("..")) == 0)
continue;
else if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
Files.push_back(Dir + _T("\\") + fd.cFileName);
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
result = true;
}
return result;
}
bool FsEnumDirDirectory(const tstring& Dir, std::vector<tstring>& Dirs)
{
if (!FsIsDirectory(Dir))
return false;
bool result = false;
WIN32_FIND_DATA fd;
tstring Path = Dir + _T("\\*.*");
HANDLE hFind = FindFirstFile(Path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (tstring(fd.cFileName).compare(_T(".")) == 0
|| tstring(fd.cFileName).compare(_T("..")) == 0)
continue;
else if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
Dirs.push_back(Dir + _T("\\") + fd.cFileName);
}
} while (FindNextFile(hFind, &fd));
FindClose(hFind);
result = true;
}
return result;
}
bool FsIsDirectory(const tstring& path)
{
DWORD attrs = GetFileAttributes(path.c_str());
if (attrs != INVALID_FILE_ATTRIBUTES) {
if (attrs & FILE_ATTRIBUTE_DIRECTORY)
return true;
}
return false;
}
bool FsIsFile(const tstring& path)
{
DWORD attrs = GetFileAttributes(path.c_str());
if (attrs != INVALID_FILE_ATTRIBUTES) {
if (!(attrs & FILE_ATTRIBUTE_DIRECTORY))
return true;
}
return false;
}
测试代码:
#include "ubase-fs.h"
int main()
{
tstring Dir = _T("d:\\test");
FsCreateDirectory(Dir);
if (FsIsDirectory(Dir))
{
printf("目录创建成功\r\n");
}
tstring FilePath = Dir + _T("\\") _T("test.dll");
HANDLE hFile = CreateFile(FilePath.c_str(), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
::CloseHandle(hFile);
}
if (FsIsFile(FilePath))
{
printf("文件创建成功\r\n");
}
std::vector<tstring> Files;
FsEnumDirectory(Dir, Files);
for (auto iter = Files.begin(); iter != Files.end(); iter++)
{
printf("枚举目录下面文件:%S\r\n", iter->c_str());
}
std::vector<tstring> Dirs;
FsEnumDirDirectory(_T("d:\\"), Dirs);
for (auto iter = Dirs.begin(); iter != Dirs.end(); iter++)
{
printf("枚举目录下面文件夹:%S\r\n", iter->c_str());
}
system("pause");
return 0;
}