Organizing files manually can be frustrating. You open a folder, and it’s full of documents, images, videos, and random downloads all mixed together. Clicking through and dragging files one by one quickly becomes boring — and error-prone.
With C++17, we have a new ally: the <filesystem> library. It gives us the ability to look inside folders, check file names and extensions, and decide what belongs where. This is exactly what we need to build our file organizer.
In this chapter, we’ll learn:
- How to detect a file’s type by looking at its extension.
- How to group files into categories (e.g., Images, Docs, Videos).
- How to move them into the right subfolders safely.
The goal is not just to write some code, but to create the first real version of our organizer: a tool that takes a messy folder and shows order, clarity, and discipline.
Step 1 – Defining categories
Every file has an extension: .jpg, .pdf, .mp3, .zip …
By creating a simple map of extension → category, we can tell our program how to classify files.
Example:
.jpg,.png,.gif→ Images.pdf,.docx,.txt→ Docs.mp3,.wav→ Audio.zip,.rar→ Archives- Anything unknown → Other
We’ll use an unordered_map in C++ to store this mapping.
#include <string>
#include <unordered_map>
#include <algorithm>
const std::unordered_map<std::string, std::string> EXT_TO_DIR = {
{".jpg","Images"}, {".png","Images"}, {".gif","Images"},
{".pdf","Docs"}, {".docx","Docs"}, {".txt","Docs"},
{".mp3","Audio"}, {".wav","Audio"},
{".zip","Archives"}, {".rar","Archives"}
};
This is just a start — you can expand the map whenever you want.
Step 2 – Scanning a folder
The <filesystem> library lets us scan a folder easily. For each file we find, we check its extension and decide the category.
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string path = "./folder_to_organize";
for (const auto& entry : fs::directory_iterator(path)) {
if (entry.is_regular_file()) {
std::string ext = entry.path().extension().string();
std::cout << "File: " << entry.path().filename()
<< " | Extension: " << ext << std::endl;
}
}
return 0;
}
Run this code and watch your folder being “listed.” You’ll see each file’s name and extension printed out.
Step 3 – Deciding where to move files
Once we know the extension, we look it up in our EXT_TO_DIR map:
std::string category = "Other";
auto it = EXT_TO_DIR.find(ext);
if (it != EXT_TO_DIR.end()) {
category = it->second;
}
Now we know the category (like “Images” or “Docs”) and can decide where the file should go.
Step 4 – Moving files safely
Finally, we use <filesystem> to create a subfolder if it doesn’t exist, and then move the file there.
fs::path targetDir = path / category;
fs::create_directories(targetDir); // safe: does nothing if it already exists
fs::rename(entry.path(), targetDir / entry.path().filename());
Important: Always test on a copy of your files first. For extra safety, you can add a dry-run mode where the program only prints what it would do without actually moving anything.
End of Chapter Reflection
At this point, you’ve written a program that doesn’t just look at files — it starts to bring order into chaos. From a messy folder, you can automatically separate documents, images, and music with the press of a key.
For beginners, this is a big milestone: you’re not just printing “Hello World” anymore. You’re controlling your digital space with C++. That’s power, discipline, and the first real taste of automation.
