C Programming: File Listing Utility – Directory Structure

File Listing Utility - Directory Structure

The C program is a file listing utility that displays information about files in a specified directory. It uses command line arguments to display the listing in a tree style. The program iterates through the files in the specified directory and shows all the files and folders in specific directory with the file size, last modified date and other file attributes.

This program works like the dir /p command of the Windows operating system or ls command in Unix.

Key Components of the Program:

Header Files:

  • <stdio.h>: Standard input/output functions.
  • <stdlib.h>: General utility functions, including memory allocation.
  • <string.h>: String manipulation functions.
  • <ctype.h>: Functions for character classification.
  • <direct.h>: Functions for directory manipulation.

Structures and Union:

  • struct ftime: Represents the timestamp of a file, breaking down the time and date components.
  • union f: A union of an array and the ftime structure, used for extracting file timestamp information.

Functions:

  • getdiskno(const char a[]): Determines the current disk number based on the provided drive letter or the current working drive.
  • ret(int a): Retrieves information about available space on a disk.
  • countdig(long int a): Counts the number of digits in a given number.

Main Function:

  • Accepts command-line arguments (argc and argv).
  • Processes command line arguments to determine the directory to list and optional filtering based on file attributes.
  • It uses _findfirst and _findnext functions to iterate through files in the specified directory.
  • Prints details such as filename, size, timestamp, etc.
  • Pauses and clears the screen after every 20 files for better readability.

Command-line Parameters:

The program accepts the following command line parameters:

  1. Directory Path:
    • If provided, specifies the directory to list files from.
    • Example: program.exe C:\Users\Username\Documents
  2. Filter Mode:
    • Optional parameter to filter files based on attributes.
    • Options:
      • r: Read-only files.
      • h: Hidden files.
      • d: Directories.
      • s: System files.
      • a: Archive files.
      • l: Volume labels.
    • Example: program.exe C:\Users\Username\Documents r

Source code of the Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <direct.h>
#include <io.h>

struct ftime {
    unsigned ft_tsec : 5;
    unsigned ft_min : 6;
    unsigned ft_hour : 5;
    unsigned ft_day : 5;
    unsigned ft_month : 4;
    unsigned ft_year : 7;
};

union f {
    int a[2];
    struct ftime b;
} c;

int getdiskno(const char a[]) {
    if (a[1] == ':') {
        return(tolower(a[0]) - 'a' + 1);
    }
    return (_getdrive() + 1);
}

long int ret(int a) {
    struct _diskfree_t b;
    _getdiskfree(a, &b);
    return (long int)((long int)(b.avail_clusters) * (long int)(b.sectors_per_cluster) * (long int)(b.bytes_per_sector));
}

int countdig(long int a) {
    int count = 0;
    if (a == 0) return 1;
    while (a != 0) {
        a = a / 10;
        count++;
    }
    return count;
}

int main(int argc, char* argv[]) {
    int count = 0, mode = 0, i;
    long int totsize = 0;
    struct _finddata_t a;
    intptr_t handle;
    char string[15];

    if (argc >= 2) {
        strcpy(string, argv[1]);
    }
    else {
        strcpy(string, "*.*");
    }

    if (argc >= 3) {
        if (strcmp(argv[2], "r") == 0)
            mode = _A_RDONLY;
        if (strcmp(argv[2], "h") == 0)
            mode = _A_HIDDEN;
        if (strcmp(argv[2], "d") == 0)
            mode = _A_SUBDIR;
        if (strcmp(argv[2], "s") == 0)
            mode = _A_SYSTEM;
        if (strcmp(argv[2], "a") == 0)
            mode = _A_ARCH;
    }

    if ((handle = _findfirst(string, &a)) == -1) {
        printf("\nCould not find a file to match your criterion\n");
        exit(0);
    }

    count++;
    printf("\nListing files...\n");
    totsize += a.size;
    printf("\n%s", a.name);
    for (i = 0; i < (15 - strlen(a.name)); i++)
        printf(" ");
    printf("%ld", a.size);
    c.a[0] = a.time_access;
    for (i = 0; i < (10 - countdig(a.size)); i++)
        printf(" ");
    if (c.b.ft_day < 10) printf("0");
    printf("%u-", c.b.ft_day);
    if (c.b.ft_month < 10) printf("0");
    printf("%u-%u", c.b.ft_month, c.b.ft_year + 1980);
    printf("  ");
    if (c.b.ft_hour < 10) printf("0");
    printf("%u:", c.b.ft_hour);
    if (c.b.ft_min < 10) printf("0");
    printf("%u:", c.b.ft_min);
    if (c.b.ft_tsec < 10) printf("0");
    printf("%u", c.b.ft_tsec);

    while (_findnext(handle, &a) == 0) {
        count++;
        printf("\n%s", a.name);
        totsize += a.size;
        for (i = 0; i < (15 - strlen(a.name)); i++)
            printf(" ");
        printf("%ld", a.size);
        c.a[0] = a.time_access;
        for (i = 0; i < (10 - countdig(a.size)); i++)
            printf(" ");
        if (c.b.ft_day < 10) printf("0");
        printf("%u-", c.b.ft_day);
        if (c.b.ft_month < 10) printf("0");
        printf("%u-%u", c.b.ft_month, c.b.ft_year + 1980);
        printf("  ");
        if (c.b.ft_hour < 10) printf("0");
        printf("%u:", c.b.ft_hour);
        if (c.b.ft_min < 10) printf("0");
        printf("%u:", c.b.ft_min);
        if (c.b.ft_tsec < 10) printf("0");
        printf("%u", c.b.ft_tsec);

        if (count % 20 == 0) {
            if (count != 20) printf("\n");
            printf("\n\nListed %d files", count);
            printf("\nPress a key for the next page....");
            getch();
            system("cls");
        }
    }

    _findclose(handle);

    printf("\n\nListed %d files\n%ld bytes in all\n", count, totsize);
    printf("\n%ld bytes available in drive %c:", ret(getdiskno(string)), 'a' + getdiskno(string) - 1);

    return 0;
}
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post