[英]go to other logical drives and continues to search for file
i have a program that search for files of a particular extention(.apk) in a particular logical drive(C:). 我有一个程序,在特定的逻辑驱动器(C :)中搜索特定扩展名(.apk)的文件。 my system has 3 more partitions :- D: E: F: and these also contains apk files. 我的系统还有3个分区: - D:E:F:这些还包含apk文件。 now i want that my program will also search in these logical drives for the apk's files. 现在我想我的程序也将在这些逻辑驱动器中搜索apk的文件。 how i can do this. 我怎么能做到这一点。 please anybody have some suggestion then help me, am trying this since morning. 请任何人有一些建议然后帮助我,我从早上开始尝试这个。 here is my code..... 这是我的代码.....
int SearchDirectory(std::vector<std::string> &refvecFiles,
const std::string &refcstrRootDirectory,
const std::string &refcstrExtension,
bool bSearchSubdirectories = true)
{
std::string strFilePath; // Filepath
std::string strPattern; // Pattern
std::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;
std::vector<std::string> vecAPKFiles;
//std::vector<std::string> vecTxtFiles;
// Search 'c:' for '.apk' files including subdirectories
iRC = SearchDirectory(vecAPKFiles, "c:", "apk");
if(iRC)
{
std::cout << "Error " << iRC << std::endl;
return -1;
}
// Print results
for(std::vector<std::string>::iterator iterAvi = vecAPKFiles.begin();
iterAvi != vecAPKFiles.end();
++iterAvi)
std::cout << *iterAvi << std::endl;
TCHAR szDrive[] = (" A:");
DWORD uDriveMask = GetLogicalDrives();
while(uDriveMask)
{
// Use the bitwise AND, 1â€"available, 0-not available
if(uDriveMask & 1)
printf("%s ", (const char *)szDrive);
// increment, check next drive
++szDrive[1];
// shift the bitmask binary right
uDriveMask >>= 1;
}
printf("\n ");
// Wait for keystroke
_getch();
return 0;
}
You've got a bit weird drive string char szDrive[] = " A:";
你有一点奇怪的驱动器字符串char szDrive[] = " A:";
(Even with the half-baked TCHAR stuff removed). (即使删除了半生不熟的TCHAR东西)。 I'd use char szDrive[] = "A:\\";
我用char szDrive[] = "A:\\";
instead, and increment ++szDrive[0];
相反,并增加++szDrive[0];
. 。 You can then pass szDrive
to SearchDirectory()
然后,您可以将szDrive
传递给SearchDirectory()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.