Operating System - Microsoft
1752434 Members
5874 Online
108788 Solutions
New Discussion юеВ

Re: Write C program to detect and List files in window

 
Henry Chua
Super Advisor

Write C program to detect and List files in window

Hi,

Is there anyway to write a C program to detect the represent of a file and to list files in a directory in windows?

Best regards
Henry
3 REPLIES 3
Arch_Muthiah
Honored Contributor

Re: Write C program to detect and List files in window

Henry,

If you want to use BAT program, it is easy.
The following two lines BAT code will create a .txt file with list of files from any drive or folder you pass. This output file can be fopened from your C program and can be listed.
=======
@echo off
dir "D:\" > C:\list_of_program_files.txt
========

Archunan
Regards
Archie
Arch_Muthiah
Honored Contributor

Re: Write C program to detect and List files in window

Henry,

I found this for you from the net, it won't go into the directory loop. But you can do little modification to do that too.

Here is the code which will work exactly like "dir" command.

#include
#include

int EnumerateFiles(char start_dir[], BOOL use_start_dir)
{
WIN32_FIND_DATA find_data;
HANDLE find_h;
DWORD find_ret;
SYSTEMTIME creation_time, access_time;
int ret;
char *file_name, atts, dir[MAX_PATH];

if(use_start_dir)
{
strcpy(dir, start_dir);
//if(strrchr(start_dir, '.') == NULL) strcat(dir, "*.*");
}
else strcpy(dir, "*");

ret = 0;
find_h = FindFirstFile(dir, &find_data);
if(find_h != INVALID_HANDLE_VALUE)
{
puts("( - Directory, - System File, - Read Only, - Hidden)");
puts("Created\t\tLast Accessed\tAttributes\tName\n");

do
{
/*Keep track of how many attributes for alignment purposes*/
atts = 0;
if(find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) atts++;
if(find_data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) atts++;
if(find_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) atts++;
if(find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) atts++;

FileTimeToSystemTime(&find_data.ftCreationTime, &creation_time);
FileTimeToSystemTime(&find_data.ftLastAccessTime, &access_time);
if((file_name = strrchr(find_data.cFileName, '\\')) != NULL) file_name++;
else file_name = find_data.cFileName;

printf("%s%d/%s%d/%d\t%s%d/%s%d/%d\t%s%s%s%s%s\t%s\n", /*so messy!*/
((creation_time.wDay < 10) ? "0" : ""), creation_time.wDay,
((creation_time.wMonth < 10) ? "0" : ""), creation_time.wMonth,
creation_time.wYear, ((access_time.wDay < 10) ? "0" : ""), access_time.wDay,
((access_time.wMonth < 10) ? "0" : ""), access_time.wMonth, creation_time.wYear,
((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? "" : ""),
((find_data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) ? "" : ""),
((find_data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) ? "" : ""),
((find_data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ? "" : ""),
((atts > 2) ? "" : "\t"), /*alignment purposes...*/
file_name);

if(ret) break;
if(!FindNextFile(find_h, &find_data)) find_ret = GetLastError();
else find_ret = 0;
}
while(find_ret != ERROR_NO_MORE_FILES);
FindClose(find_h);
}
else ret = 1;

return ret;
}

int main(int argc, char *argv[])
{
int ret;
char *error = 0;

if(argc == 1) ret = EnumerateFiles(0, FALSE);
else if(argc == 2) ret = EnumerateFiles(argv[1], TRUE);
else EnumerateFiles(0, FALSE);

switch(ret)
{
case 0 : break;
case 1 : error = "Invalid directory or no specified files within such a directory"; break;
default : error = "Unknown error"; break;
}
if(error) puts(error);

return 0;
}
Regards
Archie
Henry Chua
Super Advisor

Re: Write C program to detect and List files in window

Hi Archunan,

Thanks for the info, actually what i was intending to achieve is to able to copy files with the same name over the a directory without having to delete the old files. For example, the new files will be renamed to file01, file02 and so forth. Is there a easy way to to this?

Best regards
Henry