Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Executing DOS shell commands via system()

by Grygonos (Chaplain)
on Jun 06, 2003 at 13:41 UTC ( [id://263655]=perlquestion: print w/replies, xml ) Need Help??

Grygonos has asked for the wisdom of the Perl Monks concerning the following question:

Brothers & Sisters.... I'm currently trying to execute DOS commands such as erase and copy via a call like the folowing
@args = ('erase ' , $filename); system(@args) || die "failed to delete";
well then I realized that wouldn't work because erase is part of cmd.exe and not an actual program itself. I also tried executing cmd.exe and passing erase and the filename as a paramete... but alas to no avail... I can make a batch file and execute that.. but that's extremely un-monkly... any ideas?

Replies are listed 'Best First'.
Re: Executing DOS shell commands via system()
by cbro (Pilgrim) on Jun 06, 2003 at 13:45 UTC
    You should use the Perl functions unlink() to delete a file and copy() (From the File::Copy module) to copy a file.
      wow... showing the inexperience huh... hehe thanks :)
        And so appeared, perlmonks, to help us all out. Everybody is inexperienced at some subset of every language.
Re: Executing DOS shell commands via system()
by crouchingpenguin (Priest) on Jun 06, 2003 at 14:13 UTC

    cbro was spot on. Also, concerning your use of system... This is probably what you want/mean:

    system(@args) == 0 || die "Failed to delete: $!";

    See system concerning return value.


    cp
    ----
    "Never be afraid to try something new. Remember, amateurs built the ark. Professionals built the Titanic."
Re: Executing DOS shell commands via system()
by Jenda (Abbot) on Jun 06, 2003 at 14:49 UTC
    • If you are using the system( @args) style you should NOT include any spaces at the end of the command name or parameters.
    • If the erase is a command implemented within cmd.exe all you have to do is to call the cmd.exe:
      @args = ('cmd.exe', '/c', 'erase', $filename);

    Of course in this case unlink($filename) would be better.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

      @args = ('cmd.exe', '/c', 'erase', $filename);

      If Win32 had "real"™ command-line argument processing, then that wouldn't work. For example, in Unix you have to use:

      @args = ( 'sh', '-c', "erase $filename" );
      and this is important in case you want to quote arguments to be given to the internal command. But going very far down that road gets rather complicated quickly so I'll leave that for another time.

      The reason your code works is because, in Win32, system(@args) is (quite unfortunately) exactly the same as system("@args") because Win32 only deals with command lines while Unix (and C) want to deal with lists of command arguments. This can make getting the proper arguments passed in quite difficult, especially since it also means that each program parses its own command line and so you often have to prepare/quote your arguments different for different commands.

      Also note that Perl will do the "cmd /c" trick for you automatically (if it can't find a "erase.*" executable in your PATH).

                      - tye
Re: Executing DOS shell commands via system() (done)
by tye (Sage) on Jun 06, 2003 at 20:22 UTC

    FYI, Perl already does that for you!

    C:\>set prom PROMPT=$P$G C:\>perl -e "system('set prom')" PROMPT=$P$G C:\>perl -e "system('set.exe prom')" Environment variable .exe not defined
    That last example is in there to show you that is isn't running some SET.EXE but is really running cmd.exe and passing it the command "set prom".

                    - tye

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://263655]
Approved by kelan
Front-paged by castaway
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-18 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found