Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List Directory without using dialog? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I want to list a directory and get the file names and directory names in it, is there away?
After checking Microsoft Platform SDK documentation my expectations are quite low, but there must be a possibility for this?

Thanks in advance!
/Julien
 
There is an easier way to to this. Just create a CFileFind object and use it. There should be more documentation for the CFileFind Class in MSDN.



Rene B.


 
I have a question for you. How do you get this list of files in a dialog window. I am trying to let the user select a directory instead of a file. CFileDilog does not seem to let me do that. Is there an easy way to do this. Thanks.

Rene B.
 
Simplest way to let the user select a directory is with the shell browse for folder dialog. Here is an example with a few bells and whistles to set the start directory (in this case to current directory, but you could pass another directory as a parameter to the callback routine) and to show the currently selected directory as status text. If you don't need this, you can forget the callback routine.

#include "stdafx.h"
#include <shlobj.h>

int CALLBACK cbpbrff(HWND hwnd,UINT uMsg,LPARAM lp,LPARAM pData)
{
char dir[256];
switch(uMsg)
{
case BFFM_INITIALIZED:
{
if (GetCurrentDirectory(256,dir))
{
SendMessage(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)dir);
}
break;
}
case BFFM_SELCHANGED:
{
if (SHGetPathFromIDList((LPITEMIDLIST)lp,dir))
{
SendMessage(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)dir);
}
break;
}
default:
break;
}
return 0;
}

void getFolder(char* title,char* fldrPath,HWND owner = NULL)
{
BROWSEINFO brinfo;
LPITEMIDLIST pidl;
ZeroMemory(&brinfo,sizeof(brinfo));
brinfo.hwndOwner = owner;
brinfo.pidlRoot = NULL;
brinfo.lpszTitle = title;
brinfo.ulFlags = BIF_RETURNONLYFSDIRS|BIF_STATUSTEXT;
brinfo.lpfn = cbpbrff;
pidl = SHBrowseForFolder(&brinfo);
if (pidl) SHGetPathFromIDList(pidl,fldrPath);
IMalloc* im;
SHGetMalloc(&im);
im->Free(pidl);
im->Release();
}

int main(int argc, char* argv[])
{
char path[256];
getFolder(&quot;Select folder&quot;,path);
return 0;
}

If called from a windows app you should set the owner hwnd. :) Click here if this helped! :)
vvvvvvvvv
 
I tried this code but I get a crash after the function: SHGetPathFromIDList is called. Any suggestions? Thanks.
 
Never mind about the previous message. I fixed it. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top