How to Write a Script C++ to Open a Batch File

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 );                            

0 Response to "How to Write a Script C++ to Open a Batch File"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel