nerdmike
asked on
ANSI C - list directory contents
Hi
Can anyone provide an example in ANSI C of how to list contents of a directory
Thanks
-Mike
Can anyone provide an example in ANSI C of how to list contents of a directory
Thanks
-Mike
ASKER
Those were all unix specific solutions
I need ANSI C *ONLY*
I need ANSI C *ONLY*
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sucks
I'm writing code for windows, but I try to keep it ANSI wherever possible.
An example using the win32 api would be nice.
-mike
I'm writing code for windows, but I try to keep it ANSI wherever possible.
An example using the win32 api would be nice.
-mike
I can understand your frustration ... As it turns out, ANSI C supports almost nothing to interact with the system and windows is not POSIX compliant
https://www.experts-exchange.com/questions/20909231/How-can-I-list-all-File-Name-in-specific-path.html
https://www.experts-exchange.com/questions/20761334/Directory-browsing-with-C-in-VS-on-Windows.html
Cheers!
https://www.experts-exchange.com/questions/20909231/How-can-I-list-all-File-Name-in-specific-path.html
https://www.experts-exchange.com/questions/20761334/Directory-browsing-with-C-in-VS-on-Windows.html
Cheers!
Here's a simple directory routine that works well with win32. It prints all of the attributes available to you (via the _finddata_t structure) and the time of the last write to the file. There are two other times available on NTFS systems they are create time and the last access time. I have comments in the code that show how to read them.
Based on the _A_SUBDIR attribute you might want to build two lists (one of files and one of directorys). Also if you are displaying the list directly to the user you might consider not displaying _A_HIDDEN and _A_SYSTEM files, although I always prefer to see them.
Hope this helps
-david
#include <io.h>
#include "stdio.h"
#include "time.h"
void listDir(char *path)
{
struct _finddata_t fileInfo;
intptr_t fPtr;
char attribs[8];
char timeBuff[32];
printf(" checking path : %s\n",path);
if((fPtr = _findfirst(path, &fileInfo )) == -1L)
return;
else
{
do{
// decode the archive flag
attribs[0] =( fileInfo.attrib & _A_ARCH ) ? 'A' : '.';
// decode the read only flag
attribs[1] =( fileInfo.attrib & _A_RDONLY ) ? 'R' : '.';
// decode the hidden file flag
attribs[2] = ( fileInfo.attrib & _A_HIDDEN ) ? 'H' : '.';
// decode the System file flag
attribs[3] =( fileInfo.attrib & _A_SYSTEM ) ? 'S' : '.';
// for readability
attribs[4] = '-';
// is this a subdirectory 'D' or a File 'F'
attribs[5] = (fileInfo.attrib & _A_SUBDIR ) ? 'D' : 'F';
attribs[6] = 0x00;
// get the time of the last write to this file
ctime_s( timeBuff, 30, &fileInfo.time_write );
// other times available on non FAT systems are create time
// fileInfo.time_access and fileInfo.time_write these are -1 on FAT disks
printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
}while(_findnext(fPtr, &fileInfo) == 0);
_findclose(fPtr);
}
}
main(int argc, char *argv[])
{
listDir("c:\\*.*");
}
Based on the _A_SUBDIR attribute you might want to build two lists (one of files and one of directorys). Also if you are displaying the list directly to the user you might consider not displaying _A_HIDDEN and _A_SYSTEM files, although I always prefer to see them.
Hope this helps
-david
#include <io.h>
#include "stdio.h"
#include "time.h"
void listDir(char *path)
{
struct _finddata_t fileInfo;
intptr_t fPtr;
char attribs[8];
char timeBuff[32];
printf(" checking path : %s\n",path);
if((fPtr = _findfirst(path, &fileInfo )) == -1L)
return;
else
{
do{
// decode the archive flag
attribs[0] =( fileInfo.attrib & _A_ARCH ) ? 'A' : '.';
// decode the read only flag
attribs[1] =( fileInfo.attrib & _A_RDONLY ) ? 'R' : '.';
// decode the hidden file flag
attribs[2] = ( fileInfo.attrib & _A_HIDDEN ) ? 'H' : '.';
// decode the System file flag
attribs[3] =( fileInfo.attrib & _A_SYSTEM ) ? 'S' : '.';
// for readability
attribs[4] = '-';
// is this a subdirectory 'D' or a File 'F'
attribs[5] = (fileInfo.attrib & _A_SUBDIR ) ? 'D' : 'F';
attribs[6] = 0x00;
// get the time of the last write to this file
ctime_s( timeBuff, 30, &fileInfo.time_write );
// other times available on non FAT systems are create time
// fileInfo.time_access and fileInfo.time_write these are -1 on FAT disks
printf("%-32s %s %9.ld %s",fileInfo.name, attribs, fileInfo.size , timeBuff);
}while(_findnext(fPtr, &fileInfo) == 0);
_findclose(fPtr);
}
}
main(int argc, char *argv[])
{
listDir("c:\\*.*");
}
ASKER
Will that work on NTFS only or any win32 system?
This will work on any windows system FAT or NTFS. If you use the extra dates ( fileInfo.time_access and fileInfo.time_write ) they won't work on a FAT system. But as written it will work on any Windows system.
-dave
-dave
Note that you must provide a file spec (like *.* or *.ext) as well as a path or when you call the routine or it won't find any files.
not too fab at C myself, but here's some past solutions:
https://www.experts-exchange.com/questions/11668118/Listing-contents-of-a-directory-and-determining-status.html
https://www.experts-exchange.com/questions/10205792/How-to-get-a-list-of-files-in-directory.html
https://www.experts-exchange.com/questions/20926775/directory-listing.html
Hope that helps :-)
Rob.