• C++使用通配符查找文件(FindFirstFile)


    调用 FindFirstFile 和 FindNextFile 可搜索某个目录下的相应文件。

    BOOL SearchFilesByWildcard(WCHAR *wildcardPath)
    {
    	HANDLE hFile = INVALID_HANDLE_VALUE; 
    	WIN32_FIND_DATA pNextInfo;  
     
    	hFile = FindFirstFile(wildcardPath,&pNextInfo); 
    	if(INVALID_HANDLE_VALUE == hFile)  
    	{  
    		return FALSE;  
    	}  
     
    	WCHAR infPath[MAX_PATH] = {0};
    	if(pNextInfo.cFileName[0] != '.')
    	{
    		printf("Find result = %ws
    ",pNextInfo.cFileName);
    	}
     
    	while(FindNextFile(hFile,&pNextInfo))  
    	{  
    		if(pNextInfo.cFileName[0] == '.')
    		{
    			continue;  
    		}
     
    		printf("Find result = %ws
    ",pNextInfo.cFileName);
    	}
     
    	return FALSE;
    }
    
    int main(int argc,char* argv[])
    {
    	setlocale(LC_ALL,"chs");
     
    	//查找 abc开头的txt文件
    	printf("Search 1:
    ");
    	SearchFilesByWildcard(L"E:\Test\abc*.txt");
    	printf("
    ");
     
    	//查找 abc开头的文件
    	printf("Search 2:
    ");
    	SearchFilesByWildcard(L"E:\Test\abc*");
    	printf("
    ");
     
    	//查找jnt文件
    	printf("Search 3:
    ");
    	SearchFilesByWildcard(L"E:\Test\*.jnt");
    	printf("
    ");
     
    	//查找文件名为4个字符的txt文件
    	printf("Search 4:
    ");
    	SearchFilesByWildcard(L"E:\Test\????.txt");
    	printf("
    ");
     
    	//查找包含"档案"的文件
    	printf("Search 5:
    ");
    	SearchFilesByWildcard(L"E:\Test\*档案*");
    	printf("
    ");
     
    	getchar();
    	return 0;
    }
    
  • 相关阅读:
    省选模拟81
    概率期望+计数
    省选模拟80
    省选模拟79
    省选模拟78
    Jenkins 安装及 Git | Maven 安装
    虚拟机VirtualBox及轻量级的CentOS
    服务器免密码登录
    Nginx 常用配置模板
    IDEA配置 及 快捷键
  • 原文地址:https://www.cnblogs.com/chay/p/10888771.html
Copyright © 2020-2023  润新知