J
jacob navia
I am updating the examples about C programming for lcc-win, and I have
developed a small example that lists the files in a given directory. It
is very primitive, but it is just an example ofreusable code that you
can change and/or incoporate into larger programs.
Please feel free to comment, propose changes, etc.
-----------------------------------------------cut here
/* Print all the files in a directory that match a certain pattern.
demonstrates: printf, FindFirstFile,FindNextFile,FindClose,
FileTimeToLocalFileTime,FileTimeToSystemTime
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
static void printfile(WIN32_FIND_DATA *pdata,unsigned long long *pSize,
unsigned *nrOfDirs,unsigned *nrOfFiles,unsigned attrib);
static char *months[] = { "???",
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static int DecodeOptions(char *options)
{
int result = 0;
if (*options == '/' || *options == '-')
options++;
strlwr(options);
switch (*options) {
case 'A':
case 'a':
options++;
while (*options) {
if (*options == 'd') {
result |= FILE_ATTRIBUTE_DIRECTORY;
options++;
}
if (*options == 'h') {
result |= FILE_ATTRIBUTE_HIDDEN;
options++;
}
if (*options == 's') {
result |= FILE_ATTRIBUTE_SYSTEM;
options++;
}
}
break;
}
return result;
}
int main(int argc,char *argv[])
{
HANDLE handle;
unsigned long long totalSize=0;
unsigned int nrOfDirs=0;
unsigned int nrOfFiles=0;
WIN32_FIND_DATA data;
char *pattern;
int idx=1;
unsigned attrib = 0;
if (argc < 2) {
pattern = "*.*";
}
else {
if (argv[1][0]=='/' || argv[1][0]=='-') {
attrib=DecodeOptions(argv[1]);
idx++;
}
pattern = argv[idx];
if (pattern == NULL)
pattern = "*.*";
}
memset(&data,0,sizeof(data));
/* find the first file that matches */
if ((handle = FindFirstFile(pattern,&data)) ==
INVALID_HANDLE_VALUE) {
printf("No \"%s\" files found\n",pattern);
return(-1);
}
/* print the pattern we received */
printf("Listing of\t%s\n\n",pattern);
/* print the first file specs */
printfile(&data,&totalSize,&nrOfDirs,&nrOfFiles,attrib);
/* loop finding all files that match */
while (FindNextFile(handle,&data)) {
printfile(&data,&totalSize,&nrOfDirs,&nrOfFiles,attrib);
}
if (GetLastError() != ERROR_NO_MORE_FILES) {
printf("Unknown error %d\n",GetLastError());
}
FindClose(handle);
if ((nrOfFiles+nrOfDirs) > 1) {
if (nrOfDirs>0)
printf("\tDirs:%d ",nrOfDirs);
if (nrOfFiles>0)
printf("files:%d ",nrOfFiles);
if (totalSize > 0)
printf(" Total (bytes):\t%'lld\n",totalSize);
}
return 0;
}
/* print the file specs and update some global counts */
static void printfile(WIN32_FIND_DATA *pdata,unsigned long long *pSize,
unsigned *nrDirs,unsigned *nrOfFiles, unsigned attrib)
{
FILETIME localFileTime;
SYSTEMTIME SystemTime;
long long siz;
int isDir=0;
/* ignore the . and .. entries */
if (!strcmp(pdata->cFileName,".") || !strcmp(pdata->cFileName,".."))
return;
if (attrib && (attrib&pdata->dwFileAttributes)== 0)
return;
/* print some attributes */
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("%-30s",pdata->cFileName);
if (strlen(pdata->cFileName) > 29)
printf("\n%36s"," <DIR>");
else
printf(" <DIR>");
*nrDirs += 1;
isDir=1;
}
else {
printf("%-36s",pdata->cFileName);
if (strlen(pdata->cFileName) > 35)
printf("\n%36s"," ");
*nrOfFiles+=1;
}
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
printf("H");
else printf(" ");
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
printf("S");
else printf(" ");
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
printf("R");
else printf(" ");
printf(" ");
/* print the file size. */
siz = ((long long) pdata->nFileSizeHigh << 32LL)|
(long long)pdata->nFileSizeLow;
if (isDir == 0) printf("%'12llu ",siz);
else printf("%13s"," ");
*pSize += siz;
/* transform the file creation time to local file time */
FileTimeToLocalFileTime(&pdata->ftLastWriteTime,&localFileTime);
/* transform the local file time into a system time */
FileTimeToSystemTime(&localFileTime,&SystemTime);
/* print the time */
printf("%4d %3s %02d %02d:%02d:%02d",
SystemTime.wYear,months[SystemTime.wMonth],
SystemTime.wDay,SystemTime.wHour,SystemTime.wMinute,
SystemTime.wSecond);
printf("\n");
}
developed a small example that lists the files in a given directory. It
is very primitive, but it is just an example ofreusable code that you
can change and/or incoporate into larger programs.
Please feel free to comment, propose changes, etc.
-----------------------------------------------cut here
/* Print all the files in a directory that match a certain pattern.
demonstrates: printf, FindFirstFile,FindNextFile,FindClose,
FileTimeToLocalFileTime,FileTimeToSystemTime
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
static void printfile(WIN32_FIND_DATA *pdata,unsigned long long *pSize,
unsigned *nrOfDirs,unsigned *nrOfFiles,unsigned attrib);
static char *months[] = { "???",
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static int DecodeOptions(char *options)
{
int result = 0;
if (*options == '/' || *options == '-')
options++;
strlwr(options);
switch (*options) {
case 'A':
case 'a':
options++;
while (*options) {
if (*options == 'd') {
result |= FILE_ATTRIBUTE_DIRECTORY;
options++;
}
if (*options == 'h') {
result |= FILE_ATTRIBUTE_HIDDEN;
options++;
}
if (*options == 's') {
result |= FILE_ATTRIBUTE_SYSTEM;
options++;
}
}
break;
}
return result;
}
int main(int argc,char *argv[])
{
HANDLE handle;
unsigned long long totalSize=0;
unsigned int nrOfDirs=0;
unsigned int nrOfFiles=0;
WIN32_FIND_DATA data;
char *pattern;
int idx=1;
unsigned attrib = 0;
if (argc < 2) {
pattern = "*.*";
}
else {
if (argv[1][0]=='/' || argv[1][0]=='-') {
attrib=DecodeOptions(argv[1]);
idx++;
}
pattern = argv[idx];
if (pattern == NULL)
pattern = "*.*";
}
memset(&data,0,sizeof(data));
/* find the first file that matches */
if ((handle = FindFirstFile(pattern,&data)) ==
INVALID_HANDLE_VALUE) {
printf("No \"%s\" files found\n",pattern);
return(-1);
}
/* print the pattern we received */
printf("Listing of\t%s\n\n",pattern);
/* print the first file specs */
printfile(&data,&totalSize,&nrOfDirs,&nrOfFiles,attrib);
/* loop finding all files that match */
while (FindNextFile(handle,&data)) {
printfile(&data,&totalSize,&nrOfDirs,&nrOfFiles,attrib);
}
if (GetLastError() != ERROR_NO_MORE_FILES) {
printf("Unknown error %d\n",GetLastError());
}
FindClose(handle);
if ((nrOfFiles+nrOfDirs) > 1) {
if (nrOfDirs>0)
printf("\tDirs:%d ",nrOfDirs);
if (nrOfFiles>0)
printf("files:%d ",nrOfFiles);
if (totalSize > 0)
printf(" Total (bytes):\t%'lld\n",totalSize);
}
return 0;
}
/* print the file specs and update some global counts */
static void printfile(WIN32_FIND_DATA *pdata,unsigned long long *pSize,
unsigned *nrDirs,unsigned *nrOfFiles, unsigned attrib)
{
FILETIME localFileTime;
SYSTEMTIME SystemTime;
long long siz;
int isDir=0;
/* ignore the . and .. entries */
if (!strcmp(pdata->cFileName,".") || !strcmp(pdata->cFileName,".."))
return;
if (attrib && (attrib&pdata->dwFileAttributes)== 0)
return;
/* print some attributes */
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("%-30s",pdata->cFileName);
if (strlen(pdata->cFileName) > 29)
printf("\n%36s"," <DIR>");
else
printf(" <DIR>");
*nrDirs += 1;
isDir=1;
}
else {
printf("%-36s",pdata->cFileName);
if (strlen(pdata->cFileName) > 35)
printf("\n%36s"," ");
*nrOfFiles+=1;
}
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
printf("H");
else printf(" ");
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
printf("S");
else printf(" ");
if (pdata->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
printf("R");
else printf(" ");
printf(" ");
/* print the file size. */
siz = ((long long) pdata->nFileSizeHigh << 32LL)|
(long long)pdata->nFileSizeLow;
if (isDir == 0) printf("%'12llu ",siz);
else printf("%13s"," ");
*pSize += siz;
/* transform the file creation time to local file time */
FileTimeToLocalFileTime(&pdata->ftLastWriteTime,&localFileTime);
/* transform the local file time into a system time */
FileTimeToSystemTime(&localFileTime,&SystemTime);
/* print the time */
printf("%4d %3s %02d %02d:%02d:%02d",
SystemTime.wYear,months[SystemTime.wMonth],
SystemTime.wDay,SystemTime.wHour,SystemTime.wMinute,
SystemTime.wSecond);
printf("\n");
}