changing the directory from inside a c program under windows using system command
Asked Answered
E

5

10

I have a problem where I have to run a command prompt command from inside a C program. Here is what I did

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


int main(){



system("cd \Users");
system("Dir");

}

The problem is I want to cd into Users first and then execute the command Dir where as currently the program outputs that it cant find the path specified and the Dir is executed in the parent process directory i-e where the program source file is located.

The actual task I want to do is just want to run a java file from a particular directory from inside a C program. that java file is in C:\Users\Abdullah\pro . My C program's parent directory is C:\Users\Cprog. Please advise on how may I do this

Espouse answered 30/5, 2012 at 7:16 Comment(4)
Please have a look at my answer below. Tell me if it helped or not, or any further questions you may have after looking at the example from Microsoft. Side note: Is it really necessary that the current directory is also changed to that directory?Epididymis
Thank you for your answer. The problem is that the program that i need to execute in the particular directory is to b run using a script. e-g cd into working directory then, run.bat --arg1 arg1 --arg2 arg2 etc. So I can not start a process to do that I suppose :/Espouse
Updated my answer accordingly. See msdn.microsoft.com/en-us/library/windows/desktop/… and the current directory parameter.Epididymis
Thanks Alot for pointing me to this. Much appreciated :)Espouse
E
6

Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.

Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().

For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.

Epididymis answered 30/5, 2012 at 7:42 Comment(0)
N
8

system() starts a new process. This new process changes its current directory, then ends. The current directory of your program's process does not change.

You want chdir() (or _chdir()).

Need answered 30/5, 2012 at 7:18 Comment(5)
Alternatively, he can use a single call to system. Something like system( "cd \Users & dir" ) might work. (I think & is the character required. It would be ; under Unix.)Dade
...or just system("Dir \Users");Dagnah
Nothing working :/ or may be I am too dumb. I just want to run a java file from a particular directory from inside a C program. that java file is in C:\Users\Abdullah\pro . My C program's parent directory is C:\Users\Cprog. Please advise on how may I do thisEspouse
Then... why not just pass that to CreateProcess()?Need
@Abdullah, you should edit your question to include that part about the Java program you want to start.Epididymis
E
6

Your program has some incorrect assumptions. First of all, "cd" and "dir" are not programs, but commands built into the shell, cmd.exe. Second, I suspect you don't need to change the current directory at all.

Either way, since this is a Windows system, I would look at an example on how to start a program with CreateProcess().

For changing the current directory, check out the lpCurrentDirectory parameter of the CreateProcess() call.

Epididymis answered 30/5, 2012 at 7:42 Comment(0)
A
2

For Windows only, there is also this SetCurrentDirectory() function.

Ara answered 30/5, 2012 at 7:23 Comment(0)
E
0

'cd' is NOT a separate executable which you are trying to execute. Normally 'cd' is a shell/command's built-in command. You can NOT execute 'cd' with 'system()' and expect to make it work accordingly in your program. You will have to use 'chdir()' function (or system call) if you want the directory change to be performed inside your program. Consult the manual page for 'chdir()'.

Extract answered 30/5, 2012 at 7:55 Comment(0)
H
0

This should work:

system("dir Users\\whatEverNextFolder > test.txt");
Hedvah answered 28/4, 2015 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.