Run batch file from C++ code

Hi All,

I want to run a batch file from specific environment in my sample C++ code.
Bellow are the steps:

1. open a environment specific command prompt
2. then need to run a batch file.

Could anyone please help me here?


Regards,
Akash
Environment programs can be invoked with std::system() of the <cstdlib> header.

Running a batch file would at my environment be:
1
2
3
4
5
6
#include <cstdlib>

int main()
{
    std::system("sh my_batch_file");
}
Last edited on
The phrase “batch file” typically means “on Windows.”

The system() call specifically starts an instance of sh on *nixen, and cmd.exe on Windows, passing your string as argument. Hence:

    /bin/sh -c your_string
    %ComSpec% /C your_string
 

Obviously, this means that the operating environment invoked by system() is OS-dependent. Some users my (unwisely) substitute another shell for /bin/sh or cmd.exe. If you wish to be extra pedantic, you can specify the shell as nuderobmonkey did — just be path-specific so that you don’t suffer from path injection vulnerabilities:

    std::system( "/bin/csh -c my_shell_file.csh" );    // Yes, the (in)famous C-shell

Here’s something to play with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>

namespace fs = std::filesystem;

int main()
{
  // Create an executable batch file (Windows)
  #ifdef _WIN32
  const char* batch_file_name = "a.bat";
  {
    std::ofstream batch_file( batch_file_name );
    batch_file << 
      "@echo off\n"
      "echo I am a batch file!\n"
      "pause\n";
  }
  
  // Create an executable shell file (Linux/POSIX)
  #else
  const char* batch_file_name = "./a.sh";
  {
    std::ofstream batch_file( batch_file_name );
    batch_file <<
      "echo I am a shell file!\n"
      "read -n 1 -s -p \"Press any key...\"\n"
      "echo";
  }
  fs::permissions( batch_file_name, fs::perms::owner_exec, fs::perm_options::add );
  #endif
  
  // Execute the file
  std::cout << "main() before batch call\n";
  std::system( batch_file_name );
  std::cout << "main() after batch call\n";
  
  // Delete the file
  remove( batch_file_name );
}

Compile with something like:

    clang++ -Wall -pedantic-errors -O3 -std=c++17 call-batch-example.cpp

or:

    cl /EHsc /W4 /Ox /std:c++17 call-batch-example.cpp

Have fun!

[edit] Made things more readable, typos, etc
Last edited on
Thank you nuderobmonkey and Duthomhas.
It helps.
you can also use system to make a c++ program that is like a script or batch program, but more powerful. Batch specifically is fairly limited (compared to unix scripts) so making a 'super-batch' in c++ is worth a look if you do a lot of this kind of work.
@jonnin
Cmd.com batch programming has come a long way from its old DOS days. You'd be surprised what it can do. I use it all the time.

Whenever I want to do something more complex than cmd.com can handle, I use Tcl, which has the added bonus of working the same on *nixen and possessing greater powers than bash or csh/zsh/whatever.
I may check that out.
Ive got cygwin hacked into my console, so I can call the unix commands in batch files, allowing me to do most anything I want, but not everyone has or wants all that.
I am not very good at it, to be honest. Most of my batch files do very simple things like launch a program or backup a folder etc.
Yeah, I usually use ConEmu, but I also use the plain Windows Console and MSYS2 as needed.

I also run Fedora using VirtualBox seamless mode, which is very nice.

I keep a a number of GNU tools in C:\Users\Michael\bin, which defaults to the head of my PATH, as well as a number of doskey macros for common things.

My *nix environment is configured similarly with the .profile / .bashrc scripts.

Development is usually a short iterative process between the two OSes using a shared folder and
Registered users can post here. Sign in or register to post.