Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a batch file (.bat) in a C program?

Tags:

c

gcc

batch-file

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?

like image 459
niko Avatar asked Jul 13 '11 13:07

niko


People also ask

What is batch file in 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.

What program runs .bat files?

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.

Can you run .bat files?

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.


2 Answers

Drop the start. It's a cmd.exe thing. Just run system("omanam.bat");.

like image 192
eran Avatar answered Sep 28 '22 10:09

eran


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.
like image 39
Vishwanath Dalvi Avatar answered Sep 28 '22 10:09

Vishwanath Dalvi