文件夹操作

int CCorreDataMain::IsDirectoryFind(CString szPath)//文件夹是否存在 0存在 -1为空,-2为非法路径,-3为盘符异常,-4需要创建目录
{
if (szPath == "") return -1;//为空
if (szPath.Right(1)=='//') szPath.TrimRight('//');//去掉
if (szPath.GetLength()<2)
return -2;//非法路径
else if (szPath.GetLength()==2)
{
szPath.MakeUpper();
TCHAR aa = szPath.GetAt(0);
if ((aa>='A')&&(aa<='Z'))
{
DWORD dwDrivers = GetLogicalDrives();
DWORD dwMask = 1<<(aa-'A');
if (dwMask & dwDrivers)
{
return 0;//盘符存在;
}else
return -3;//盘符异常
}
else
return -3;//盘符异常
}else
{
CFileFind ff;
BOOL bExist = ff.FindFile(szPath);
if (!bExist)//目录不存在
{
return -4;//需要创建新目录
}
}
return 0;
}
BOOL CCorreDataMain::CheckPath(CString &szPath,BOOL bCanCreateDir,BOOL bDirectory)//检查路径是否有问题
{
if (bDirectory)//如果路径是目录的话
{
switch(IsDirectoryFind(szPath))
{
case -1:
AfxMessageBox(_T("目录不能为空!"));
return FALSE;
break;
case -2:
AfxMessageBox(_T("非法路径!"));
return FALSE;
break;
case -3:
AfxMessageBox(_T("盘符异常!"));
return FALSE;
break;
case -4:
if (szPath.Right(1)=='//') szPath.TrimRight('//');
if (bCanCreateDir)//如果目录不存在是否自动创建?
{
if (!CreateDirectory(szPath,NULL))
{
AfxMessageBox(_T("文件夹创建失败!"));
return FALSE;
}
}
break;
}
if (szPath.Right(1)=='//') szPath.TrimRight('//');
}else
{//路径是文件的话
CString szTmp = szPath;
int nLen = szTmp.GetLength();
szTmp = szTmp.Left(szTmp.ReverseFind('//') +1);
if (!CheckPath(szTmp,bCanCreateDir,TRUE))
{
//路径有问题
AfxMessageBox(szTmp+_T("路径有问题!"));
return FALSE;
}
CFileFind ff;
if (!ff.FindFile(szPath))
{ //文件不存在
ff.Close();
return FALSE;
}
}
return TRUE;
}


BOOL CCorreDataMain::CheckFileSize(CString szFile,LONG lLen)
{
CFile tmpfile;
if (!tmpfile.Open(szFile,CFile::typeBinary|CFile::modeRead))
{
return FALSE;
}
LONG lLength;
lLength = tmpfile.GetLength();
tmpfile.Close();
if (lLength!=lLen) return FALSE;
return TRUE;
}
原文地址:https://www.cnblogs.com/carl2380/p/2317356.html