Advertisement

Obtaining files in a dir. / dir.'s in a dir. / ?

Started by May 03, 2001 07:38 PM
3 comments, last by paulcoz 23 years, 9 months ago
I need some advice - I'd like to be able to do the following (in Win32): (1) Given a directory name (as a string), obtain the names of all of the files in the directory (as strings). (2) Given a directory name (as a string), obtain the names of all folders in the directory (as strings). I also need to know how to deal with folders that contain both folders and filenames? Do you get a combined list of files and folders or one list for each? Could someone tell me for each of these items what commands I can use to accomplish these tasks? And, (3) Is it possible to refer to filenames outside the app directory by giving an absolute path eg. 'C:\temp\bitmap.bmp'? I'd like to be able to combine two strings like 'install directory' and 'folder name' to come up with absolute paths. Thanks, Paulcoz. Edited by - paulcoz on May 3, 2001 9:03:04 PM
Check out FindFirstFile/FindNextFile in the Win32 MSDN help. I''m sure you can find plenty of samples for using these if you search for them in your search engine of choice.

Absolute pathnames are always ok. Relative pathnames can get dicey because the current directory can get changed on you (the standard file save and open dialogs do this).

HTH
Advertisement
Actually, you can prevent the standard open and save dialogs from changing the current directory by specifying the OFN_NOCHANGEDIR flag in the Flags member of the OPENFILENAME structure.

And paulcoz, here''s an example of using those two API functions.

  HANDLE handle;WIN32_FIND_DATA win;int nCount = 0, bSuccess;// locate the first file, if one existsif ((handle = FindFirstFile("*.mpk", &win)) == INVALID_HANDLE_VALUE)    return(0);	// copy the filenamestrcpy(szFilenames[0], win.cFileName);nCount++;// find remaining filesdo{    if (bSuccess = FindNextFile(handle, &win))    {        strcpy(szFilenames[nCount], win.cFileName);        nCount++;    }} while (bSuccess);  


This snippet locates all the *.mpk files in the current directory and places them in an array (declared elsewhere) called szFilenames[100][256]. Note that these files are not going to be returned in alphabetical order when you find them, so you''ll have to run a sort immediately afterwards if you want to order them.

-Ironblayde
 Aeon Software

"In C we had to code our own bugs. In C++ we can inherit them." - Unknown
"Your superior intellect is no match for our puny weapons!"
Thanks, that points me in the right direction. Any idea how to look for the files in a specific folder?

Paulcoz.

Edited by - paulcoz on May 4, 2001 4:09:22 AM
This file should answer some of your questions.

dirinfo.cpp
(Excuse the mess, I never cleaned up the code)

Most of what you want to know is in the GetDirectorySize() function.

Keeb

[Edited by - Keebler on May 20, 2005 3:24:58 PM]

This topic is closed to new replies.

Advertisement