Article View: borland.public.cppbuilder.nativeapi
Article #8531Re: Get list of files in directory in code?
From: Ed Mulroy [TeamB]
Date: Mon, 11 Oct 2004 00:54
Date: Mon, 11 Oct 2004 00:54
112 lines
3826 bytes
3826 bytes
You can verify the existence of the directory either with
FindFirstFile and FindClose or with GetFileAttributes.
You can retrieve the file names with FindFirstFile, FindNextFile and
FindClose.
Here is an example:
----------------------------
#include <windows.h>
#include <vector>
#include <string>
typedef std::vector<std::string> StringVect;
const DWORD NOT_FOUND = 0xFFFFFFFF;
StringVect FindDllsInDir(const char *dir_name)
{
using std::strcpy;
using std::strlen;
using std::strcmp;
StringVect list_of_files;
DWORD dir_attrib = GetFileAttributes(dir_name);
// if the dir is found and is a directory name
if ((dir_attrib != NOT_FOUND) &&
(dir_attrib & FILE_ATTRIBUTE_DIRECTORY))
{
// info struct used by the find functions
WIN32_FIND_DATA wfd;
// buffer in which we will assembly full names with path
char buffer[MAX_PATH + 1];
strcpy(buffer, dir_name);
// end_ptr is the location following the
// directory name
char *end_ptr = buffer + strlen(buffer);
strcpy(end_ptr, "\\*.dll");
// 'buffer' now contains the orig dir followed by \*.dll
// search for a *.DLL file in that directory
HANDLE find_hand = FindFirstFile(buffer, &wfd);
// if a file was found
if (find_hand != INVALID_HANDLE_VALUE)
{
do // loop to find all the files
{
// assemble file name with path by
// overwriting where the \*.dll was located
strcpy(end_ptr, wfd.cFileName);
// add it to the vector of strings
list_of_files.push_back(buffer);
// continue as long as files are found
} while (FindNextFile(find_hand, &wfd));
FindClose(find_hand); // close the search handle
}
}
return list_of_files; // return what we've found
}
#if defined(TESTING)
// you can test this code by saving it to a file
// with a .cpp extension and giving a command like this
// bcc32 -WCR -v -DTESTING filename
#include <cstdio>
int main()
{
using std::printf;
StringVect sv = FindDllsInDir("c:\\winnt");
int nfound = sv.size();
printf("%d found\n", nfound);
for (int i = 0; i < nfound; ++i)
{
printf("%s\n", sv[i].c_str());
}
return 0;
}
#endif
----------------------------
. Ed
> Randall Parker wrote in message
> news:4169bd73$1@newsgroups.borland.com...
>
> Suppose one has a string which is a path to a directory. How can
> one:
>
> 1) Find out if the directory exists.
>
> 2) retrieve a list of strings of filenames in that dir that, say,
> end in .dll?
Technical Details
Message-ID:
<4169d9ea@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4169bd73$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,fa18d5d80bd97e57 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Ed Mulroy [TeamB]" <dont_email_me@invalid.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <4169bd73$1@newsgroups.borland.com> Subject: Re: Get list of files in directory in code? Date: Sun, 10 Oct 2004 20:54:56 -0400 Lines: 114 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 12.161.27.67 Message-ID: <4169d9ea@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1097456106 12.161.27.67 (10 Oct 2004 17:55:06 -0700) X-Authenticated-User: edmulroy Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!newsfeed.icl.net!news-FFM2.ecrc.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail