How do I start running a batch file (.bat) from my C program? I used
system("start /B omanam.bat");
but it's not working. How can I make .bat to open through C?
It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF , FOR , and GOTO labels.
To run typical commands, such as to modify system settings, start apps or launch a website, batch files can be run using command prompt. Tools like PowerShell and Bash (Bourne Again Shell) can be used to create advanced batch file scripts.
To run a batch file, move to the directory containing the file and type the name of the batch file. For example, if the batch file is named "hope. bat," you'd type "hope" to execute the batch file.
Drop the start
. It's a cmd.exe thing. Just run system("omanam.bat");
.
If your C executable program and batch file are in same directory then
system("batchfilename.bat arg1 arg2");
where arg1
and arg2
are the arguments for this batch file.
If the batch file is in another directory
system("f:\\bin\\batchfilename.bat arg1 arg2");
where arg1
and arg2
are the arguments for this batch file.
C code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Calling batch file doit.bat\n");
system("doit Hello. theansweris: 42");
printf("Press \'Enter\' to exit the program\n");
getchar();
return 0;
}
Batch file code:
@rem This is the batch file doit.bat
@echo.
@echo.
@echo.
@echo In doit.bat:
@echo.
@echo.
@echo.
@echo argument #1 is ^"%1^"
@echo argument #2 is ^"%2^"
@echo argument #3 is ^"%3^"
@echo.
@echo.
@echo Tttttthat's all, folks!
@echo.
@echo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With