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

I am very very new to Perl and CGI and I have no idea of how to accomplish what I need. I need to create a cgi script that will execute a bat file that is on my server. Now for a little background on what this does. There is no web page involved. The cgi is on a server that offers video files to a roku (streaming media player). Once I select a channel to watch, the cgi script will be called which in turn executes a bat file. The bat file holds information that will convert the video stream from one format to one recognized by the roku. The bat file must start before the next line can be executed. I just need to know how to write a cgi that can start the bat file and with a variable passed to the bat file. The variable will be the particular video file selected. Please any help greatly appreciated. Maybe one day I will know enough to help somebody else.

  • Comment on How do I execute a bat file from cgi script?

Replies are listed 'Best First'.
Re: How do I execute a bat file from cgi script?
by Anonymous Monk on Dec 09, 2013 at 01:34 UTC
Re: How do I execute a bat file from cgi script?
by Random_Walk (Prior) on Dec 09, 2013 at 13:36 UTC

    Do read the above links, they are good. Do you want to wait for that .bat file to finish before your script continues, or do you want the script to carry on once it starts the .bat. If the latter, what should happen if the script finishes before the .bat it launched? Here is some totally untested code to give you some ideas

    use strict; # Always start a Perl script with these two without the +m use warnings; # there are many subtle ways to shoot yourself in the f +oot use CGI; # Load the module to handle CGI stuff my $cgi = CGI->new(); # get a CGI object. # Point to the batfile, notice we can use / as a path separator in Per +l # even when we are in a Windows world. my $batfile = 'c:/program files/mycode/batman.bat'; # I guess you are going to get the parameter from the CGI invocation my $param = $cgi->param('some_field'); # run the command and wait for it to complete. Catch STDERR my $result = `$batfile $param 2>&1`; # This probably won't work as we did not set up an http header, you ma +y want # to use logfile, send output to the cgi invoker, or just throw it aw +ay Print "the command: $batfile $param\nGasve the result: $result";

    This code will be saved in your cgi-bin and invoked with a url like:

    http://yourserver/cgi-bin/thiscode.pl?some_field=fieldOfDreams

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      Thank you all for the direction. I have read the majority of the links and started to get an idea. The bat file will continue to run after the script ends. The bat file will end when the current channel is exited. Other than starting the bat file, the script will really do nothing else. The script will be called from code on the roku, once the script starts the bat file, the roku executes it's next line which is to start streaming the file created by the bat file. Thanks again