Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
No such thing as a small change
 
PerlMonks  

How to execute dos commands from Perl

by Poornima (Initiate)
on May 31, 2012 at 05:40 UTC ( #973431=perlquestion: print w/ replies, xml ) Need Help??
Poornima has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to run a .bat file from Perl script. I use system() or exec to execute the .bat file, but the file runs in Unix shell and not on dos prompt. How do i make the bat file to execute in dos prompt?

Comment on How to execute dos commands from Perl
Re: How to execute dos commands from Perl
by NetWallah (Monsignor) on May 31, 2012 at 06:12 UTC
    In a WindowsXP/cygwin environment, I was able to get this to work:

    perl -e "print for qx|c:/windows/system32/cmd.exe /c test.bat| "

    Note the use of the full path to cmd.exe, using FORWARD SLASHES.

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Hi, Thanks for your quick response. But i need to call up a .exe from within a perl script and after execution the command should return to next step in perl script. Can you please help me out on it?
        If you want to execute a .bat under Windows, then you need to have a script file that Windows understands.

        I suggest that you type: "help start"
        to see the options for that to make a more complex Windows .bat file.

        To capture the output of a shell command (Windows or not) in Perl in the most simple way, use "backticks".

        #!/usr/bin/perl -w use strict; my $result1 = `dir`; print $result1; my $result2 = `dir *.txt`; print $result2;
        Yes - the 'qx' and backticks provide that functionality. The perl script waits for the command to complete, then executes the next perl command.
        use strict; use warnings; my $externalExe="C:/path/to/myprog.exe"; print "Starting external program...\n"; print for qx|$externalExe 2>&1|; # Executes the program, and prints it +'s output print "Program $externalExe run completed.\n";

                     I hope life isn't a big joke, because I don't get it.
                           -SNL

Re: How to execute dos commands from Perl
by GrandFather (Cardinal) on May 31, 2012 at 06:54 UTC

    You need to get your story straight. Are you trying to run a DOS/Windows batch file, or are you trying to run a DOS/Windows executable? What operating system are you using? What have you tried and how did it fail?

    Pretend we know nothing at all about what you are trying to do nor about the system you are trying to do it on. That shouldn't be too hard because it is actually the case - no pretence needed!

    True laziness is hard work
      Hi, I am trying to execute a .NET exe from Perl script which should execute on dos prompt adn return the control back to perl script. The .NET exe would be sending fax to the users adn create a .done file, which would be read from the perl script after the execution.
        Then how come you say it was executed in a UNIX shell? Are you using Cygwin? Are you trying to execute a .Net program on UNIX?

        Just to be clear, by "dos prompt" and "dos commands" do you mean the Windows cmd.exe shell?

        IMHO, if you want to execute a .net program on windows, you should use someting like this:

        my $CmdNgen = "$fw20cmd\\ngen.exe"; my $ParmNgenPause = "ngen queue pause"; my $rc; $rc = ProcessItWin( $CmdNgen, $ParmNgenPause ); sub ProcessItWin { my ( $ProcessCmd, $ProcessParam ) = @_; my $ProcessObj = undef; #print "Start $ProcessCmd $ProcessParam\n"; my $rc = Win32::Process::Create( $ProcessObj, $ProcessCmd, $Pr +ocessParam, 0, NORMAL_PRIORITY_CLASS, q{.} ) or $rc = ErrorReport($ProcessCmd); my $pid = $ProcessObj->GetProcessID(); $ProcessObj->Wait(INFINITE); return $rc; } sub ErrorReport { my $cmd = shift; print "\n *** Error CreateProcess: Command=$cmd, ", Win32::For +matMessage( Win32::GetLastError() ), "\n"; return 0; }

        Also please read perldoc Win32::Process

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://973431]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2013-06-19 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many continents have you visited?









    Results (666 votes), past polls