views:

734

answers:

4

Hi, I have an application and executables. I want my application to run my executables.

The executable files are in a folder, lets say in "c:\sample".

In this directory there is a batch file that calls my exe's. like:

start a1.exe
start a2.exe
start a3.exe

let's name it as startAll.bat

and suppose every exe has a data like a1.dat a2.dat ... and these data files are near this exe's.

I want to call this batch file by my application.

system("c:\\\\sample\\\\startAll.bat");

when I call it like that, command cannot find these exe's.

if I add directory names to batch files, it can not find the data that time. I think it is because of working directory.

start c:\sample\a3.exe

how can I change the working directory before I call this batch file?

or do you suggest anything else?

A: 

Try with double slashes


system("c:\\sample\\startAll.bat");
dudewat
it is not the case.. executables are looking data in application's working directory.. not near this executable..
ufukgun
+5  A: 

Call chdir("C:\\sample") before calling system(...)

Or put a cd command in your batch file

EDIT

Since you're not on C: the first lines of the batch script should be

C:
cd \sample

EDIT2

Using the suggestions made by Johannes and MattH a much better version of the BAT file would start with something like this

setlocal
set BATDIR=%~dp0
cd /d %BATDIR%

Now the bat file will work regardless of the directory it's in as there are no hard coded paths. SETLOCAL is used to avoid side effects from running the script (like changing directory or setting environment variables)

Peter van der Heijden
tried cd and it worked.. thanks for this simple answer :)
ufukgun
actually it did not worked when i call it by system("c:\\sample\\startAll.bat"), it only worked when i call it from cmd by manually.. so it is still not the answer...
ufukgun
Is your executable on the C: drive? If not you should also issue a drive letter change
Peter van der Heijden
yes Peter, you are right. but how can i change my drive letter. it does not accept calling "c:\" in batch file or from system
ufukgun
You can just use `CD /D C:\sample` to simultaneously change drive and directory.
Joey
+5  A: 

The system function can take multiple commands like this:

system("C: && cd \\sample && startAll.bat");

That's neater than changing the current working directory of your calling process, because that can have its own unwanted side-effects.

Depending on how you set up these files, it might be neater than hard-coding a cd command into the batch file.

Edit: I tested this with a C program like this:

#include "stdafx.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
    system("C: && cd \\temp && test.bat");
    return 0;
}

and a batch file called C:\temp\test.bat like this:

echo "Hello world" > pog

and when I run that C program (in a different directory from c:\temp), sure enough a file called pog appears in C:\temp.

RichieHindle
no it did not worked... it did not changed the working directory...
ufukgun
@ufukgun: I tested this and it works perfectly for me. There must be some other reason it's not working for you.
RichieHindle
actually calling cd from batch file also not worked.. i also expected your solution works but it did not.. i did not understand why... :(
ufukgun
+4  A: 

I often prefer to make my batch files ignore the working directory of the caller, if I only intend to work with paths relative to the batch file. You can do this with the following at the start of the file:

SET BATDIR=%~dp0
CD %BATDIR%

Or you can %BATDIR% when calling your external files.

To understand how the above works, take a look here

MattH