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

Learn To Use Windows DOS Commands In A C++ App

Learn To Use Windows DOS Commands In A C++ App

Do you know why we use Command Prompt or Power Shell? Can we use Windows DOS Commands in a C++ app and in other operating systems? In this post, we will explain how you can run Windows DOS commands in C++.

Apple DOS was the first family of Disk Operating Systems (DOS) for the first Apple II series between 1978 to 1983. Apple DOS has three major releases: DOS 3.1, DOS 3.2, and DOS 3.3. IBM licensed and re-released PC DOS 1.0 in 1981 for use in its x86 PCs. MS-DOS (Microsoft Disk Operating System) was another DOS initially developed in parallel. These two products diverged after twelve years, in 1993, with recognizable differences in compatibility, syntax, and capabilities.

DOS (Disk Operating System) is a command and text-based operating system that primarily developed for x86 personal computers (now it supports both 32bit and 64bit operating systems). It is found that using graphics is makes a lot of things easier and better than writing commands in a console. That resulted with the development of Mac-OS, Amiga-OS, Windows, and other UI-based operating systems. After many years of graphical user interfaces, GUIs, we still have Command Prompt or Power Shell which runs these DOS commands. Mac-OS is changed its base to Linux, but it has also about the same DOS commands on Linux.

When we type commands into the command prompt on Windows, COMMAND.COM executable parse the input line and it matches a command name to a built-in command or to the name of an executable program file or batch file on disk. Some C and C++ compilers can be used by the DOS commands which means you don’t need a UI-based operating system to run your commands.

Today Windows is still evolving and has progressed in graphical sophistication. Yet we can still use DOS commands on Windows. Some of these commands are very useful to create a folder or to copy or move files. There might be other console-based commands, programming languages, and similar beneficial console applications. All of these kinds of commands can be run programmatically by C++ code. In this post, we will explain how to run DOS commands.

How to use system commands in a C++ app

In C++, we can run system commands – in Windows these are MS-DOS commands – by using the system() C++ command. It is very easy to use, just write your command as text in the system command. For example cls command which clears DOS screen, we can run this in C++ as below,

In some console applications, clearing screen may be useful to output time based or loop based variables. For example this simple example clears the screen and writes count.

This example creates a new directory by using mkdir command and it copies x folder to y folder by the xcopy command.

As you see we can easily reach and use DOS commands in the latest C++ versions. C++ Builder and other CLANG compilers supports this . Note that sometimes using dos command may help you much to use 3rd party commands. For example you can use console based zip , rar, unrar commands to pack or unpack your application files, data files etc.. You can also run your other console applications that you develop in C++.

Try these examples for yourself – download a free trial of C++ Builder.

close

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
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Join Two Wide Strings In Modern C++ Software

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

How To Use The All-Powerful swscanf function In Your C++ App

C++ComponentsLanguage FeatureLearn C++

How To Use A Sharpen Effect in C+ Software On Windows?

C++C++11C++14C++17Introduction to C++Learn C++

What Is The Erase WString Method In A Modern C++ App