CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2003
    Posts
    2

    Red face Execute DOS commands in VC++

    I know that what i am about to ask is pretty stupidious but...

    How can i run a file from within VC++ ?

    More specifically i want to delete a file so i want to run
    "del myfile.txt" just as if i was in DOS shell

    Thanks for reading this!

  2. #2
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661
    does the system function not work?!

    try just this
    Code:
      //you must include stdlib.h before this
      system("del fname.ext"); //executes tis command in the shell
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  3. #3
    Join Date
    Feb 2007
    Posts
    8

    Re: Execute DOS commands in VC++

    I see the thread is some years old, but I hope someone will respond..

    I have the same question as Logikos, and I tryed SeventhStar's solution (and it works)...
    but, if i want to use commands based on variables, like...

    system(exec);
    where exec is a string, problems arrise (because function system is taking only const char* variables..and mine are System::String)

    Any suggestions, plz? (using Visual Studio 2005)

  4. #4
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Execute DOS commands in VC++

    You obviously use managed C++ (.NET).
    So why don't you try System.Diagnostics.Process.Start instead of trying to convert System.String to const char*
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Execute DOS commands in VC++

    In general you are usually better off using an api or class that performs the intended functionality rather than using system commands.

    Why? Because the former allows you to capture detailed error information whereas the latter does not.

  6. #6
    Join Date
    Feb 2007
    Posts
    8

    Re: Execute DOS commands in VC++

    hey, thnx for responding...

    yes, i'm using managed C++

    i didn't knew about the function you mentioned, thnx!

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Execute DOS commands in VC++

    To delete a file, check out File.Delete in msdn.

  8. #8
    Join Date
    Feb 2007
    Posts
    8

    Re: Execute DOS commands in VC++

    me again...

    System.Diagnostics.Process.Start works fine, but it has 1 flaw, it's executing files simultaneously...
    If the code that's following this function depends on this output, there will be an error

    I'm currently using Sleep(10000) (as in sleep for 10 secs), but thats just a workaround.., is there a proper solution to this matter (continue executing code after this function has finished)?

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Execute DOS commands in VC++

    This isn't a flaw in Process.Start because this method is intended to launch a process asynchronously. If you need to launch a process synchronously and read its std output, you need to tell Process.Start to do that.

    Check out my response in the C# program calling jar (or .bat) post. It include a code snippet that redirects std output and waits for the process to end.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured