system(“cd “) in a C program

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");

Leave a Comment