比较简单,使用win32函数FindFirstFile和FileNextFile进行相关操作
其中需要注意的是文件的属性的判断
#pragma once
#include <Windows.h>
#include <string>
using namespace std;
#ifndef WINCE
#include "assert.h"
#else
#undef assert
#define assert ASSERT
#endif
typedef enum
{
RFM_MUSIC=0,
RFM_MOVIE,
RFM_ALL,
}ReadFileMode;
// CCeFileFind window
class CCeFileFind
{
// Construction
public:
CCeFileFind( );
public:
// Operations
virtual void Close();
virtual BOOL FindNextFile( );
virtual BOOL FindFile( LPCTSTR pstrName = NULL);
public:
// Attributes
//Gets the length of the found file, in bytes.
DWORD GetLength() const;
//Gets the name, including the extension, of the found file
wstring GetFileName() const;
//Gets the whole path of the found file.
wstring GetFilePath() const;
//Gets the whole path of the found file.
wstring GetRoot() const;
// to get the time the specified file was created
virtual BOOL GetCreationTime( FILETIME* pFileTime ) const;
//Gets the time that the file was last accessed.
virtual BOOL GetLastAccessTime( FILETIME* pFileTime ) const;
//Gets the time the file was last changed and saved.
virtual BOOL GetLastWriteTime( FILETIME* pFileTime ) const;
//Indicates the desired file attributes of the file to be found.
virtual BOOL MatchesMask( DWORD dwMask ) const;
//Determines if the name of the found file has the name "." or "..",
//indicating that is actually a directory.
virtual BOOL IsDots( ) const;
//Determines if the found file is read-only.
BOOL IsReadOnly( ) const;
//Determines if the found file is a directory.
BOOL IsDirectory( ) const;
//Determines if the found file is compressed.
BOOL IsCompressed( ) const;
//Determines if the found file is a system file.
BOOL IsSystem( ) const;
//Determines if the found file is hidden.
BOOL IsHidden( ) const;
//Determines if the found file is temporary.
BOOL IsTemporary( ) const;
//Determines if the found file is normal (in other words, has no other attributes).
BOOL IsNormal( ) const;
//Determines if the found file is archived.
BOOL IsArchived( ) const;
// Implementation
public:
virtual ~CCeFileFind();
private:
LPWIN32_FIND_DATA m_pfiledata;
LPWIN32_FIND_DATA m_pNextdata;
wstring m_csRoot;
HANDLE m_hFileHandle;
void AssertDoneNext() const;
};
// CeFileFind.cpp : implementation file
//
#include "stdafx.h"
#include "CeFileFind.h"
#define DELETE_POINTER(ptr) if( ptr != NULL ) { delete ptr; ptr = NULL; }
#define DIR_SEPERATOR _T('\\')
/
// CCeFileFind
CCeFileFind::CCeFileFind()
: m_hFileHandle(NULL) // initialize to NULL
, m_pfiledata(NULL)
, m_pNextdata(NULL)
{
}
CCeFileFind::~CCeFileFind()
{
Close();
}
// Operations
BOOL CCeFileFind::FindFile(LPCTSTR pstrName)
{
Close();
// if NULL , wild card search
if( NULL == pstrName )
{
m_csRoot = DIR_SEPERATOR;
pstrName = _T("\\*.*");
}
else
{
m_csRoot = pstrName;
int nPos = m_csRoot.rfind( '\\' );
if( nPos == 0 )
m_csRoot = _T('\\');
else
m_csRoot = m_csRoot.substr(0, nPos + 1);
}
if (m_pNextdata==NULL)
{
m_pNextdata = new WIN32_FIND_DATA;
}
// search for file
m_hFileHandle = FindFirstFile( pstrName, m_pNextdata );
if ( m_hFileHandle == INVALID_HANDLE_VALUE )
{
Close();
return FALSE;
}
// file was found
return TRUE;
}
BOOL CCeFileFind::FindNextFile()
{
assert(m_hFileHandle != NULL);
if (m_hFileHandle == NULL)
return FALSE;
if (m_pfiledata == NULL)
m_pfiledata = new WIN32_FIND_DATA;
AssertDoneNext();
LPWIN32_FIND_DATA pTemp = m_pfiledata;
m_pfiledata = m_pNextdata;
m_pNextdata = pTemp;
return ::FindNextFile(m_hFileHandle,m_pNextdata);
}
void CCeFileFind::Close()
{
DELETE_POINTER( m_pfiledata );
DELETE_POINTER( m_pNextdata );
if( m_hFileHandle!= NULL && m_hFileHandle != INVALID_HANDLE_VALUE )
{
::FindClose( m_hFileHandle );
m_hFileHandle = NULL;
}
}
BOOL CCeFileFind::MatchesMask(DWORD dwMask) const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if ( m_pfiledata != NULL)
return (!!(m_pfiledata->dwFileAttributes & dwMask) );
else
return FALSE;
}
wstring CCeFileFind::GetRoot() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
return m_csRoot;
else
return _T("");
}
BOOL CCeFileFind::GetLastAccessTime(FILETIME* pTimeStamp) const
{
assert(m_hFileHandle != NULL);
assert(pTimeStamp != NULL);
AssertDoneNext();
if (m_pfiledata != NULL && pTimeStamp != NULL)
{
*pTimeStamp = m_pfiledata -> ftLastAccessTime;
return TRUE;
}
else
return FALSE;
}
BOOL CCeFileFind::GetLastWriteTime(FILETIME* pTimeStamp) const
{
assert(m_hFileHandle != NULL);
assert(pTimeStamp != NULL);
AssertDoneNext();
if (m_pfiledata != NULL && pTimeStamp != NULL)
{
*pTimeStamp = m_pfiledata -> ftLastWriteTime;
return TRUE;
}
else
return FALSE;
}
BOOL CCeFileFind::GetCreationTime(FILETIME* pTimeStamp) const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL && pTimeStamp != NULL)
{
*pTimeStamp = m_pfiledata -> ftCreationTime;
return TRUE;
}
else
return FALSE;
}
BOOL CCeFileFind::IsDots() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
// return TRUE if the file name is "." or ".." and
// the file is a directory
BOOL bResult = FALSE;
if (m_pfiledata != NULL && IsDirectory())
{
LPWIN32_FIND_DATA pFindData = m_pfiledata;
if (pFindData->cFileName[0] == _T('.'))
{
if (pFindData->cFileName[1] == _T('\0') ||
(pFindData->cFileName[1] == _T('.') &&
pFindData->cFileName[2] == _T('\0')))
{
bResult = TRUE;
}
}
}
return bResult;
}
BOOL CCeFileFind::IsArchived( ) const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsCompressed() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes == FILE_ATTRIBUTE_COMPRESSED )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsDirectory() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsHidden() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsNormal() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_NORMAL )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsReadOnly() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_READONLY )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsSystem() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_SYSTEM )
return TRUE;
else
return FALSE;
}
return FALSE;
}
BOOL CCeFileFind::IsTemporary() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
{
if( m_pfiledata -> dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY )
return TRUE;
else
return FALSE;
}
return FALSE;
}
wstring CCeFileFind::GetFilePath() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
wstring csResult = m_csRoot;
if (csResult[csResult.size() - 1] != DIR_SEPERATOR )
csResult += DIR_SEPERATOR;
csResult += GetFileName();
return csResult;
}
wstring CCeFileFind::GetFileName() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
wstring ret;
if (m_pfiledata != NULL)
ret = m_pfiledata->cFileName;
return ret;
}
DWORD CCeFileFind::GetLength() const
{
assert(m_hFileHandle != NULL);
AssertDoneNext();
if (m_pfiledata != NULL)
return m_pfiledata -> nFileSizeLow;
else
return 0;
}
void CCeFileFind::AssertDoneNext() const
{
// if you trip the ASSERT in the else side, you've called
// a Get() function without having done at least one
// FindNextFile() call
if (m_hFileHandle == NULL)
assert( m_pfiledata == NULL && m_pNextdata == NULL);
else
assert( m_pfiledata != NULL && m_pNextdata != NULL);
}
BOOL ScanAllFile(const TCHAR* pszFilename)
{
std::wstring srcPath = pszFilename;
std::wstring srcFile = srcPath + TEXT("\\*.*");
// search files from the Directory
BOOL bWorking = m_finder.FindFile(srcFile.c_str());
while(bWorking)
{
// search files from Current directory
bWorking = m_finder.FindNextFile();
//is it a .directory or a ..directory
if(!m_finder.IsDots() && ! m_finder.IsSystem())
{
//is it a directory
if(m_finder.IsDirectory())
{
// if it is directory ,then call itself again
wstring srcTempPath = m_finder.GetFileName();
m_FilePathVec.push_back(srcTempPath);
}
else
{
// if it is file ,then delete
wstring srcTempPath = m_finder.GetFileName();
ClassifyFile(srcTempPath);
}
}
}
m_finder.Close();
return TRUE;
}