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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
CC++Introduction to C++Learn C++

Who Wrote The C Programming Language?

CC++Introduction to C++Language FeatureLearn C++

Is C A High Level Programming Language?

CC++Introduction to C++Learn C++

How To Run A C Program in Command Prompt

CC++C++17Learn C++

How To Compile And Run A C Program