⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filenamemisc.cpp

📁 封装文件名的有关操作
💻 CPP
字号:
// FileNameMisc.cpp: implementation of the CFileNameMisc class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FileNameMisc.h"

#define LAST_WRITE   1
#define LAST_ACCESS  2
#define CREATE_TIME  3

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define SplitPath(sFile, sDrive, sFolder, sTitle, sExt) _splitpath(sFile, sDrive, sFolder, sTitle, sExt)

CFileNameMisc::CFileNameMisc(LPCSTR szFileName)
{ m_szFileName = szFileName; }

LPCSTR CFileNameMisc::GetFileName()
{
	CString szFileName;

	SplitPath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);

	szFileName = m_szFname;
	szFileName = szFileName + m_szExt;

	return szFileName;
}

LPCSTR CFileNameMisc::GetExtension()
{
	SplitPath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);

	return strlwr(m_szExt);
}

LPCSTR CFileNameMisc::GetFileSize()
{
	CFileFind cFind;

	cFind.FindFile(m_szFileName);
	cFind.FindNextFile();

	ostrstream oBeam;

	oBeam << cFind.GetLength() << ends;

	cFind.Close();

	return oBeam.str();
}

LPCSTR CFileNameMisc::GetFilePath()
{
	CString szFileName;

	SplitPath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);
	
	szFileName = m_szDrive;
	szFileName = szFileName + m_szDir;

	return szFileName;
}

LPCSTR CFileNameMisc::GetFileTitle()
{
	SplitPath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);

	return m_szFname;
}

LPCSTR CFileNameMisc::operator = (LPCSTR szFileName)
{ 
	m_szFileName = szFileName; 
	return m_szFileName; 
}


bool CFileNameMisc::operator == (LPCSTR szFileName)
{ 
	if (szFileName == m_szFileName) 
	{ return true; } else { return false; }
}

bool CFileNameMisc::operator != (LPCSTR szFileName)
{
	if (szFileName != m_szFileName) 
	{ return true; } else { return false; }
}

bool CFileNameMisc::IsFolder()
{
	CString m_Trim = m_szFileName;

	m_Trim.Trim();

	CString gTitle = GetFileTitle();

	if (gTitle == "" || gTitle == "." || gTitle == "..") 
	{ return 0; }

	if (m_Trim.Right(1) != "\\")  
	{ m_Trim.Insert(strlen(m_Trim), "\\"); }

	if (strlen(m_Trim) <= 4) { return 0; }

	int gAttrib = GetFileAttributes(m_Trim);

	if (gAttrib == 16 || gAttrib == 17 || gAttrib == 18) { return 1; } 
	if (gAttrib >= FILE_ATTRIBUTE_DIRECTORY) { return 1; } 

	return 0;
}

bool CFileNameMisc::IsDrive()
{
	if (strlen(m_szFileName) <= 4) 
	{ return 1; } else { return 0; }
}

bool CFileNameMisc::IsZIP() 
{
	CString gExt = GetExtension();
		
	if (gExt.Find(".zip") != -1)
	{ return 1; } else { return 0; }
}
bool CFileNameMisc::IsACE() 
{
	CString gExt = GetExtension();
		
	if (gExt.Find(".ace") != -1)
	{ return 1; } else { if (gExt.Find(".a01") != -1)
	{ return 1; } else { if (gExt.Find(".a00") != -1)
	{ return 1; } else { return 0; } } }
}

bool CFileNameMisc::IsRAR()
{
	CString gExt = GetExtension();
		
	if (gExt.Find(".rar") != -1)
	{ return 1; } else { if (gExt.Find(".r01") != -1)
	{ return 1; } else { if (gExt.Find(".r00") != -1)
	{ return 1; } else { return 0; } } }
}

bool CFileNameMisc::IsCAB()
{
	CString gExt = GetExtension();
		
	if (gExt.Find(".cab") != -1)
	{ return 1; } else { return 0; }
}

bool CFileNameMisc::IsArchive()
{
	CString gExt = GetExtension();
		
	if (gExt == ".cab" || gExt == ".zip" || gExt == ".rar" ||
		gExt == ".ace" || gExt == ".r01" || gExt == ".r00" ||
		gExt == ".a00" || gExt == ".a01")
	{
		return 1;
	}

	return 0;
}

LPCSTR CFileNameMisc::GetFileTime(int iTime, LPCSTR lpFormat)
{
	if (iTime != LAST_WRITE && iTime != LAST_ACCESS && iTime != CREATE_TIME)
							{ iTime = CREATE_TIME; }
	if (lpFormat == NULL)	{ lpFormat = "%B %d, %Y"; }

	CFileFind cFind;

	cFind.FindFile(m_szFileName);
	cFind.FindNextFile();

	CTime fTime;

	if (iTime == LAST_WRITE)  { cFind.GetLastWriteTime(fTime); }
	if (iTime == LAST_ACCESS) { cFind.GetLastAccessTime(fTime); }
	if (iTime == CREATE_TIME) { cFind.GetCreationTime(fTime); }
	
	cFind.Close();

	return fTime.Format(lpFormat);

}

BOOL CFileNameMisc::DoesExist()
{
	WIN32_FIND_DATA fd;

	CString	szFindPath = m_szFileName;

	int nSlash = szFindPath.ReverseFind('\\');

	if( nSlash == szFindPath.GetLength()-1)
	{
		szFindPath = szFindPath.Left(nSlash);
	}

	HANDLE hFind = FindFirstFile( szFindPath, &fd );

	if ( hFind != INVALID_HANDLE_VALUE )
	{
		FindClose( hFind );
	}

	return hFind != INVALID_HANDLE_VALUE;
}

LPCSTR CFileNameMisc::GetFileDrive()
{
	SplitPath(m_szFileName, m_szDrive, m_szDir, m_szFname, m_szExt);

	return m_szDrive;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -