Этот код MS Windows перечисляет все .txt
файлы в C :. Чтобы получить список всех других файлов, измените strcpy(DirSpec, "c:\\*.txt")
на strcpy(DirSpec, "c:\\*")
.
#include <stdio.h>
#include <stdlib.h>
#define _WIN32_WINNT 0x0501
#include <windows.h>
#define BUFSIZE MAX_PATH
int main(int argc, char *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;
LPSTR DirSpec;
unsigned int nFiles=0;
DirSpec = (LPSTR) malloc (BUFSIZE);
strcpy(DirSpec, "c:\\*.txt");
printf ("Current directory : %s\n\n", DirSpec);
hFind = FindFirstFile(DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("incorrect Handle : %u.\n", GetLastError());
return (-1);
}
else
{
printf ("%s\n", FindFileData.cFileName);
while ( FindNextFile (hFind, &FindFileData) != 0)
{
nFiles++;
printf ("%s\n", FindFileData.cFileName);
}
dwError = GetLastError();
FindClose(hFind);
printf ("\n %d files found.\n\n", nFiles);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile Error.\n", dwError);
return (-1);
}
}
free(DirSpec);
return (0);
}