#include "StdAfx.h"
|
#include "StrFileUtils.h"
|
#include "Guid.h"
|
#include "MBFile.h"
|
|
|
CStrFileUtils::CStrFileUtils(void)
|
{
|
}
|
|
|
CStrFileUtils::~CStrFileUtils(void)
|
{
|
|
}
|
|
CString CStrFileUtils::GetFileName(CString strFileFullName)
|
{
|
int nPos = strFileFullName.ReverseFind('\\'); // Îļþ·¾¶£¬ÒÔ'\'б¸Ü·Ö¸ôµÄ·¾¶
|
|
return strFileFullName.Right(strFileFullName.GetLength() - nPos - 1);
|
}
|
|
|
CString CStrFileUtils::GetPath(CString strFileFullName)
|
{
|
int nPos = strFileFullName.ReverseFind(_T('\\'));
|
CString strFileName = strFileFullName.Left(nPos+1); // »ñÈ¡ÎļþÃû
|
return strFileName;
|
}
|
|
CString CStrFileUtils::GetPath1(CString strFileFullName)
|
{
|
int nPos = strFileFullName.ReverseFind(_T('\\'));
|
CString strFileName = strFileFullName.Left(nPos); // »ñÈ¡ÎļþÃû
|
return strFileName;
|
}
|
|
|
CString CStrFileUtils::GetSimplePath(CString strFileFullName)
|
{
|
int nPos = strFileFullName.ReverseFind(_T('\\'));
|
CString strFileName = strFileFullName.Left(nPos); // »ñÈ¡ÎļþÃû
|
return strFileName;
|
}
|
|
CString CStrFileUtils::GetServerPath(CString strRootPath,CString strServerPath,CString strFilePathName)
|
{
|
CString strTmpSerPath1,strTmpSerPath2;
|
|
if(!IsDirExit( strFilePathName))
|
{
|
strTmpSerPath1 = GetSimplePath(strFilePathName);
|
|
}else{
|
strTmpSerPath1 = strFilePathName;
|
}
|
////////////////////////////////////////////
|
strTmpSerPath1.Replace(strRootPath,_T(""));
|
strTmpSerPath1.Replace(_T('\\'),_T('/'));
|
|
if( strTmpSerPath1.Find(_T('/')) == 0 )
|
{
|
if( strServerPath.IsEmpty() )
|
{
|
strTmpSerPath2 = strTmpSerPath1.Mid(1,strTmpSerPath1.GetLength());
|
}else{
|
strTmpSerPath2 = strServerPath + strTmpSerPath1;
|
}
|
}else
|
{
|
if( strServerPath.IsEmpty() )
|
{
|
strTmpSerPath2 = strTmpSerPath1;
|
} else
|
{
|
strTmpSerPath2 = strServerPath + _T("/")+ strTmpSerPath1;
|
}
|
}
|
return strTmpSerPath2;
|
}
|
|
CString CStrFileUtils::GetSimpleFileName(CString strFileFullName)
|
{
|
int nPos = strFileFullName.ReverseFind(_T('.'));
|
if( nPos== -1 )
|
{
|
nPos = strFileFullName.ReverseFind('\\');
|
return strFileFullName.Right(strFileFullName.GetLength()-nPos-1);
|
}
|
CString strFileName = strFileFullName.Left(nPos); // »ñÈ¡ÎļþÃû
|
nPos = strFileName.ReverseFind('\\');
|
strFileName = strFileName.Right(strFileName.GetLength()-nPos-1);
|
return strFileName;
|
}
|
|
CString CStrFileUtils::GetFileExtName(CString strFileFullName)
|
{
|
int nPos = strFileFullName.ReverseFind('.');
|
if( nPos == -1 )
|
return _T("");
|
CString strFileExtName = strFileFullName.Right(strFileFullName.GetLength() - nPos); // »ñÈ¡À©Õ¹Ãû
|
return strFileExtName;
|
}
|
|
CString CStrFileUtils::GetDir( CString strFileName )
|
{
|
if( IsDirExit(strFileName) )
|
return strFileName;
|
|
int nPos = strFileName.ReverseFind('\\');
|
strFileName = strFileName.Left(nPos );
|
|
return strFileName;
|
}
|
|
CString CStrFileUtils::GetRootDir( CString strFileName )
|
{
|
int nPos = strFileName.ReverseFind('\\');
|
if( -1 == nPos )
|
return strFileName;
|
strFileName = strFileName.Left(nPos );
|
|
return strFileName;
|
}
|
|
|
bool CStrFileUtils::DoRemoveDirectory(CString strDirName)
|
{
|
bool result;
|
HANDLE Handle;
|
WIN32_FIND_DATA fData;
|
CString strTemp;
|
DWORD errorcode;
|
|
Handle = FindFirstFile(strDirName + _T("\\*.*"), &fData);
|
|
if (Handle == INVALID_HANDLE_VALUE)
|
return FALSE;
|
|
do {
|
errorcode = GetLastError();
|
|
if ( fData.cFileName[0] == '.' )
|
continue;
|
|
if (fData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
|
if (strDirName[strDirName.GetLength()-1] != '\\'){
|
TCHAR chA[MAX_PATH];
|
CString strA = strDirName+'\\'+ fData.cFileName;
|
lstrcpy(chA, strA);
|
DoRemoveDirectory(chA);
|
}
|
else{
|
TCHAR chB[MAX_PATH];
|
CString strB = strDirName + fData.cFileName;
|
lstrcpy(chB, strB);
|
DoRemoveDirectory(chB);
|
}
|
|
strTemp = strDirName + _T("\\") + fData.cFileName;
|
|
SetFileAttributes(strTemp, ~FILE_ATTRIBUTE_READONLY);
|
if (!RemoveDirectory(strTemp))
|
result = FALSE;
|
else
|
result = TRUE;
|
}
|
else
|
{
|
strTemp = strDirName + _T("\\") + fData.cFileName;
|
BOOL bl = SetFileAttributes(strTemp, ~FILE_ATTRIBUTE_READONLY);
|
|
if (!DeleteFile(strTemp))
|
result = FALSE;
|
else
|
result = TRUE;
|
}
|
|
}while(FindNextFile(Handle,&fData));
|
|
errorcode = GetLastError();
|
|
if (errorcode == ERROR_NO_MORE_FILES)//¿ÕĿ¼
|
{
|
::RemoveDirectory(strDirName);
|
result = TRUE;
|
}
|
|
if (Handle)
|
FindClose(Handle);
|
return result;
|
}
|
|
// ÅжÏÎļþÊÇ·ñ´æÔÚ
|
bool CStrFileUtils::IsExist( CString strFile)
|
{
|
DWORD dwAttrib = GetFileAttributes(strFile);
|
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
|
}
|
|
bool CStrFileUtils::IsDirExit( CString strPath )
|
{
|
DWORD dwAttrib = GetFileAttributes(strPath);
|
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
|
}
|
|
|
void CStrFileUtils::CreateDir(CString strPath)
|
{
|
CString strWPath = strPath;
|
CString strTemp;
|
if(!PathFileExists(strPath))//Îļþ¼Ð²»´æÔÚÔò´´½¨
|
{
|
strPath.TrimLeft(strPath.Left(3));
|
int i = strPath.Find(_T("\\"));
|
if(i>0)
|
{
|
strTemp = strWPath.Left(3) + strPath.Left(i);
|
}
|
else
|
{
|
strTemp = strWPath;
|
}
|
strPath.TrimLeft(strPath.Left(i));
|
if(!PathFileExists(strTemp))
|
CreateDirectory(strTemp,NULL);
|
|
while(strPath.Find(_T("\\")) == 0)
|
{
|
strPath.TrimLeft(strPath.Left(1));
|
int j = strPath.Find(_T("\\"));
|
if(j > 0)
|
{
|
strTemp = strTemp + _T("\\") + strPath.Left(j);
|
strPath.TrimLeft(strPath.Left(j));
|
}
|
else
|
strTemp = strTemp + _T("\\") + strPath;
|
if(!PathFileExists(strTemp))
|
CreateDirectory(strTemp, NULL);
|
}
|
}
|
|
}
|
|
bool CStrFileUtils::CreateFile( CString strFileName )
|
{
|
// ´´½¨Îļþ¼Ð
|
CString strPath = GetPath(strFileName);
|
CreateDir(strPath);
|
|
HANDLE hHandle = ::CreateFile(strFileName,GENERIC_WRITE,FILE_SHARE_READ,NULL,
|
OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
|
if( hHandle )
|
{
|
CloseHandle(hHandle);
|
}
|
|
return true;
|
}
|
|
bool CStrFileUtils::CopyTmpIniFile(CString strTmpIniName,CString strIniName)
|
{
|
// ´´½¨Îļþ¼Ð
|
CString strPath = GetPath(strIniName);
|
CreateDir(strPath);
|
// ¿½±´
|
return CopyFile(strTmpIniName,strIniName,true);
|
}
|
|
bool CStrFileUtils::CopySqliteDb3(CString strSrcDb,CString strTargetDb)
|
{
|
// ´´½¨Îļþ¼Ð
|
CString strPath = GetPath(strTargetDb);
|
CreateDir(strPath);
|
// ¿½±´
|
return CopyFile(strSrcDb,strTargetDb,true);
|
}
|
|
|
|
CString CStrFileUtils::GetCurModuleDir()
|
{
|
TCHAR szCurrentDir[MAX_PATH] = { 0 };
|
GetModuleFileName(NULL, szCurrentDir, sizeof(szCurrentDir));
|
LPTSTR lpInsertPos = _tcsrchr(szCurrentDir, _T('\\'));
|
_tcscpy(lpInsertPos + 1, _T("\0"));
|
return szCurrentDir;
|
}
|
|
CString CStrFileUtils::GetLocalDt()
|
{
|
CString strFileTime;
|
SYSTEMTIME st;
|
CString strDate;
|
::GetLocalTime(&st);
|
strFileTime.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
|
return strFileTime;
|
}
|
|
__int64 CStrFileUtils::GetFileSize(CString strFileFullName)
|
{
|
CFileStatus fStatus;
|
|
if ( !CFile::GetStatus( strFileFullName, fStatus ) )
|
return false;
|
return fStatus.m_size;
|
|
|
/*WIN32_FIND_DATA fileInfo;
|
HANDLE hFind;
|
DWORD fileSize;
|
hFind = FindFirstFile(strFileFullName ,&fileInfo);
|
if(hFind != INVALID_HANDLE_VALUE) {
|
fileSize = fileInfo.nFileSizeLow;
|
|
}
|
return fileSize;*/
|
}
|
|
CString CStrFileUtils::GetClustersSize( CString &strPath )
|
{
|
CString strCluserters;
|
double m_dFree = 0;
|
int nIndex = strPath.Find(_T(":"));
|
CString firstchar = strPath.Mid(0,nIndex);
|
CString strRootPath=firstchar + _T(":\\");
|
/*CString strRootPath="E:\\123.txt";*///´ø¸ùĿ¼±ê¼ÇµÄ´ÅÅÌ·ûºÅ
|
DWORD dwSectorsPerCluster=0;//ÿ´ØÖÐÉÈÇøÊý
|
DWORD dwBytesPerSector=0;//ÿÉÈÇøÖÐ×Ö½ÚÊý
|
DWORD dwFreeClusters=0;//Ê£Óà´ØÊý
|
DWORD dwTotalClusters=0;//×Ü´ØÊý
|
if (GetDiskFreeSpace(strRootPath,&dwSectorsPerCluster,&dwBytesPerSector,
|
&dwFreeClusters,&dwTotalClusters))
|
{
|
//m_dwVolSize=dwTotalClusters*dwSectorsPerCluster*dwBytesPerSector;//²»ÄÜÕâÑù£¬·ñÔòÔ½½ç
|
double dd=dwSectorsPerCluster*dwBytesPerSector/(1024.*1024.);
|
dd=dd/1024.;
|
m_dFree=dwFreeClusters*dd;//¸Ã´ÅÅÌÊ£ÓàÈÝÁ¿×Ü´óС
|
strCluserters.Format(_T("%.2lf"),m_dFree);
|
if (m_dFree < 1)
|
{
|
|
}
|
}
|
return strCluserters;
|
}
|
|
bool CStrFileUtils::IsEmptyDir( CString &strPath )
|
{
|
CFileFind ff;
|
CString strFilePath;
|
BOOL bFind = ff.FindFile(strPath + _T("//*.*"));
|
|
while(bFind)
|
{
|
bFind = ff.FindNextFile();
|
if (ff.IsDots() || ff.IsSystem() || ff.IsHidden())
|
continue;
|
// ÊÇĿ¼
|
if (ff.IsDirectory())
|
{
|
strFilePath = ff.GetFilePath();
|
if(!IsEmptyDir(strFilePath)){
|
ff.Close();
|
return false;
|
}
|
}
|
else
|
{
|
strFilePath = ff.GetFilePath();
|
DWORD nFileSize = GetFileSize(strFilePath);
|
if( nFileSize != 0 ){
|
ff.Close();
|
return false;
|
}
|
}
|
}
|
ff.Close();
|
|
return true;
|
}
|
|
bool CStrFileUtils::HasFileOnNextLevel(CString &strPath)
|
{
|
CFileFind ff;
|
CString strFilePath;
|
BOOL bFind = ff.FindFile(strPath + _T("//*.*"));
|
|
while(bFind)
|
{
|
bFind = ff.FindNextFile();
|
if (ff.IsDots() || ff.IsSystem() || ff.IsHidden())
|
continue;
|
// ÊÇĿ¼
|
if (ff.IsDirectory())
|
{
|
ff.Close();
|
return true;
|
}
|
else
|
{
|
strFilePath = ff.GetFilePath();
|
DWORD nFileSize = GetFileSize(strFilePath);
|
if( nFileSize != 0 ){
|
ff.Close();
|
return true;
|
}
|
}
|
}
|
ff.Close();
|
|
return false;
|
}
|
|
CString CStrFileUtils::GetFileSizeDisplayStr(__int64 u64FileSize)
|
{
|
CString strFileSize;
|
if (u64FileSize > 1024*1024*1024)
|
strFileSize.Format(_T("%.2lf GB"), ((double)u64FileSize)/(1024*1024 * 1024));
|
else if (u64FileSize > 1024*1024)
|
strFileSize.Format(_T("%.2lf MB"), ((double)u64FileSize)/(1024*1024));
|
else if (u64FileSize > 1024)
|
strFileSize.Format(_T("%.2lf KB"), ((double)u64FileSize)/1024);
|
else
|
strFileSize.Format(_T("%d Bytes"), u64FileSize);
|
|
return strFileSize;
|
}
|
|
CString CStrFileUtils::GetFileFlowDisplayStr( CString strTime )
|
{
|
CString strYMD,strHMS;
|
int n = strTime.FindOneOf(_T(" "));
|
strYMD = strTime.Left(n);
|
strHMS = strTime.Right(strTime.GetLength() - n -1);
|
|
CString strCurYMD;
|
SYSTEMTIME st;
|
CString strDate;
|
GetLocalTime(&st);
|
strCurYMD.Format(_T("%04d-%02d-%02d"),st.wYear,st.wMonth,st.wDay);
|
if( strCurYMD == strYMD )
|
{
|
return strHMS;
|
}
|
return strYMD;
|
}
|
|
CString CStrFileUtils::GetRemainTimeDisplayStr( __int64 uRemainTime )
|
{
|
if( uRemainTime == 0 )
|
return _T("--");
|
CString strRemainTime;
|
__int64 days = 1000 * uRemainTime / (1000 * 60 * 60 * 24);
|
__int64 hours = (1000 * uRemainTime % (1000 * 60 * 60 * 24)) / (1000 *60 * 60);
|
__int64 minutes = (1000 * uRemainTime % ( 1000 * 60 * 60)) / (1000 * 60);
|
__int64 seconds = (1000 * uRemainTime % (1000 *60))/1000;
|
if( days == 0 )
|
{
|
strRemainTime.Format(_T("%02I64u:%02I64u:%02I64u"),hours,minutes,seconds );
|
}else{
|
strRemainTime.Format(_T("%02I64u:%02I64u:%02I64u:%02I64u"),days,hours,minutes,seconds );
|
}
|
return strRemainTime;
|
}
|
|
bool CStrFileUtils::HasSlashSplit(CString strSvrPath)
|
{
|
int nPos = strSvrPath.Find(_T('/'));
|
if( nPos >= 0 )
|
return true;
|
|
return false;
|
}
|
|
CString CStrFileUtils::GetLastErrorMessageString(DWORD dwLastError )
|
{
|
LPVOID lpMsgBuf = NULL;
|
FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
|
dwLastError,
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
(LPTSTR) &lpMsgBuf, 0, NULL );
|
|
CString str = (LPCTSTR)lpMsgBuf;
|
|
if( lpMsgBuf != NULL )
|
LocalFree(lpMsgBuf);
|
|
return str;
|
}
|
|
// ´ò¿ªÎļþ
|
bool CStrFileUtils::OpenFile(CString strFileName,CString &strErrInfo)
|
{
|
if( !IsExist( strFileName) )
|
{
|
strErrInfo = _T("Îļþ²»´æÔÚ");
|
return false;
|
}
|
// ´ò¿ªÎļþ
|
SHELLEXECUTEINFO ShExecInfo = {0};
|
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
|
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST;
|
ShExecInfo.hwnd = NULL;
|
ShExecInfo.lpVerb = NULL;
|
ShExecInfo.lpFile = strFileName;
|
ShExecInfo.lpParameters = _T("");
|
ShExecInfo.lpDirectory = NULL;
|
ShExecInfo.nShow = SW_SHOW;
|
ShExecInfo.hInstApp = NULL;
|
ShellExecuteEx(&ShExecInfo);
|
return true;
|
}
|
|
// Îļþ¼Ð
|
bool CStrFileUtils::OpenFolder(CString strPath,CString &strErrInfo)
|
{
|
CString strParam;
|
strParam.Format(_T("/select, %s"),strPath);
|
|
SHELLEXECUTEINFO ShExecInfo = {0};
|
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
|
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST;
|
ShExecInfo.hwnd = NULL;
|
ShExecInfo.lpVerb = NULL;
|
ShExecInfo.lpFile = _T("explorer.exe");
|
ShExecInfo.lpParameters = strParam;
|
ShExecInfo.lpDirectory = NULL;
|
ShExecInfo.nShow = SW_SHOW;
|
ShExecInfo.hInstApp = NULL;
|
ShellExecuteEx(&ShExecInfo);
|
return true;
|
}
|
|
// µÃµ½ÎļþÐÞ¸Äʱ¼ä
|
CString CStrFileUtils::GetFileMdyTime(CString strFileName)
|
{
|
CString strMdyTime;
|
CFileStatus status;
|
if( CFile::GetStatus( strFileName, status ) ) // static function
|
{
|
CTime create_time=status.m_ctime ;
|
CTime last_modified_time=status.m_mtime ;
|
strMdyTime = last_modified_time.Format("%Y-%m-%d %H:%M:%S");
|
|
}
|
return strMdyTime;
|
}
|
|
// дÎļþ
|
bool CStrFileUtils::WriteFile( CString strFileName,CString strContent,CString &strErrInfo )
|
{
|
CString strPath;
|
|
try{
|
strPath = GetSimplePath(strFileName);
|
|
CStrFileUtils::CreateDir(strPath);
|
|
WORD unicode = 0xFEFF;
|
CFile file;
|
file.Open( strFileName, CFile::modeCreate|CFile::modeWrite );
|
file.Write(&unicode,2);
|
file.Write(strContent, strContent.GetLength()*2);
|
file.Close();
|
|
}
|
catch( ... )
|
{
|
strErrInfo = strFileName + _T(" дʧ°Ü£¡");
|
return false;
|
}
|
return true;
|
}
|
|
|
// µÃµ½Ä¿Â¼Ãû³Æ
|
CString CStrFileUtils::GetDirName( CString strFileID )
|
{
|
strFileID.Replace(_T("{"),_T(""));
|
strFileID.Replace(_T("}"),_T(""));
|
|
CString strDir;
|
CGuid guid;
|
guid.FromString1(strFileID);
|
|
int nDir = (guid.m_guid.Data1 + guid.m_guid.Data2 + guid.m_guid.Data3) % 99;
|
strDir.Format(_T("%d"),nDir);
|
|
return strDir;
|
}
|
|
|
bool CStrFileUtils::GetStrFromFile( CString strFileName, CString &strBuf )
|
{
|
CFile file;
|
HLOCAL memBuffer;
|
CMBFile::e_FileType eFileType = CMBFile::eAutoFileType;
|
CMBFile::e_FileFormat eFileFormat = CMBFile::eAutoFileFormat;
|
DWORD dwNewSize,dwSize;
|
TCHAR *pBuffer = NULL;
|
|
TRY
|
{
|
if ( !file.Open( strFileName, CFile::shareDenyWrite | CFile::modeRead ) )
|
return false;
|
|
// µÃµ½ÎļþÀàÐÍ
|
CMBFile::GetFileType( &file,eFileType,dwSize);
|
file.SeekToBegin();
|
|
if( eFileType == CMBFile::eANSI )
|
{
|
// *********************¶ÁÈ¡Îļþ******************************************************************/
|
LONGLONG ansifilelen = file.GetLength();//»ñµÃansi±àÂëÎļþµÄÎļþ³¤¶È
|
char *p = new char[ansifilelen + 1];//newÒ»¿éÐÂÄÚ´æ
|
file.Read(p, ansifilelen);
|
p[ansifilelen] = '\0';
|
|
LONGLONG unicodefilelen = MultiByteToWideChar(CP_ACP, 0, p, strlen(p), NULL, 0);//°ÑansiÎļþ³¤¶ÈתΪUnicodeÎļþ³¤¶È
|
|
wchar_t *wp = new wchar_t[unicodefilelen + 1];//newÒ»¿éÐÂÄÚ´æ
|
MultiByteToWideChar(CP_ACP, 0, p, strlen(p), wp, unicodefilelen);//°Ñ¶Áµ½µÄÎļþת³É¿í×Ö·û
|
wp[unicodefilelen] = '\0';
|
|
delete[] p;//ÊÍ·ÅÄÚ´æ
|
file.Close();
|
strBuf = wp;
|
|
delete[] wp;
|
} else{
|
eFileType = CMBFile::eAutoFileType;
|
eFileFormat = CMBFile::eAutoFileFormat;
|
|
memBuffer = CMBFile::Load( &file, eFileType, eFileFormat, dwNewSize );
|
file.Close( );
|
|
pBuffer = static_cast<TCHAR*> (::LocalLock (memBuffer));
|
|
_tcscpy_s( strBuf.GetBuffer( dwNewSize ), dwNewSize, pBuffer );
|
strBuf.ReleaseBuffer( );
|
::LocalUnlock( memBuffer );
|
::LocalFree( memBuffer );
|
}
|
}
|
CATCH_ALL(e)
|
{
|
file.Close( );
|
THROW_LAST ();
|
return false;
|
}
|
END_CATCH_ALL
|
return true;
|
}
|