// Finaldesktop.cpp : Defines the entry point for the console application.
//
include
include
include
include
include
using namespace std;
int SearchDirectory(vector &refvecFiles,
const string &refcstrRootDirectory,
const string &refcstrExtension,
bool bSearchSubdirectories = true)
{
string strFilePath; // Filepath
string strPattern; // Pattern
string strExtension; // Extension
HANDLE hFile; // Handle to file
WIN32_FIND_DATA FileInformation; // File information
strPattern = refcstrRootDirectory + "\.";
hFile = FindFirstFile(strPattern.c_str(), &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
do
{
if(FileInformation.cFileName[0] != '.')
{
strFilePath.erase();
strFilePath = refcstrRootDirectory + "\" + FileInformation.cFileName;
if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(bSearchSubdirectories)
{
// Search subdirectory
int iRC = SearchDirectory(refvecFiles,
strFilePath,
refcstrExtension,
bSearchSubdirectories);
if(iRC)
return iRC;
}
}
else
{
// Check extension
strExtension = FileInformation.cFileName;
strExtension = strExtension.substr(strExtension.rfind(".") + 1);
if(strExtension == refcstrExtension)
{
// Save filename
refvecFiles.push_back(strFilePath);
}
}
}
} while(FindNextFile(hFile, &FileInformation) == TRUE);
// Close handle
FindClose(hFile);
DWORD dwError = GetLastError();
if(dwError != ERROR_NO_MORE_FILES)
return dwError;
}
return 0;
}
int main()
{
int iRC = 0;
vector vecAviFiles;
vector vecTxtFiles;
// Search 'c:' for '.avi' files including subdirectories
iRC = SearchDirectory(vecAviFiles, "c:", "avi");
if(iRC)
{
cout << "Error " << iRC << endl;
return -1;
}
// Print results
for(vector::iterator iterAvi = vecAviFiles.begin();
iterAvi != vecAviFiles.end();
++iterAvi)
cout << *iterAvi << endl;
// Search 'c:\textfiles' for '.txt' files excluding subdirectories
iRC = SearchDirectory(vecTxtFiles, "c:\textfiles", "txt", false);
if(iRC)
{
cout << "Error " << iRC << endl;
return -1;
}
// Print results
for(vector::iterator iterTxt = vecTxtFiles.begin();
iterTxt != vecTxtFiles.end();
++iterTxt)
cout << *iterTxt << endl;
// Wait for keystroke
_getch();
return 0;
}