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

Hello all -

I know this seems to be a very basic question, but I haven't been able to make it work. I'm trying to run a .bat file on the web. I've tried using both system and exec, but the .bat file does not run. The statement currently looks like this:

system ("d:\webmaster\cgi-bin\comb.bat");

Does anyone have any idea what I am missing?

Thanks!
E

Replies are listed 'Best First'.
Re: How do I run a .bat file?
by blakem (Monsignor) on Oct 24, 2001 at 02:38 UTC
    Try one of these....
    system ("d:\\webmaster\\cgi-bin\\comb.bat"); system ("d:/webmaster/cgi-bin/comb.bat"); system ('d:\webmaster\cgi-bin\comb.bat');

    -Blake

Re: How do I run a .bat file?
by blackmateria (Chaplain) on Oct 24, 2001 at 02:39 UTC
    Etelka, you need to escape the backslashes (i.e. double them up):

    system ("d:\\webmaster\\cgi-bin\\comb.bat");

    Otherwise perl sees "\cg" and "\co" from "\cgi-bin\comb.bat" and interprets them as control characters. That'll give you get a "file not found" error instead of running the batch file like you expect. Hope this helps!

Re: How do I run a .bat file?
by tommyw (Hermit) on Oct 24, 2001 at 02:39 UTC

    In general, on a windows machine, you need to specify a binary executable. So you can't run .bat files directly.

    Fortunately, command.com does know how to run .bat files. So if you change your call to:

    system ("command.com /c d:\webmaster\cgi-bin\comb.bat");
    you should be alright (subject to having to specify where command is (hopefully you don't need to).

    Then again, the above answers might be the right ones: I don't use perl on windows

      Perl already does that for you and does a better job of it. (:

              - tye (but my friends call me "Tye")
        True, but this bat file is the only way we've can run this series of executables that expect input that we have. Even with all of the great information I've gotten here, I haven't been able to make it work. Perhaps I'll tell the engineer who wants me to write this that it can't be done with this particular file! ;-)

        Thanks again -
        ~E
      I believe you need to use "cmd.exe" if it is Win NT or W2K. "command.com" is for the Win9X platforms.

      Thanks for the reply. So the system command can run more than one command?

      (I actually tried it, and it didn't so I'm beginning to think there is something wrong with the machine or the perl install. But, I figure that no matter what's wrong with the machine/install, I'm still going to have to figure out how to do it...)

      E

        Nope, it's running one command, with two parameters (/c and blah.bat). type

        command /?
        at the command prompt to see the details

Re: How do I run a .bat file?
by Starky (Chaplain) on Oct 24, 2001 at 10:27 UTC
    A more sophisticated way to do it in Windows is with Win32::Process::Create:
    Win32::Process::Create( $obj, 'c:\path\to\batch\file\myfile.bat', 'myfile arg1 arg2', 0, NORMAL_PRIORITY_CLASS, 'c:\this\will\be\the\working\directory' ) || die(Win32::FormatMessage( Win32::GetLastError() )); $obj->Wait(2000); $obj->GetExitCode($code); print STDERR "The exit code is $code."; if ($code == 259) { warn "The batch file did not terminate after 2 seconds!"; }
    $obj and $code are simply scalars that will be populated with the object used to get the exit code and the exit code.

    Using Win32::Process will give you more reliable access to exit codes than system() or backticks. I believe that you can now get exit codes with the normal method with backticks (don't quote me) but that used to not be the case. The module also give you greater flexibility in your system call.

    Check out the Win32::Process docs for more information.

    Note that it's been a couple years since I used Windows (I started using Linux a couple years ago and haven't looked back), so my knowledge of the Win32::Process modules is a little rusty. However, I do recall that at the time, Win9x and WinNT differed in their behavior, so I had to write conditionals to account for those differences in many cases. Go figure.

    Hope this helps :-)

Re: How do I run a .bat file?
by hopes (Friar) on Oct 24, 2001 at 02:41 UTC
    In add to the others answers, if the script don't work it will probably be a "permission" problem.
    .bat is suppossed to be a valid extension and should execute the batch file if you had the rigth permissions and exists the correct association in the web server
    Hopes
Re: How do I run a .bat file?
by Etelka (Novice) on Oct 31, 2001 at 02:11 UTC
    Thanks for everyone's help - I did finally get it to work.

    The problems turned out to be that I needed backslashes instead of forward slashes, and I needed to fix my bat file to run in a different place on the server (the directory wasn't in the path...)

    It turned out that I was doing the perl right, just not everything else.... ;-)

    ~E
      I'm using the cronW script, and running it on windows sbs 2011 I found out that cmd.exe /c C:\massweb\eeb\cron.bat is the way to get it to execute a batch file.