GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Mar-2005, 10:50
CJE CJE is offline
New Member
 
Join Date: Mar 2005
Posts: 2
CJE is on a distinguished road

Execute Batch File from within C Code


I am writing a command line C program for the windows 2000/XP environments.

How do I execute/call a batch file from within my c code?

Thanks!
  #2  
Old 08-Mar-2005, 13:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by CJE
I am writing a command line C program for the windows 2000/XP environments.

How do I execute/call a batch file from within my c code?

Thanks!

An easy way is with the system() function.

If your batch file is in the same directory as the executable, or is in the %path% of executables for your machine, just use something like this

CPP / C++ / C Code:
  system("batchfilename.bat xxx yyy");

Where xxx and yyy are arguments to the batch file named batchfilename.bat.

If the batch file is somewhere else, then you have to include the directory path. Suppose you put all of your batch files in directory F:\bin. Then you would have something like this:
CPP / C++ / C Code:
  system("f:\\bin\\batchfilename.bat xxx yyy");

You don't really need the .bat

Anything that you can enter from the command line can be done with the system() call.


Here's a program example:
CPP / C++ / 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;
}

You can try it with this 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.

Regards,

Dave
 
 

Recent GIDBlogReview: Gel laptop cooling pad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:56.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.