- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Microsoft
- >
- Write C program to detect and List files in window
Operating System - Microsoft
1753706
Members
4548
Online
108799
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-20-2006 10:04 PM
тАО07-20-2006 10:04 PM
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2006 09:59 AM
тАО07-21-2006 09:59 AM
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
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
Archie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2006 10:00 AM
тАО07-21-2006 10:00 AM
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;
}
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("(
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
Archie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-21-2006 08:50 PM
тАО07-21-2006 08:50 PM
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
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
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP