Using _chdir(), ctime()/ctime_s(), _findfirst(), _findnext(), _findclose()
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Printing file names and attributes in the current directory
To show: Using _chdir(), ctime()/ctime_s(), _findfirst(), _findnext(), _findclose()
/* The use of the 32-bit _find* functions to print a list of all files (and their attributes) in the current directory. */
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <time.h>
#include <direct.h>
#include <conio.h>
#include <ctype.h>
#define SIZE 50
int main(void)
{
// char path[50] = "C:\\WINNT\\System32\\config"; - Windows 2000
char path[50] = "C:\\Windows\\System32\\config";
struct _finddata_t c_file;
intptr_t hFile;
char buf[SIZE];
printf("Change to %s\n", path);
if(_chdir(path))
{
printf("Unable to locate the directory: %s\n", path);
exit(1);
}
else
/* Find first in the current directory */
hFile = _findfirst("*.*", &c_file);
/* List the files... */
printf("Listing of files in the directory %s\n\n", path);
printf("\nRDO HID SYS ARC FILE DATE %20c SIZE\n", ' ');
printf("--- --- --- --- ---- ---- %20c ----\n", ' ');
printf((c_file.attrib & _A_RDONLY) ? " Y " : " N ");
printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N ");
printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N ");
printf((c_file.attrib & _A_ARCH) ? " Y " : " N ");
// unsafe version is ctime()
ctime_s(buf, SIZE, &(c_file.time_write));
printf(" %-30s %.20s %9ld\n", c_file.name, buf, c_file.size);
/* Find the rest of the files */
while(_findnext(hFile, &c_file) == 0)
{
printf((c_file.attrib & _A_RDONLY) ? " Y " : " N ");
printf((c_file.attrib & _A_SYSTEM) ? " Y " : " N ");
printf((c_file.attrib & _A_HIDDEN) ? " Y " : " N ");
printf((c_file.attrib & _A_ARCH) ? " Y " : " N ");
// unsafe version is ctime()
ctime_s(buf, SIZE, &(c_file.time_write));
printf(" %-30s %.20s %9ld\n", c_file.name, buf, c_file.size);
}
_findclose(hFile);
return 0;
}
Output example:
Change to C:\Windows\System32\config
Listing of files in the directory C:\Windows\System32\config
RDO HID SYS ARC FILE DATE SIZE
--- --- --- --- ---- ---- ----
N N N N . Mon Oct 23 06:47:59 0
N N N N .. Mon Oct 23 06:47:59 0
N N N Y AppEvent.Evt Sun Dec 10 19:10:07 327680
N N N Y default Sun Dec 10 19:10:26 262144
N N Y Y default.LOG Sun Dec 10 21:10:47 1024
N N N Y default.sav Sun Jul 17 04:56:00 94208
N N N Y MyAppTes.evt Thu Aug 11 18:26:11 65536
N N N Y MyCustLo.evt Thu Aug 11 19:21:28 65536
N N N Y SAM Sun Dec 10 19:10:26 262144
N N Y Y SAM.LOG Sun Dec 10 21:10:17 1024
N N N Y SecEvent.Evt Sun Jul 17 04:57:11 65536
N N N Y SECURITY Sun Dec 10 19:10:26 262144
N N Y Y SECURITY.LOG Sun Dec 10 21:20:15 1024
N N N Y software Sun Dec 10 19:10:26 39059456
N N Y Y software.LOG Sun Dec 10 22:21:11 24576
N N N Y software.sav Sun Jul 17 04:56:00 659456
N N N Y SysEvent.Evt Sun Dec 10 21:10:20 262144
N N N Y system Sun Dec 10 21:10:04 4980736
N N Y Y system.LOG Sun Dec 10 21:16:35 1024
N N N Y system.sav Sun Jul 17 04:56:00 897024
N N N N systemprofile Thu Jul 21 18:19:06 0
N N Y Y TempKey.LOG Sun Jul 17 04:55:57 1024
N N N Y userdiff Sun Jul 17 04:56:01 262144
N N Y Y userdiff.LOG Sun Jul 17 04:56:01 1024
Press any key to continue . . .