Dmitri |
Отправлено: 09.06.2003, 17:30 |
|
admin@localhost
Группа: Модератор
Сообщений: 110
|
Как считать имена файлов из заданного каталога и вывести их, например, в TMemo? Т.е. мне надо, чтобы пользователь задал каталог, программа просканировала его и вывела имена файлов (без расширений) в TMemo (или в многомерный массив).
|
|
Dr.Phoenix |
Отправлено: 09.06.2003, 18:22 |
|
Дежурный стрелочник
Группа: Участник
Сообщений: 48
|
FindFirstFile + FindNextFile
|
|
Dmitri |
Отправлено: 11.06.2003, 12:28 |
|
admin@localhost
Группа: Модератор
Сообщений: 110
|
Что-то я не нашел таких функций. В каком help'нике? Можно подробнее?
|
|
pasha |
Отправлено: 11.06.2003, 13:59 |
|
Дежурный стрелочник
Группа: Участник
Сообщений: 62
|
The FindFirstFile function searches a directory for a file whose name
matches the specified filename. FindFirstFile examines subdirectory names
as well as filenames.
HANDLE FindFirstFile(
LPCTSTR lpFileName, // pointer to name of file to search for
LPWIN32_FIND_DATA lpFindFileData // pointer to returned information
);
Parameters
lpFileName
Windows 95: Points to a null-terminated string that specifies a valid
directory or path and filename, which can contain wildcard characters (*
and ?). This string must not exceed MAX_PATH characters.
Windows NT: Points to a null-terminated string that specifies a valid
directory or path and filename, which can contain wildcard characters (*
and ?).
There is a default string size limit for paths of MAX_PATH characters. This
limit is related to how the FindFirstFile function parses paths. An application
can transcend this limit and send in paths longer than MAX_PATH
characters by calling the wide (W) version of FindFirstFile and
prepending "\\?\" to the path. The "\\?\" tells the function to turn off path
parsing; it lets paths longer than MAX_PATH be used with FindFirstFileW.
This also works with UNC names. The "\\?\" is ignored as part of the path.
For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private",
and "
\\?\UNC\bill_g_1\hotstuff\coolapps" is seen as "\\bill_g_1
\hotstuff\coolapps".
lpFindFileData
Points to the WIN32_FIND_DATA structure that receives information about
the found file or subdirectory. The structure can be used in subsequent
calls to the FindNextFile or FindClose function to refer to the file or
subdirectory.
Return Values
If the function succeeds, the return value is a search handle used in a
subsequent call to FindNextFile or FindClose.
If the function fails, the return value is INVALID_HANDLE_VALUE. To get
extended error information, call GetLastError.
Remarks
The FindFirstFile function opens a search handle and returns information
about the first file whose name matches the specified pattern. Once the
search handle is established, you can use the FindNextFile function to
search for other files that match the same pattern. When the search
handle is no longer needed, close it by using the FindClose function.
This function searches for files by name only; it cannot be used for
attribute-based searches.
See Also
FindClose, FindNextFile, GetFileAttributes, SetFileAttributes, WIN32_FIND_DATA
---------------------
Пример из программы:
CODE |
...
SetCurrentDirectory(Data->TempPath);
h=FindFirstFile("*.*",&FD);
if(h==INVALID_HANDLE_VALUE) i=0; else i=1;
do {
if(!i) break;
if(FD.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {
i=FindNextFile(h,&FD);
continue;
}
p=FD.cFileName;
strlwr(FD.cFileName);
while(*p!=0&&*p!='.') p++;
if(*p=='.') p++;
if(!strcmp(p,"db")||(*p=='x')||(*p=='y')||(*p=='p'&&*(p+1)=='x'))
DeleteFile(FD.cFileName);
i=FindNextFile(h,&FD);
} while(i);
FindClose(h);
...
|
Кстати, можно использовать и FindFirst
CODE |
TStringList* me = new TStringList();
//---------------------------------------------------------------------------
__fastcall TFAbout::TFAbout(TComponent* Owner)
: TForm(Owner)
{
// находим все музыкальные файлы mp3 для формы "О программе"
// в текущей директории и помещаем их в me
AnsiString currDir = GetCurrentDir();
TSearchRec sr;
if(FindFirst(currDir+"\\"+"*.mp3", faAnyFile, sr) == 0)
{
me->Add(currDir+"\\"+sr.Name);
while (FindNext(sr) == 0) me->Add(currDir+"\\"+sr.Name);
}
FindClose(sr);
//-----
}
//------------------------------------------------------------------
// соответственно найти все файлы *.*
|
А описание FindFirstFile в Helpe есть, по крайней мере в C++Builder 6
(оттуда и взято)
|
|
|