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 graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond