Article View: borland.public.cppbuilder.nativeapi
Article #855Re: Getting directory file names in builder 5
From: Ed Mulroy [TeamB]
Date: Mon, 10 Dec 2007 17:01
Date: Mon, 10 Dec 2007 17:01
80 lines
3334 bytes
3334 bytes
If you are looking for a way to issue the command line directory request
then look at the function 'system'.
If you are looking to do it with your own code, the Windows functions
FindFirstFile, FindNextFile and FindClose are used for that. Here is a
console app that you might use as a starting point:
---------------------
#include <stdio.h>
#include <windows.h>
void PrintFileInfo(WIN32_FIND_DATA & file_info)
{
if (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
printf("<Dir> ");
printf("%s\n", file_info.cFileName);
} // end PrintFileInfo
int ListFilesInDir(const char *directory)
{
HANDLE find_handle;
WIN32_FIND_DATA wfd;
char find_pattern[MAX_PATH];
int num_files_found = 0;
// construct a search pattern string
lstrcpy(find_pattern, directory);
// end the name with "\\*.*"
const char *str_to_append;
if (*(find_pattern + strlen(find_pattern) - 1) == '\\')
str_to_append = "*.*"; // it already has the backslash
else
str_to_append = "\\*.*";
lstrcat(find_pattern, str_to_append);
// search for the files
find_handle = FindFirstFile(find_pattern, &wfd);
// if a file was found
if (find_handle != INVALID_HANDLE_VALUE)
{
do // loop to report what was found and find the next one
{
++num_files_found;
PrintFileInfo(wfd);
} while (FindNextFile(find_handle, &wfd));
FindClose(find_handle); // close the file search handle
}
return num_files_found;
} // end ListFilesInDir
int main()
{
int num_found = ListFilesInDir("C:\\Windows");
printf("%d files found\n", num_found);
return 0;
}
---------------------
> tom wrote in message
> news:475cb157@newsgroups.borland.com...
>
> Haven't been here in awhile and haven't used bcb5 in awhile either.
> What I would like to know is how can I get a listing of file names from a
> given directory in bcb5? Seems to me I did it before but for the life of
> me I can't remember how or in what program I did it.
>
> Any way does has this command: dir /b MyDirName > temp.txt This
> reads all the names in MyDirName and puts them into a text file. I
> remembered this from the dos days, and looking back in old books. Is ther
> a way in BCB5. thanks for any help.
Technical Details
Message-ID:
<475d70fa$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<475cb157@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,3b8df2facea4c3de X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket> Newsgroups: borland.public.cppbuilder.nativeapi References: <475cb157@newsgroups.borland.com> Subject: Re: Getting directory file names in builder 5 Date: Mon, 10 Dec 2007 12:01:44 -0500 Lines: 82 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-RFC2646: Format=Flowed; Response NNTP-Posting-Host: 71.77.53.190 Message-ID: <475d70fa$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1197306106 71.77.53.190 (10 Dec 2007 09:01:46 -0700) X-Authenticated-User: edmulroy Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail