判断文件是否存在/返回文件大小
std::string rawDataDir = CSystemHelper::GetRawDataDir();
boost::filesystem::path pathRawDataDir(rawDataDir);
bool ret = boost::filesystem::exists(pathRawDataDir);
bool CSystemHelper::IsPathExist(const std::string& sPath)
{
try
{
boost::filesystem::path path(sPath);
return boost::filesystem::exists(path);
}
catch (...)
{
return false;
}
}
std::string CSystemHelper::GetParentPath(const std::string& fileFullPath)
{
try
{
boost::filesystem::path fullPath = fileFullPath;
return fullPath.parent_path().string();
}
catch (...)
{
return "";
}
}
uint64_t CSystemHelper::GetFileSize(const std::string& fileFullPath)
{
using namespace boost::filesystem;
uintmax_t size = 0u;
boost::filesystem::path pathSource(fileFullPath);
if (exists(pathSource))
{
boost::system::error_code error;
try
{
size = file_size(pathSource, error);
}
catch(...)
{
}
}
return static_cast<uint64_t>(size);
}
(1) 创建文件夹
// 0:failed 1:succeed 2:existed
int CSystemHelper::CreateDir(const string& dirPath)
{
using namespace boost::filesystem;
if (is_directory(dirPath))
return 2;
try
{
bool b = create_directories(path(dirPath));
if(b)
return 1;
}
catch (...)
{
}
return 0;
}
(2)获取路径下的所有文件
void CSystemHelper::GetAllRawDataFileList(std::vector<std::string>& fileList,const std::string& dir)
{
using namespace boost::filesystem;
if (is_directory(dir))
{
directory_iterator end;
directory_iterator pos(dir);
for ( pos; pos != end; ++pos)
{
if (!is_directory(*pos) && (extension(*pos) == ".raw"))
fileList.push_back(pos->path().filename().string());
}
}
}
(3)删除文件
bool CSystemHelper::DeleteFile(const std::string& fullPath)
{
using namespace boost::filesystem;
bool suc = false;
path ptest = fullPath;
if(exists(fullPath))
{
try
{
suc = remove(ptest);
}
catch(...)
{
suc = false;
}
}
return suc;
}
(4)删除整个文件夹
bool CSystemHelper::DeleteDir(const std::string& dir)
{
using namespace boost::filesystem;
bool suc = false;
path ptest = dir;
if (exists(ptest))
{
try
{
if(boost::filesystem::is_empty(ptest))
suc = remove(ptest);
else
{
uintmax_t num = remove_all(ptest);
suc = (num != 0);
}
}
catch(...)
{
suc = false;
}
}
return suc;
}
(5)复制文件
bool CSystemHelper::CopyTo(const std::string& sourfullpath, const std::string& targfullPath, bool overwrite)
{
using namespace boost::filesystem;
path targ = targfullPath;
path sour = sourfullpath;
bool suc = false;
if (exists(sourfullpath) && ((!exists(targfullPath) || overwrite)))
{
try
{
copy_file(sour, targ,copy_option::overwrite_if_exists);
suc = true;
}
catch(...)
{
//
}
}
return suc;
}
复制文件夹
bool CSystemHelper::CopyDir(const std::string& sourDir, const std::string& destDir )
{
using namespace boost::filesystem;
boost::filesystem::path PathSource(sourDir) ;
boost::filesystem::path PathTarget(destDir) ;
if( !exists(PathSource) )
return false;
if( !exists(PathTarget) )
create_directories(PathTarget);
directory_iterator end;
bool suc = true;
for( directory_iterator it(PathSource); it != end; ++it )
{
path newFrom = PathSource;
newFrom /= it->path().filename().string();
path newTo = PathTarget;
newTo /= it->path().filename().string();
if( is_directory( newFrom ) )
suc = suc && CopyDir(newFrom.string(), newTo.string() );
else
{
try
{
copy_file(newFrom, newTo,copy_option::overwrite_if_exists);
}
catch(...)
{
suc = suc && false;
}
}
}
return suc;
}
查找文件名相同后缀不同的所有文件
//查找文件名相同后缀不同的所有文件
//dir:要查询的目录
//fileName:查询的文件名
//allFiles: 返回查询结果,带后缀文件名
void CSystemHelper::find_files_with_name(const string& dir, const string& fileName, vector<string>& allFiles)
{
try
{
if (!(exists(dir)) || !is_directory(dir))
{
return;
}
recursive_directory_iterator end;
for (recursive_directory_iterator pos(dir) ;pos != end ; ++pos) //递归目录下的文件
{
string fileNameWithExpand = pos->path().filename().string();
string subFileName = fileNameWithExpand.substr(0u, fileName.length());
if (subFileName == fileName)
{
allFiles.push_back(fileNameWithExpand);
}
}
}
catch (...)
{
}
}