Click here to Skip to main content
15,888,254 members
Articles / Mobile Apps / Windows Mobile

Directory file listing utility

Rate me:
Please Sign up or sign in to vote.
3.28/5 (21 votes)
13 Jun 2012CPOL3 min read 99.5K   3.7K   39  
An article about methods to recursively list all files in a given directory.
#include "stdafx.h"
#include "BrowseForFolder.H"

struct	BrowseData
{
	LPCTSTR				pszTitle;
	LPCTSTR				pszInitDir;
	UINT				ExtFlags;
};

BOOL CheckNotReadOnly(LPCTSTR pszDir)
{
//	Return TRUE if ReadOnly
//	DWORD	Attributes = GetFileAttributes(pszDir);
//	return Attributes & FILE_ATTRIBUTE_READONLY;
	
	CString	Path = pszDir;
	
	if(!Path.IsEmpty() && (Path.Right(1) != _T('\\')))
	{
		Path += _T('\\');
	}

	Path += _T("XXXTemp");

	if(CreateDirectory(Path, NULL))
	{
		RemoveDirectory(Path);
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}

int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	BrowseData	*pbd = (BrowseData *) lpData;
	
	switch(uMsg)
	{
		case BFFM_INITIALIZED:
			::SendMessage(hwnd, BFFM_SETSELECTION, (WPARAM) TRUE, (LPARAM) pbd->pszInitDir);
			break;

		case BFFM_SELCHANGED:
		{
			TCHAR pszBuffer[MAX_PATH];
		
			if (::SHGetPathFromIDList((LPCITEMIDLIST) lParam, pszBuffer))
			{
				if (pbd->ExtFlags & BFF_CHECK_NOT_READONLY)
				{
					if (CheckNotReadOnly(pszBuffer))
					::SendMessage(hwnd, BFFM_ENABLEOK, (WPARAM) FALSE, (LPARAM) 0);
				}
			}
		}

		break;
	}

	return 0;
}

CString BrowseForFolder(CWnd *pParent, LPCTSTR pszTitle, LPCTSTR pszInitDir, UINT ExtFlags, UINT Flags)
{
	CString		Folder;

	LPMALLOC pMalloc;		/* Gets the Shell's default allocator */
	
	if(::SHGetMalloc(&pMalloc) == NOERROR)
	{
		TCHAR				pszBuffer[MAX_PATH];
		BrowseData			bd;
		bd.pszTitle			= pszTitle;
		bd.pszInitDir		= pszInitDir;
		bd.ExtFlags			= ExtFlags;

		BROWSEINFO			bi;
		bi.hwndOwner		= pParent->GetSafeHwnd();
		bi.pidlRoot			= NULL;
		bi.pszDisplayName	= pszBuffer;
		bi.lpszTitle		= pszTitle;
		bi.ulFlags			= Flags;
		bi.lpfn				= BrowseCallbackProc;
		bi.lParam			= (LPARAM) &bd;
		bi.iImage			= 0;

		LPITEMIDLIST	pidl;

		if((pidl = ::SHBrowseForFolder(&bi)) != NULL)
		{
			if(::SHGetPathFromIDList(pidl, pszBuffer))
				Folder = pszBuffer;

			// Free the PIDL allocated by SHBrowseForFolder.
			pMalloc->Free(pidl);
		}

		// Release the shell's allocator.
		pMalloc->Release();
	}

	return Folder;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Mind & Machines
Bangladesh Bangladesh
(AKA The Freak), besides being a computer nerd, given his love for extreme sports, is a fire spinner, sky diver, and a XC/DH biker. He juggles his time between computing, research, business, travelling, gourmet cooking, and many other bizarre hobbies.

Comments and Discussions