Why This Program is not Changing Directory from C:/ to H: Drive or D: Drive.


using namespace std;

int main(int a,char *b[])
    int option;

    cout<<endl<<"Enter 2 for 2nd Semster Programs "<<endl;
    cout<<endl<<"Enter 3 for 3rd Semster Programs "<<endl;
        system("explorer .");
    else if(option==3)
        system("explorer .");
        system("explorer c:/");


Recommended Answers

All 3 Replies

This isn't a C++ question, this is a Windows Explorer command line arguments question. Try opening a command line (i.e. regular old command line completely independent of your C++ program) and entering the commands you did. They will not switch drives there either. Try this to get to D:\directory1\directory2:

explorer /select D:\directory1\directory2
explorer /root D:\directory1\directory2

Stick either of those in the quotes of your "system" command and see what happens. In short, your program works, you just have the wrong thing in the quotes of your system command, which is not a C++ issue.

On a side-note, there are downsides to using "system". Google "C++ do not use system"

FInally, make sure you #include the cstdlib library for "system". It apparently worked without it for you, but it did not for me.

commented: Thanks for your reply.I applied all these commands on cmd and it works there that's y i am using them in program. +0

Whoops, forgot two things. One, I forgot the comma, so it needs to be this from the command line:

explorer /root, D:\directory1\directory2

Note the comma after root.

Also, at the COMMAND LINE, the above would be what you wanted. However, in your C++ program, remember to escape the backwards slash, so you want this in your C++ program:

system("explorer /root, D:\\directory1\\directory2");

When programming in C++, you type "\\" for a literal backslash. A SINGLE backslash means to not treat the following character literally, so "\n" means the single character newline, not two characters (backslash followed by n). Since backslash is a special character, when you need text to be interpreted literally as a single backslash, you use two of them: "\\". So the code

system("explorer /root, D:\\directory1\\directory2");

means "Run the program "explorer" and give it the arguments "/root, D:\directory1\directory2"

Note that it strips out the first backslash and treats the second backslash literally. So, once again, your C++ code will be:

system("explorer /root, D:\\directory1\\directory2");

And again, experiment by using "select" rather than "root". I'm pretty sure "root" is what you want. Note that "root" does not mean the superuser "root" here. It means "go to the root of directory D:\directory1\directory2, as in GO INSIDE the directory. "select" would go to D:\directory1 and HIGHLIGHT directory2.

commented: Thank for your reply.. +0

Thanks for your reply.I applied all these commands on cmd and it works there that's y i am using them in program

I sort of missed part of the problem, I believe. You are trying to execute multiple commands using multiple "system" calls. I believe that is NOT the equivalent of opening a single command line shell, then executing the commands on that command shell. It is more like the equivalent of opening a command line shell, executing the command "d:", then closing the command line shell, opening a new command line shell, then executing the next command, which is to open Windows Explorer from the current directory. The command to change to the D directory was LOST when the first "system" command executed. When you then executed the second "system" command, which was to open Explorer in the current directory, that current directory was no longer the "D:" directory.

That's why it's not working as you thought. You thought it would create ONE shell and execute more than one command in it when in fact it opened up one shell PER command, thus losing previous commands.

At least that's what I THINK happened. Could be wrong. At any rate, if you use the solution I gave you, that's ONE command with arguments so it should work.

commented: Thanks It's very Helpful..Thanks again +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.