简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM