#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    320
    Rep Power
    13

    Changing directory, running another program


    abc.exe exists in C:\test\abc. abc.exe runs and returns to the C:\test\abc directory.

    This is causing a problem with abc.exe; I need my proposed program to run abc.exe and then change directory to C:\test before exiting.

    It's something that could be done in two lines with a dos bat file, however my program must be a .exe.

    I tried making a console app that basicall did this:

    // test9.cpp : Defines the entry point for the console application.
    //

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

    int main(int argc, char* argv[])
    {
    system("c:\\test\\abc.exe");
    system("cd c:\\test");
    return 0;
    }


    This calls abc.exe, but doesn't change the current directory to c:\test.

    any assistance/pointers are appreciated. Thanks.

    TIA
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    it does not change the directory because the system call spawns another process which has this working dir then, but immediately quits.

    try "chdir()" instead. did this help?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    320
    Rep Power
    13
    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
    	system("C:\\windows\\notepad.exe");
    	chdir("C:\\windows\\desktop");
    	return 0;
    }
    Compiling...
    test9.cpp
    D:\c\test9\test9.cpp(12) : error C2065: 'chdir' : undeclared identifier
    Error executing cl.exe.

    test9.exe - 1 error(s), 0 warning(s)
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    191
    #include <unistd.h>

IMN logo majestic logo threadwatch logo seochat tools logo