Thread: the system command

  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391

    the system command

    Hi all.

    If I was to open up a DOS window, and type:

    Code:
    dir "c:\examples\programs\project\*.html"
    I'd get a list of html files in the "project" directory.

    But when I try to get the same results using the system() command:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main( void )
    {
        system("dir c:\examples\programs\project\*.html");
    
        return 0;
    }
    I get the error message "File Not Found".

    I've tried various combinations of:

    Code:
    system("dir ""c:\examples\programs\project\*.html""");
    to no avail, even though they compile OK.

    If I was to use system("dir"), and put the executable into the "projects" folder, everything works fine.

    But how can I use the system() command so that it will give me a listing of any directory that I choose? Is even possible to use literal strings in system()?

    Thanks in advance.
    Last edited by happyclown; 12-31-2008 at 07:50 PM. Reason: another typo

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Try
    Code:
    system("dir c:\\examples\\programs\\project\\*.html");

  3. #3
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Quote Originally Posted by carrotcake1029 View Post
    Try
    Code:
    system("dir c:\\examples\\programs\\project\\*.html");
    I tried that and got the DOS error message:

    "The system cannot find the path specified".



    EDIT:

    In a DOS window, this command works:

    Code:
    dir "c:\examples\programs\project\*.html"
    So I think I would still need the " at the beginning and the end of the path?

    I am using Windows XP, open up a DOS command console to run executables etc.
    Last edited by happyclown; 12-31-2008 at 08:20 PM.

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Well, I just tried it on my Windows Vista box and it worked just fine. I don't have DOS available to test on, sorry.

    I used this:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
        system("dir C:\\Windows\\*.exe");
        return 0;
    }
    And it printed all of the executables in that directory.
    Last edited by carrotcake1029; 12-31-2008 at 08:21 PM.

  5. #5
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Ok, thanks.

    I tried a different path(folder), and it works fine.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If your path has any spaces you would need to put quotes around it, otherwise it should work without them.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Never use system(). It's crappy.
    Use Win32 api.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Alex31 View Post
    Never use system(). It's crappy.
    Use Win32 api.
    system() is portable, although the string you pass to it isn't. The one good thing about system() is that it's easy to use for quick tests, and later, once you know it works, you can switch it to the proper functions, once you figure out what those functions are.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. system command not executing with format specifier
    By ridhamshah in forum C Programming
    Replies: 6
    Last Post: 11-08-2006, 05:58 AM
  2. Help on System Command
    By Smoki in forum C++ Programming
    Replies: 5
    Last Post: 12-14-2005, 06:47 PM
  3. Problem using system() command.
    By mmongoose in forum C++ Programming
    Replies: 2
    Last Post: 08-20-2005, 08:44 PM
  4. System() command opens a dos-box
    By phil_drew in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2004, 07:43 AM
  5. IDEA: Customizeable command system
    By Inquirer in forum Contests Board
    Replies: 1
    Last Post: 10-02-2002, 10:17 PM