📄 vmdirscanner.cpp
字号:
HANDLE hFind;
if ( !GetCurrentDirectory( MAX_PATH+1, achCurrentDir ) )
{
SetLastError();
return( false );
}
if ( 0 != strcmp( achCurrentDir, m_achCurSearchDir ) )
{
if ( !SetCurrentDirectory( m_achCurSearchDir ) )
{
SetLastError();
SetCurrentDirectory( achCurrentDir );
return( false );
}
}
hFind = FindFirstFile( m_achMask, &xFileInfo );
if ( INVALID_HANDLE_VALUE != hFind )
{
do
{
if ( xFileInfo.dwFileAttributes
& ( FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_ARCHIVE ) )
{
sprintf( achFullPath, "%s%s", m_achCurSearchDir, xFileInfo.cFileName );
std::string oFullPath = achFullPath;
m_oFilesFound.push_back( oFullPath );
}
} while ( FindNextFile( hFind, &xFileInfo ) );
}
FindClose( hFind );
return ( SetCurrentDirectory( achCurrentDir ) != 0 );
}
/* end of function "VMDirScanner::CurDirFileScan" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: GoUpOneDir
DESCRIPTION: Sets the desination string up one directory
INPUT: pchDir - the destination directory string
OUTPUT: pchDir - contents are modified
RETURNS: void
*/
void VMDirScanner::GoUpOneDir( char* pchDir )
{
for ( int iLength = strlen( pchDir ) - 1; iLength > 0; iLength-- )
{
if ( pchDir[ iLength ] == '\\' )
{
pchDir[ iLength ] = '\0';
return;
}
}
return;
}
/* end of function "GoUpOneDir" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: AddBackSlash
DESCRIPTION: Check end of string and add a backshash if one does not
exist
INPUT: pchWorking - a pointer to the string to add the '\' to
OUTPUT: pchWorking - contents are modified
RETURNS: void
*/
void VMDirScanner::AddBackSlash( char* pchWorking )
{
short iLength;
// add a \ if needed
iLength = strlen( pchWorking );
if ( iLength )
{
if ( pchWorking[ iLength - 1 ] != '\\' )
{
strcat( pchWorking, "\\" );
}
}
}
/* end of function "AddBackSlash" */
/*****************************************************************************/
/*****************************************************************************/
/*
FUNCTION NAME: ScanForAllFiles
DESCRIPTION: finds a subdirectory in the current working directory,
changes the current working directory to this sub-
directory, and recusively calls itself until there are
no more subdirectories
COMMENTS: When a new directory is entered from above, a handle for
the new directory is obtained using FindFirstDirectory.
Once the first directory is found, the current working
directory is changed to this first directory and
ScanForAllFiles() is recursively called again. At this
point, the next available directory is searched for
using FindNextFile, entered and a recursive call is made
to ScanForAllFiles(). When each directory has been searched,
until no more directories exist, the current working directory
is changed to the parent directory (..). This continues until
the current working directory is equal to the original
directory.
INPUT: wLevel - bookmark, when wLevel is greater than 0, then
the current working directory is a subdirectory of the
original directory. If wLevel is equal to 0, then the
directory is the original directory and the recursive
calls stop
RETURNS: FALSE if any errors
*/
bool VMDirScanner::ScanForAllFiles( WORD wLevel )
{
bool bRC=TRUE;
HANDLE hSearch;
WIN32_FIND_DATA xFindInfo;
if ( !GetCurrentDirectory( MAX_PATH, m_achCurSearchDir ) )
{
SetLastError();
return( false );
}
hSearch = FindFirstFile( "*", &xFindInfo );
if ( INVALID_HANDLE_VALUE == hSearch )
{
FindClose( hSearch );
if ( wLevel )
{
GoUpOneDir( m_achCurSearchDir );
if ( FALSE == SetCurrentDirectory("..") )
{
SetLastError();
return( false );
}
else
{
return( true );
}
}
else
{
return( true );
}
}
for (;;)
{
// if this "file" is ".." or "." then get next and continue
//
if ( strcmp( xFindInfo.cFileName,"." ) == 0
|| strcmp( xFindInfo.cFileName,".." ) == 0 )
{
if ( FindNextFile( hSearch, &xFindInfo ) == FALSE )
{
GoUpOneDir( m_achCurSearchDir );
if ( SetCurrentDirectory( ".." ) == FALSE )
{
SetLastError();
FindClose( hSearch );
return( false );
}
else
{
FindClose( hSearch );
return( true );
}
}
else
{
continue;
}
}
// if this is a directory scan it
//
if ( xFindInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
AddBackSlash( m_achCurSearchDir );
strcat( m_achCurSearchDir, xFindInfo.cFileName );
m_oDirsFound.insert( m_oDirsFound.begin(), m_achCurSearchDir );
if ( FALSE == SetCurrentDirectory( m_achCurSearchDir ) )
{
SetLastError();
FindClose( hSearch );
return( false );
}
if ( !ScanForAllFiles( ++wLevel ) )
{
FindClose( hSearch );
return( false );
}
}
else
{
// if wild card search, then just add each
// file found to the list of files that match
// the mask
//
if ( ( 0 == strcmp( m_achMask , "*.*" ) )
|| ( 0 == strcmp( m_achMask , "*.?" ) )
|| ( 0 == strcmp( m_achMask , "?.*" ) )
|| ( 0 == strcmp( m_achMask , "?.?" ) ) )
{
std::string oFullPath;
char achPartialPath[ MAX_PATH + 1 ];
strcpy( achPartialPath, m_achCurSearchDir );
AddBackSlash( achPartialPath );
strcat( achPartialPath, xFindInfo.cFileName );
oFullPath = achPartialPath;
m_oFilesFound.push_back( oFullPath );
}
else
{
if ( ( NULL != strchr( m_achMask, '*' ) )
|| ( NULL != strchr( m_achMask, '?' ) ) )
{
// need to 'fix up' the mask so that the
// next test will work. This fix up is a little
// out of place here, but it will only fire once
// so we'll live with it....
//
char achNewMask[MAX_PATH+1];
ZeroMemory( achNewMask, MAX_PATH + 1 );
int iCurDestPos = 0;
for ( unsigned int iLoop = 0; iLoop < strlen( m_achMask ); iLoop++ )
{
if ( ( '*' != m_achMask[ iLoop ] ) && ( '?' != m_achMask[ iLoop ] ) )
{
achNewMask[ iCurDestPos ] = m_achMask[ iLoop ];
iCurDestPos++;
}
}
strcpy( m_achMask, achNewMask );
}
// if it is a file that matches the mask,
// then add that file path to the 'found file' list
//
if ( NULL != strstr( xFindInfo.cFileName, m_achMask ) )
{
std::string oFullPath;
char achPartialPath[ MAX_PATH + 1 ];
strcpy( achPartialPath, m_achCurSearchDir );
AddBackSlash( achPartialPath );
strcat( achPartialPath, xFindInfo.cFileName );
oFullPath = achPartialPath;
m_oFilesFound.push_back( oFullPath );
}
}
}
if ( FindNextFile( hSearch, &xFindInfo ) == FALSE )
{
GoUpOneDir( m_achCurSearchDir );
if ( SetCurrentDirectory( ".." ) == FALSE )
{
SetLastError();
FindClose( hSearch );
return( false );
}
else
{
FindClose( hSearch );
return( true );
}
}
}
FindClose( hSearch );
return( true );
}
/* end of function "ScanForAllFiles" */
/*****************************************************************************/
/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -