CS609 Assignment no 1
Fall2024
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <time.h>
// Function to print file attributes
void printFileAttributes(DWORD attributes) {
if (attributes & FILE_ATTRIBUTE_DIRECTORY) {
printf("Directory ");
} else {
printf("File ");
}
if (attributes & FILE_ATTRIBUTE_READONLY) {
printf("(Read-only) ");
}
if (attributes & FILE_ATTRIBUTE_HIDDEN) {
printf("(Hidden) ");
}
if (attributes & FILE_ATTRIBUTE_SYSTEM) {
printf("(System) ");
}
if (attributes & FILE_ATTRIBUTE_ARCHIVE) {
printf("(Archive) ");
}
}
// Function to display directory contents
void displayDirectoryContents(const char *directoryPath) {
WIN32_FIND_DATA findFileData;
HANDLE hFind;
char searchPath[MAX_PATH];
sprintf(searchPath, "%s\\*", directoryPath);
hFind = FindFirstFile(searchPath, &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf("Error: Unable to open directory. It may not exist or you may lack permissions.\n");
return;
}
int fileCount = 0;
int dirCount = 0;
printf("%-30s %-10s %-20s %-30s\n", "Name", "Size (Bytes)", "Creation Date & Time",
"Attributes");
printf("%-30s %-10s %-20s %-30s\n", "------------------------------", "----------", "-
-------------------", "------------------------------");
do {
SYSTEMTIME creationTime;
FileTimeToSystemTime(&findFileData.ftCreationTime, &creationTime);
printf("%-30s %-10lld %-20d-%02d-%02d %02d:%02d:%02d ",
findFileData.cFileName,
(long long)findFileData.nFileSizeHigh << 32 | findFileData.nFileSizeLow,
creationTime.wYear,
creationTime.wMonth,
creationTime.wDay,
creationTime.wHour,
creationTime.wMinute,
creationTime.wSecond);
printFileAttributes(findFileData.dwFileAttributes);
printf("\n");
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
dirCount++;
} else {
fileCount++;
}
} while (FindNextFile(hFind, &findFileData) != 0);
FindClose(hFind);
printf("\nTotal Files: %d\n", fileCount);
printf("Total Directories: %d\n", dirCount);
}
int main() {
char directoryPath[MAX_PATH];
printf("**************\n");
printf("VU Talk\n");
printf("BC1234555\n");
printf("Scammer404\n");
printf("**************\n");
printf("Enter the path of the directory: ");
fgets(directoryPath, MAX_PATH, stdin);
// Remove the newline character
directoryPath[strcspn(directoryPath, "\n")] = 0;
displayDirectoryContents(directoryPath);
return 0;
}
Follow US: Youtube :
https://www.youtube.com/@vuteamhadi
VU TEAM HADI
03087122922