Browse Folder dialog, search folder and all sub folders using C/C++..

本文提供了一个使用C++实现的功能,该功能允许用户浏览文件夹及其所有子文件夹,并搜索特定类型的文件。通过调用`BrowseFolder`函数,用户可以打开一个浏览文件夹对话框,选择一个目录。然后,`SearchFolder`函数会被调用,它会遍历所选目录及其所有子目录,查找指定类型的文件,并将找到的文件名插入到一个假定的列表框中。
//Call Browse Folder Window, Search Entire Folder and all sub-folders for desired file(s).
//This function was written by Jered McFerron ( JHawkZZ ). It's a pretty handy thing to add to your
//windows application, and I decided to publish this because I've seen many requests and few replies.


//Some parts of the Browse Folder dialog code taken from http://www.mvps.org/vcfaq/sdk/20.htm.


#include <windows.h>
#include <shlobj.h>
#include <string.h>

//BROWSE FOLDER - Opens a browse folder dialog.
void	BrowseFolder( void )
{
	TCHAR path[MAX_PATH];
	BROWSEINFO bi = { 0 };
	bi.lpszTitle = ("All Folders Automatically Recursed.");
	LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
	
	if ( pidl != 0 )
	{
		// get the name of the folder and put it in path
		SHGetPathFromIDList ( pidl, path );

		//Set the current directory to path
		SetCurrentDirectory ( path );
	
		//Begin the search
		SearchFolder( path );


		// free memory used
		IMalloc * imalloc = 0;
		if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
		{
			imalloc->Free ( pidl );
			imalloc->Release ( );
		}
	}
}//BROWSE FOLDER


//SEARCH FOLDER - Searches folder and all sub-folders for audio files.
void	SearchFolder( TCHAR * path )
{
		//Declare all needed handles
		WIN32_FIND_DATA FindFileData;
		HANDLE			hFind;

		TCHAR	filename[ MAX_PATH + 256 ];
		TCHAR	pathbak[ MAX_PATH ];

		//Make a backup of the directory the user chose
		strcpy( pathbak, path );

		//Find the first file in the directory the user chose
		hFind = FindFirstFile ( "*.*", &FindFileData );

		//Use a do/while so we process whatever FindFirstFile returned
		do
		{
			//Is it valid?
			if ( hFind != INVALID_HANDLE_VALUE )
			{
				//Is it a . or .. directory? If it is, skip, or we'll go forever.
				if ( ! ( strcmp( FindFileData.cFileName, "." ) ) || ! ( strcmp( FindFileData.cFileName, ".." ) ) )
				{
					continue;
				}

				//Restore the original directory chosen by the user
				strcpy( path, pathbak );

				//Append the file found on to the path of the directory the user chose
				sprintf( path, "%s\\%s", path, FindFileData.cFileName );

				//If SetCurrentDirectory Succeeds ( returns 1 ) the current file is a directory. Pause this function,
				//and have it call itself. This will begin the whole process over in a sub directory.
				if ( ( SetCurrentDirectory( path ) ) )
				{
					SearchFolder( path );
				}
				
				 //Otherwise right here is where you need to insert what you want to do. 
			         //As an example let's add the filename to a list box.
          			 //INSERT WHAT YOU WANT DONE BELOW!
           			 SendMessage( m_listbox_hwnd, LB_ADDSTRING, 0, path ); //<--INSERT WHAT YOU WANT DONE HERE!
				//m_listbox_hwnd is just an example handle to a fake listbox. Delete all this
				and put your own code.
	
			}
		}
		while ( FindNextFile ( hFind, &FindFileData ) && hFind != INVALID_HANDLE_VALUE );

		FindClose ( hFind );
}//SEARCH FOLDER

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值