Problem with system(""); command!

This is a discussion on Problem with system(""); command! within the C++ Programming forums, part of the General Programming Boards category; Hello, I was just writing a program that will rename a file (under windows) but ran into some problems. Code: ...

  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
    32,848
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,848
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11: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, 06:41 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21