📄 globle.cpp
字号:
// Globle.cpp
#include "stdAfx.h"
#include "Globle.h"
#include <direct.h>
// Delete a file (include ReadOnly File)
// Parameter: strFullFileName is the full file name
void DeleteReadOnlyFile(CString strFullFileName)
{
CFile fDeleteFile;
CFileStatus fStatus;
// 获得文件状态
fDeleteFile.GetStatus(strFullFileName, fStatus);
if(fStatus.m_attribute & CFile::readOnly) // 如果是只读文件
{
//取消只读属性
fStatus.m_attribute &= ~CFile::readOnly;
fDeleteFile.SetStatus(strFullFileName, fStatus);
}
fDeleteFile.Remove(strFullFileName); //删除文件
}
// Remove a no empty directory
// Parameter: strPath is the full path name
void RemoveDirectory(CString strPath)
{
CFileFind FindFile;
CFile fDeleteFile;
CString strFileName;
CString strNewPath = strPath + "\\*.*";
BOOL bContinue = FindFile.FindFile(strNewPath);
// 如果目录非空,进入循环,每次循环删除一个目录或文件
// 循环结束,目录变成空目录
while (bContinue == TRUE)
{
bContinue = FindFile.FindNextFile();
if(FindFile.IsDots()) //如果是.或..
continue;
//如果是目录,递归调用RemoveDirectory()
if(FindFile.IsDirectory())
{
strNewPath = FindFile.GetFilePath();
RemoveDirectory(strNewPath);
}
//如果是文件,则删除该文件
else
{
strFileName = FindFile.GetFilePath();
DeleteReadOnlyFile(strFileName);
}
}
_rmdir(strPath); //删除目录
FindFile.Close();
}
BOOL FileCopy(CString SouceFileName,CString TargetFileName)
{
CFile SouceFile;
CFile TargetFile;
char pBuf[8192];
int iBlockSize;
if(!SouceFile.Open(SouceFileName,CFile::modeRead))
return false ;
if(!TargetFile.Open(TargetFileName,CFile::modeWrite | CFile::modeCreate))
{
SouceFile.Close();
return false;
}
while(iBlockSize=SouceFile.Read(pBuf,8192))
{
TargetFile.Write(pBuf,iBlockSize);
}
SouceFile.Close();
TargetFile.Close();
return true;
}
// Copy all contents in strSourcePath to strTargetPath.
void CopyDirectory(CString strTargetPath, CString strSourcePath)
{
_mkdir(strTargetPath); // 建立目标子目录
CFileFind FindFile;
CString strNewSorcePath, strNewTargetpath, strSourceFileName, strTargetFileName;
CString strNewPath = strSourcePath + "\\*.*";
BOOL bContinue = FindFile.FindFile(strNewPath);
//如果源目录非空,进入循环
while (bContinue == TRUE)
{
bContinue = FindFile.FindNextFile();
if(FindFile.IsDots())
continue;
//如果是子目录,递归调用函数CopyDirectory()
if(FindFile.IsDirectory())
{
strNewSorcePath = FindFile.GetFilePath();
strNewTargetpath = strTargetPath + "\\" + FindFile.GetFileName();
CopyDirectory(strNewTargetpath, strNewSorcePath);
}
//如果是文件,复制该文件到目标目录
else
{
strSourceFileName = FindFile.GetFilePath();
strTargetFileName = strTargetPath + "\\" + FindFile.GetFileName();
FileCopy(strSourceFileName,strTargetFileName);
}
}
FindFile.Close();
}
// Get size files number and sub directory number in a given directory(strPath)
// Parameter: strPath is the full path name.
// nSize is the reference of the directory size
// nFileNumber is the reference of the files number
// nFolderNumber is the reference of the directory number
void GetSizeContentInFolder(CString strPath, int &nSize, int &nFileNumber, int &nFolderNumber)
{
CFileFind FindFile;
CString strNewPath = strPath + "\\*.*";
CString strNextPath;
BOOL bContinue = FindFile.FindFile(strNewPath);
//如果目录非空,进入循环
while (bContinue == TRUE)
{
bContinue = FindFile.FindNextFile();
if(FindFile.IsDots())
continue;
//如果是子目录,目录数加一,递归调用函数GetSizeContentInFolder()
if(FindFile.IsDirectory())
{
nFolderNumber++;
strNextPath = FindFile.GetFilePath();
GetSizeContentInFolder(strNextPath, nSize, nFileNumber, nFolderNumber);
}
//如果是文件,文件数加一,并将该文件的大小累加到nSize
else
{
nFileNumber++;
nSize += FindFile.GetLength();
}
}
FindFile.Close();
}
// Get the specified directory attribute
//Parameter: strPath is the full path name of the directory
// bAttribute[0]: Is ReadOnly?
// bAttribute[1]: Is Hidden?
// bAttribute[2]: Is Archived?
// strAttribute[0]: the directiry creation time
// strAttribute[1]: the directiry size
// strAttribute[2]: the files number that the directiry contains
// strAttribute[2]: the sub directory number that the directiry contains
void GetAttribute(CString strPath, int bAttribute[], CString strAttribute[])
{
CString strLocation = strPath.Left(strPath.ReverseFind('\\'));
CTime CreateTime;
CFileFind FindFile;
FindFile.FindFile(strPath);
FindFile.FindNextFile();
// 获得目录的一般属性
bAttribute[0] = FindFile.IsReadOnly();
bAttribute[1] = FindFile.IsHidden();
bAttribute[2] = FindFile.IsArchived();
// 获得目录的创建时间
int bSuccess = FindFile.GetCreationTime(CreateTime);
if(bSuccess != 0)
strAttribute[0] = CreateTime.Format("%y年 %m月 %d日 %H时 %M分 %S秒");
else
strAttribute[0] = "(未知)";
int nSize=0, nFileNumber=0, nFolderNumber=0;
char size[10], FileNumber[10], FolderNumber[10];
// 获得目录strPath所包含的子目录数、文件数和文件所占字节的总和
GetSizeContentInFolder(strPath, nSize, nFileNumber,nFolderNumber);
itoa(nSize, size, 10);
itoa(nFileNumber, FileNumber, 10);
itoa(nFolderNumber, FolderNumber, 10);
strAttribute[1] = size;
strAttribute[1] += " 字节";
strAttribute[2] = FileNumber;
strAttribute[2] += " 个文件, ";
strAttribute[2] += FolderNumber;
strAttribute[2] += " 个文件夹";
FindFile.Close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -