C++C++17Introduction to C++Learn C++

Learn To Use Directory Operations In Your C++ App

Do you want to use DOS commands to operate on directories or folders? Do you know that you can use system commands in C++ to operate on folders? How can we get our C++ App to list files of a folder, how we can copy, rename, delete, or move directories? Can we set the access permissions of a directory by using system commands in C++? We will explain all the answers to these questions below.

C++ is a great programming language that allows you to reach every part of your device or operating system. The file system of an operating system is very important, and you need to know well both File System features of the operating system and features of your programming language. The time invested pays dividends in being able to write powerful optimized code.

How can we use system commands in our C++ app?

On Windows and some other operating systems you can use the System() command to use System Commands like cd, mkdir rmdir commands etc. For example we can create a folder by using mkdir command and we can copy folder to another folder by using xcopy command and we can remove a folder by using rmdir command. See the example below,

How to list all the files in a directory with C++

In C++, Std library has great features, classes and methods, and there is a directory_iterator(), we can use this to get all file names in that path.

We can use this directory_iterator() with a for() loop as in given example below,

We can use recursive_directory_iterator() to list all files in that folder.

How to use C++ to check if a folder already exists and create it if it doesn’t

We can use filesystem::exists(path+dir) to check a directory to see if it exists in a file system. We can create directory by using filesystem::create_directory(path+dir)

We can also use create_directories(“D:\\myfolder\\mysubfolder”); to create all folders in that given path if they are do not exists.

How to copy a directory in C++ with options

How to use C++ to set permissions on a folder

How to rename a folder in C++

Why not download a free trial copy of C++ Builder today and try out these examples?


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap