Thread: opening a batch file

  1. #1
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18

    opening a batch file

    Hi, im relatively new to C++ and recently i have been writing a program that requires the use of a batch file. However, I'm not so sure how i can get the program to run the batch file. I tried using:
    system ("C://WINDOWS//system32//cmd.exe C://Documents and Settings//User1//desktop//file.bat");
    however that line simply opens up cmd.exe and i want the program to open it up without requiring the user to enter the name of the file.
    Can anyone help?

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    just:

    system ("C:/Documents and Settings/User1/desktop/file.bat");

    also, you only need to double your slashes when they are backslash( \ ) not forward slashes like above

  3. #3
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18
    system only works for .exe and i did do double forwards slashes to replace back-slashes in the above example

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>system only works for .exe
    It works for any valid DOS command string.

    **EDIT**
    >>i did do double forwards slashes to replace back-slashes in the above example

    No, if it's forward slashes then you don't need double. If it's back-slashes, you need double-backslash. For example:
    Code:
    cout << "//\n/\n\\\n" << flush;
    Output:
    Code:
    //
    /
    \
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Like a normal DOS command with spaces, you may also have to quote your command:
    Code:
    system ("\"C:/Documents and Settings/User1/desktop/file.bat\"");

  6. #6
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18
    im still having trouble. i read all the posts here and put the following into my program
    system ("C:\\Documents and Settings\\User 1\\Desktop\\blah.bat")
    however it still hasnt run the file blah.bat in my desktop.
    The batch file contained the command
    shutdown -s
    which should shutdown my computer. I ran the file to check that it worked and it does. I then checked that i got the path to it right and i did, so why isnt it working?

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Because, as anonytmouse has mentioned, you need quotes around the string if there's spaces in it.
    Code:
    system ("\"C:\\Documents and Settings\\User 1\\Desktop\\blah.bat\"")
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. batch file introduction in multiprocesses program
    By c_geek in forum C Programming
    Replies: 0
    Last Post: 03-27-2008, 01:52 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. opening a batch file from c++
    By twomers in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2005, 04:11 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM