Create Simple File Explorer in C

Faraz

By Faraz -

Learn how to create a simple file explorer in C programming. Follow this easy guide with step-by-step instructions, code examples, and tips to get started.


create-simple-file-explorer-in-c.webp

Introduction

Creating a simple file explorer in C programming is a great way to learn more about file handling, directory navigation, and user interaction in the C language. A file explorer allows you to view, navigate, and manage files and directories on your computer. In this guide, we'll walk you through the process of creating a basic file explorer in C. This project is perfect for beginners and will help you better understand how files and directories work in C.

Step-by-Step Guide

1. Setting Up Your Development Environment

Before you begin, ensure that you have a C compiler installed on your system. If you're using Windows, you can install MinGW, and for Linux or macOS, GCC is usually pre-installed. You'll also need a text editor like Visual Studio Code, Notepad++, or any IDE you're comfortable with.

2. Understanding the Basics of File Handling in C

File handling in C is done using the standard library functions like fopen, fclose, fread, fwrite, fseek, and others. To navigate directories, you'll use functions like opendir, readdir, and closedir. Understanding these basics is essential for building a file explorer.

3. Starting with a Simple File Structure Display

Begin by writing a simple program that opens a directory and lists all the files and folders inside it. Here's a basic example:

#include <stdio.h>
#include <dirent.h>

// Function to list files and directories in the given path
void listFiles(const char *path) {
    struct dirent *de;  // Pointer for directory entry

    // Open the directory specified by the path
    DIR *dr = opendir(path);

    if (dr == NULL) {  // Check if the directory could be opened
        printf("Could not open directory %s\n", path);
        return;
    }

    printf("\nContents of %s:\n", path);
    printf("----------------------------\n");
    // Read and print all files and directories within the directory
    while ((de = readdir(dr)) != NULL)
        printf("%s\n", de->d_name);

    // Close the directory
    closedir(dr);
}

4. Navigating Between Directories

Next, allow the user to navigate between directories. You can do this by using the chdir function, which changes the current working directory. Here's an example:

#include <stdio.h>
#include <dirent.h>
#include <unistd.h>  // For chdir function

void listFiles(const char *path) {
    struct dirent *de;
    DIR *dr = opendir(path);

    if (dr == NULL) {
        printf("Could not open directory %s\n", path);
        return;
    }

    printf("Contents of %s:\n", path);
    while ((de = readdir(dr)) != NULL)
        printf("%s\n", de->d_name);

    closedir(dr);
}

int main() {
    char path[100];
    printf("Enter the path to navigate: ");
    scanf("%s", path);

    fgets(path, sizeof(path), stdin);
    path[strcspn(path, "\n")] = 0; // Remove the newline character
    listFiles(path);

    return 0;
}

5. Implementing File and Directory Operations

To make your file explorer more functional, add options for creating, deleting, and renaming files and directories. Here’s an example of how to delete a file:

#include <stdio.h>

int main() {
    char filename[100];
    printf("Enter the name of the file to delete: ");
    scanf("%s", filename);
	fgets(filename, sizeof(filename), stdin);
    filename[strcspn(filename, "\n")] = 0;
    if (remove(filename) == 0) {
        printf("File deleted successfully\n");
    } else {
        printf("Could not delete the file %s\n", filename);
    }

    return 0;
}

You can similarly use mkdir to create a directory and rename to rename files and directories.

6. Adding a Simple User Interface

Enhance your file explorer by adding a simple text-based user interface. Provide a menu with options like "View files," "Change directory," "Create file," "Delete file," etc. This will make your file explorer more user-friendly.

#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

// Function to list files and directories in the given path
void listFiles(const char *path) {
    struct dirent *de;  // Pointer for directory entry

    // Open the directory specified by the path
    DIR *dr = opendir(path);

    if (dr == NULL) {  // Check if the directory could be opened
        printf("Could not open directory %s\n", path);
        return;
    }

    printf("\nContents of %s:\n", path);
    printf("----------------------------\n");
    // Read and print all files and directories within the directory
    while ((de = readdir(dr)) != NULL)
        printf("%s\n", de->d_name);

    // Close the directory
    closedir(dr);
}

int main() {
    int choice;
    char path[256];
    char filename[100];

    while (1) {
        // Display the menu
        printf("\n===================================\n");
        printf("|       SIMPLE FILE EXPLORER      |\n");
        printf("===================================\n");
        printf("| 1. View files                   |\n");
        printf("| 2. Change directory             |\n");
        printf("| 3. Create file                  |\n");
        printf("| 4. Delete file                  |\n");
        printf("| 5. Exit                         |\n");
        printf("===================================\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar(); // To consume the newline character after the choice

        switch (choice) {
            case 1:
                // List files and directories in the given path
                printf("\nEnter the directory path: ");
                fgets(path, sizeof(path), stdin);
                path[strcspn(path, "\n")] = 0; // Remove the newline character
                listFiles(path);
                break;

            case 2:
                // Change the current working directory
                printf("\nEnter the path to change directory: ");
                fgets(path, sizeof(path), stdin);
                path[strcspn(path, "\n")] = 0; // Remove the newline character
                if (chdir(path) == 0) {
                    printf("Changed to directory %s\n", path);
                } else {
                    printf("Could not change directory to %s\n", path);
                }
                break;

            case 3:
                // Create a new file
                printf("\nEnter the name of the file to create: ");
                fgets(filename, sizeof(filename), stdin);
                filename[strcspn(filename, "\n")] = 0; // Remove the newline character
                FILE *fp = fopen(filename, "w");
                if (fp) {
                    printf("File created successfully\n");
                    fclose(fp);
                } else {
                    printf("Could not create the file %s\n", filename);
                }
                break;

            case 4:
                // Delete an existing file
                printf("\nEnter the name of the file to delete: ");
                fgets(filename, sizeof(filename), stdin);
                filename[strcspn(filename, "\n")] = 0; // Remove the newline character
                if (remove(filename) == 0) {
                    printf("File deleted successfully\n");
                } else {
                    printf("Could not delete the file %s\n", filename);
                }
                break;

            case 5:
                // Exit the program
                printf("\nExiting the File Explorer. Goodbye!\n");
                exit(0);

            default:
                printf("\nInvalid choice, please try again.\n");
        }
    }

    return 0;
}

Compiling and Running the Program

1. Compiling the Code

To compile the code, open your terminal or command prompt and navigate to the directory where your C file is located. Then, use the following command:

gcc file_explorer.c -o file_explorer

Replace filename.c with the name of your C file. This command will compile your program and create an executable file named file_explorer.

2. Running the Program

After compiling, run the program by entering the following command:

file_explorer

This will launch your simple file explorer. Follow the on-screen instructions to navigate directories, view files, and perform other file operations.

Conclusion

Creating a simple file explorer in C programming is an excellent way to practice file handling and directory navigation. By following the steps outlined in this guide, you can build a basic but functional file explorer that allows you to view, navigate, and manage files and directories. With further enhancements, you can add more features to make your file explorer even more powerful. Keep experimenting with new features and continue learning to improve your C programming skills.

That’s a wrap!

I hope you enjoyed this article

Did you like it? Let me know in the comments below 🔥 and you can support me by buying me a coffee.

And don’t forget to sign up to our email newsletter so you can get useful content like this sent right to your inbox!

Thanks!
Faraz 😊

End of the article

Subscribe to my Newsletter

Get the latest posts delivered right to your inbox


Latest Post

Please allow ads on our site🥺