I need to run a .bat script from PHP. The .bat script needs to contain file names that are PHP variables. How would I go about doing this inside PHP?
Thanks!
I need to run a .bat script from PHP. The .bat script needs to contain file names that are PHP variables. How would I go about doing this inside PHP?
Thanks!
Use fopen(), fwrite() and fclose() to create the .bat file, then use shell_exec() to execute it. See here:
fopen: https://www.php.net/manual/en/function.fopen.php
fwrite: https://www.php.net/manual/en/function.fwrite.php
fclose: https://www.php.net/manual/en/function.fclose.php
shell_exec: http://php.net/manual/en/function.shell-exec.php
=================================================================================
EDIT:
Or, if it's always the same number of php variables being passed and they are scalar, you could just create one .bat file that accepts command-line parms, then pass the scalar variables in your shell_exec.
If your bat file is just taking name of a file and some basic parameters, then you just shell_exec() with the proper parameters filled in.
Batch file command-line params are referenced %1 %2 etc. if your batch file is foo.bat and it contains:
notepad.exe %1
you can call foo.bat textfile.txt from the command line.
To run it from php, you construct your command line as a string, then call shell_exec using it
To expand on Jonathan's answer, you do not have to execute the batch file with PHP. You may also use PHP only to generate a configuration batch file. You can then call this in your batch file to access all the variables you need. This is very flexible, and keeping the variables in a separate batch file keeps your main code clean.
To illustrate, you would need three files:
batch.bat Your main batch filesetup.php The configuration generatorconfig.bat The configuration batch file (created by setup.php) First off, we will create the setup.php script which generates the config.bat file. It could look like this:
// Create a list of all variable names we want
// to use in the bat file, with their values.
// Note that these can come from anywhere - from
// a database, other files, remote API calls, etc.
$batVariables = array(
'InfoFile' => 'info.txt',
'TrackListing' => 'tracklist.txt',
'AuthorBio' => 'author.txt',
'MainTrack' => 'audio.mp3'
);
// create the code for the configuration
// batch file.
$batCode =
'@echo off'.PHP_EOL.
':: AUTOMATICALLY GENERATED FILE, DO NOT EDIT'.PHP_EOL.
PHP_EOL;
// add all variables
foreach($batVariables as $varName => $value)
{
$batCode .= 'set '.$varName.'='.$value.PHP_EOL;
}
// save the configuration file
file_put_contents('config.bat', $batCode);
This will generate a batch file that looks like this:
@echo off
:: AUTOMATICALLY GENERATED FILE, DO NOT EDIT
set InfoFile=info.txt
set TrackListing=tracklist.txt
set AuthorBio=author.txt
set MainTrack=audio.mp3
Next, add these two lines in your batch file (you will have to make sure that the php executable is accessible in your PATH system variable):
:: Generate the configuration batch file
php setup.php
:: Load the configuration with all variables
call config.bat
This will generate a fresh configuration each time the main batch file is used.
By loading the config.bat with the call command, the variables will be available directly in the scope of the main batch file. This means you can start using them as usual, like this for example:
echo Using the information file %InfoFile%.
Beyond this, you may wish to add a few error checks in case the configuration file cannot be generated. One simple way would be to ensure the file exists:
IF EXIST "config.bat" (
call "config.bat"
) ELSE (
echo The configuration file does not exist.
echo.
pause
exit
)