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

Dr. Yilmaz Yoru has 33+ 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, and Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

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

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++