12


I'm trying to use the system() function in a C program.
For example, I tried to create a directory on my desktop, using the system() function.
My code:

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

int main(void)
{
   system("cd c:\\Users\\USER\\Desktop");
   system("mkdir test");
   return 0;
}

When I run this code, a directory is created, but not on my desktop. It is created in my project directory.
Why is this happens?
Can I use the cd command in the system() function? If not, is there an replacement to the cd command that will work with system()?

I'm using Windows OS. I'm trying to use system() from a C program as I use cmd program.
I know that I can create the directory using WinAPI without any problem. I don't want to use WinAPI, my question is how can I make it work using system().

Programmer
  • 750
  • 2
  • 9
  • 17
  • In regards to your last paragraph: why? What purpose does it serve to use `system()` instead of standard functions? What are you trying to achieve that you believe can only be achieved by using `system()`? Or is this just for playing around? – siride Jul 13 '14 at 15:45

3 Answers3

15

The changed directory only lasts for the duration of the system command. The command starts a separate program, which inherits its current directory from your program, but when that program exits its current directory dies with it.

You can use && to join the commands together, and it will work:

system("cd /D C:\\Users\\USER\\Desktop && mkdir test");

I also added the /D switch, or the CD command would not change drive letter if it were called from a different drive.

However, mkdir is perfectly capable of accepting a full path, so you could simply do:

system("mkdir C:\\Users\\USER\\Desktop\\test");
Boann
  • 48,794
  • 16
  • 117
  • 146
  • 2
    Thanks. So, I need to think about it as the system() function "creates" a cmd program, and after the function returns the cmd program it created exits? I know the above description isn't really accurate, but does what I'm saying make sense? – Programmer Jul 13 '14 at 11:58
  • 1
    @Programmer It is accurate. That's literally what it does. – Boann Jul 13 '14 at 11:59
  • OK, Thank you very much. :) I got my answer. – Programmer Jul 13 '14 at 12:00
10

When you say system("some shell command");, the program spawns a shell to run the command. The shell has its own idea of the current directory, separate from your program's. The shell cds to the directory just as you asked it to, and then dies, leaving your process's CWD unaffected.

You could simply say _chdir("c:\\Users\\User\\Desktop"); to set the current directory before running the "mkdir" command. The shell that spawns to run it will then inherit your program's current directory and make the folder in the right place.

(For that matter, you could say _mkdir("test") as well, and stop using system unnecessarily. You should only reach for system when you're trying to do something that's worth running an external program / shell for.)

cHao
  • 84,970
  • 20
  • 145
  • 172
1

You have to perform both the commands on a single line like this,

system("cd c:\\Users\\USER\\Desktop && mkdir test");
Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42