Thread: Problem with system(""); command!

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Problem with system(""); command!

    Hello,

    I was just writing a program that will rename a file (under windows) but ran into some problems.

    Code:
    system("cd [my directories]"); //dosnt work, windows wont change directory
    system("rename [directory+file] [directory+renamedfile]"); //dosn't work either.
    
    //although,
    //doing this does work
    system("G:"); //where all my files are located under
    Why can't I do a CD to a directory, it says it dosn't exist, but i'll do the same command under the prompt and it works.
    ~guitarist809~

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Because system() is a crummy way of programming.
    Learn to use the API's which underly those operations, say for example the chdir() API.

    > system("cd [my directories]");
    Runs cmd.exe
    cmd.exe changes directory
    cmd.exe exits
    your program is still where it was before you called system()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    OK, the chdir() should work, but I'm still a little confused...

    After i change the directory, if I do a system("rename [myfile] [myNewfile]"); will it still be in the same directory I specified in the chdir() function? or is there a renameFile() function or something?
    ~guitarist809~

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, everything you can do with system() can be done with an API, and SO much more.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Converting MINI-BASIC in MASM over to C++?
    By Paul Panks in forum Projects and Job Recruitment
    Replies: 405
    Last Post: 07-04-2009, 05:41 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. parsing command line problem
    By Newworld in forum C Programming
    Replies: 1
    Last Post: 11-27-2004, 11:11 PM
  5. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM