C++ 文件目录相关操作 代码封装

头文件

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值