0
我有一个搜索所有驱动器以查找所有txt
扩展一些代码windows文件夹,但我的问题是,我想排除在系统驱动器的Windows文件夹,我测试了许多情况,但它不工作,我的代码是:排除在搜索文件
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include "Shlwapi.h"
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "shlwapi.lib")
void FindFilesRecursively(LPCTSTR lpFolder, LPCTSTR lpFilePattern)
{
TCHAR szFullPattern[MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFindFile;
// first we are going to process any subdirectories
PathCombine(szFullPattern, lpFolder,_T("*"));
hFindFile = FindFirstFile(szFullPattern, &FindFileData);
if(hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (_tcscmp(FindFileData.cFileName, _T(".")) != 0 &&
_tcscmp(FindFileData.cFileName, _T("..")) != 0)
{
// found a subdirectory; recurse into it
PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
FindFilesRecursively(szFullPattern, lpFilePattern);
//_tprintf_s(_T("%s\n"), lpFolder);
}
}
} while(FindNextFile(hFindFile, &FindFileData));
FindClose(hFindFile);
}
// now we are going to look for the matching files
PathCombine(szFullPattern, lpFolder, lpFilePattern);
hFindFile = FindFirstFile(szFullPattern, &FindFileData);
if(hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
// found a file; do something with it
PathCombine(szFullPattern, lpFolder, FindFileData.cFileName);
_tprintf_s(_T("%s\n"), szFullPattern);
}
} while(FindNextFile(hFindFile, &FindFileData));
FindClose(hFindFile);
}
}
DWORD mydrives = 100;// buffer length
char lpBuffer[100];// buffer for drive string storage
int main()
{
DWORD dwSize = MAX_PATH;
char szLogicalDrives[MAX_PATH] = {0};
DWORD dwResult = GetLogicalDriveStrings(dwSize,szLogicalDrives);
if (dwResult > 0 && dwResult <= MAX_PATH)
{
char* szSingleDrive = szLogicalDrives;
while(*szSingleDrive)
{
// get the next drive
FindFilesRecursively(_T(szSingleDrive), _T("*.txt"));
szSingleDrive += strlen(szSingleDrive) + 1;
}
}
}
我测试_tcscmp(FindFileData.cFileName, _T("C:\windows")) != 0
和等,但它没有工作,我也想知道如何使用getenv ("SystemDrive");
在我的代码来检测系统驱动,并提前
C:\
,对不起
注意:NTFS文件系统不区分大小写,但文件名**有**情况,因此您应该使用_tcsicmp来对字符串进行不区分大小写的比较。 –