📄 filerename.cpp
字号:
// FileRename.cpp: implementation of the CFileRename class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "zzconnect.h"
#include "FileRename.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define PATH_NAME _T("\\Program Files\\SianoSmsUtils\\")
#define ADD_FILE_SIGH _T("1")
#define VIDEO_PIX _T(".wmv") //视频后缀
#define PHOTO_PIX _T(".jpg") //图片后缀
#define DIVISION_SYMBOLE _T("#")
CStringArray CFileRename::m_arrFileName;
CFileRename::CFileRename()
{
Remove();
}
CFileRename::~CFileRename()
{
Remove();
}
void CFileRename::Remove( )
{
if( m_arrFileName.GetSize() >= 1 )
{
CString strSigned; //改过名的文件
CString strFront,strBack,strOld;
int iIndex = 0 ;
HANDLE handle = NULL;
WIN32_FIND_DATA data;
for( int i = 0 ; i < m_arrFileName.GetSize() ; i++ )
{
strSigned = m_arrFileName.GetAt(i);
iIndex = strSigned.ReverseFind('.');
strFront = strSigned.Left(iIndex-1);
strBack = strSigned.Right( strSigned.GetLength() - iIndex );
strOld = strFront + strBack;
handle = FindFirstFile(strOld,&data);
if( handle == INVALID_HANDLE_VALUE ) //未找到
{
FindClose(handle);
//重命名文件
try
{
CFile::Rename(strSigned,strOld);
}
catch(CFileException* pEx )
{
#ifdef _DEBUG
afxDump << "File " << strSigned << " not found, cause = "
<< "\n";
#endif
pEx->Delete();
return;
}
}
else
{
FindClose(handle);
try
{
if(SetFileAttributes(strSigned,FILE_ATTRIBUTE_NORMAL)==0)
{
DWORD dwRet = GetLastError();
return;
}
CFile::Remove(strSigned);
}
catch (CFileException* pEx)
{
#ifdef _DEBUG
afxDump << "File " << strSigned << " cannot be removed\n";
#endif
pEx->Delete();
return;
}
}
}
}
m_arrFileName.RemoveAll();
}
CString CFileRename::OpenPhoto( int iType,CString strName )
{
Remove();
WIN32_FIND_DATA data;
HANDLE handle = NULL;
CString strReturn = _T("");
CString str = _T("");
CString strFind;
CString strPix = _T("*.jpg");
strFind = PATH_NAME + strPix;
handle = FindFirstFile(strFind,&data);
if( handle != INVALID_HANDLE_VALUE )
{
do
{
//处理找到的文件
str = CString(data.cFileName);
if( str.Find( strName ) != -1 )
{
CString strFront,strBack,strNew;
int iIndex = 0 ;
iIndex = str.ReverseFind('.');
strFront = str.Left(iIndex);
strBack = str.Right( str.GetLength() - iIndex );
strBack = ADD_FILE_SIGH+strBack;
strNew = PATH_NAME + strFront + strBack;
CString strOri = PATH_NAME + str;
//重命名文件
try
{
CFile::Rename(strOri,strNew);
}
catch(CFileException* pEx )
{
#ifdef _DEBUG
afxDump << "File " << strNew << " not found, cause = "
<< "\n";
#endif
pEx->Delete();
return _T("");
}
m_arrFileName.Add(strNew);
if( strReturn == _T("") )
{
strReturn = strReturn + strNew;
}
else
{
strReturn = strReturn + DIVISION_SYMBOLE + strNew;
}
}
}
while (FindNextFile(handle, &data));
}
FindClose(handle);
return strReturn;
}
/*
打开文件
[in]iType:类型,1为视频,0图片
[in]strName:文件名,例如"西直门.ts"
return: 没有这个文件返回空,有则返回文件完整路径
*/
CString CFileRename::OpenFile( int iType,CString strName )
{
if( iType == 0 )
{
return OpenPhoto( iType,strName );
}
else
{
strName = strName + VIDEO_PIX;
}
CString strDest;
strDest = PATH_NAME + strName;
int iLength = strDest.GetLength();
WIN32_FIND_DATA data;
HANDLE handle = NULL;
handle = FindFirstFile(strDest,&data);
if( handle == INVALID_HANDLE_VALUE ) //没找到
{
int iError = GetLastError();
FindClose(handle);
return _T("");
}
else
{
FindClose(handle);
CString strFront,strBack,strNew;
int iIndex = 0 ;
iIndex = strName.ReverseFind('.');
strFront = strName.Left(iIndex);
strBack = strName.Right( strName.GetLength() - iIndex );
strBack = ADD_FILE_SIGH+strBack;
strNew = PATH_NAME + strFront + strBack;
//重命名文件
try
{
CFile::Rename(strDest,strNew);
}
catch(CFileException* pEx )
{
#ifdef _DEBUG
afxDump << "File " << strDest << " not found, cause = "
<< "\n";
#endif
pEx->Delete();
return _T("");
}
return strNew;
}
}
/*
关闭文件
[in]iType:类型,1为视频,0图片
[in]strName:文件名,例如"西直门.ts"
return: 改名成功或删除成功返回true,否则false
*/
bool CFileRename::CloseFile( int iType,CString strName )
{
if( iType == 0 )
{
Remove( );
return true;
}
else
{
strName = strName + VIDEO_PIX;
}
CString strDest;
strDest = PATH_NAME + strName;
HANDLE handle = NULL;
WIN32_FIND_DATA data;
handle = FindFirstFile(strDest,&data);
CString strFront,strBack,strNew;
int iIndex = 0 ;
iIndex = strName.ReverseFind('.');
strFront = strName.Left(iIndex);
strBack = strName.Right( strName.GetLength() - iIndex );
strBack = ADD_FILE_SIGH+strBack;
strNew = PATH_NAME + strFront + strBack;
if( handle == INVALID_HANDLE_VALUE ) //未找到
{
FindClose(handle);
//重命名文件
try
{
CFile::Rename(strNew,strDest);
}
catch(CFileException* pEx )
{
#ifdef _DEBUG
afxDump << "File " << strDest << " not found, cause = "
<< "\n";
#endif
pEx->Delete();
return false;
}
return true;
}
else // 找到"西直门1.ts",删除操作
{
FindClose(handle);
try
{
if(SetFileAttributes(strNew,FILE_ATTRIBUTE_NORMAL)==0)
{
DWORD dwRet = GetLastError();
return false;
}
CFile::Remove(strNew);
}
catch (CFileException* pEx)
{
#ifdef _DEBUG
afxDump << "File " << strNew << " cannot be removed\n";
#endif
pEx->Delete();
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -