Zero logo

Batch Files - Passing parameters

Passing parameters between batch files and command line applications (scripted in PHP or Perl) is relatively easy. Passing parameters from the parent application to a child application, you can use either environment variables or include parameters in the command line that runs the application. Passing parameters from a child to a parent, either use an intermediary file or use the batch command "FOR /F" to process the output text from the child.

This page covers the above and provides examples that can be used in Uniform Server batch files.

Background

A parent process passes its environment to a child process. A child inherits only a copy (snapshot of the current environment) of the parent environment. This means a child cannot change the original environment.

Although a child process can access and change inherited environment variables, it requires a mechanism to pass any changes back to the parent. Writing new or modified variables to a file is one solution. A parent reads this file and makes changes as required.

An alternative is for the child to output variables to the standard stream (for example, "print" or "echo" uses the standard stream to output to the screen), allowing the parent to intercept and process this data. Although it may sound complicated, the command FOR /F has this functionality built in, hence is easy to implement.

Passing variables to a child

This example demonstrates passing by environment variables and program (application) parameters. Although only one environment variable and one program parameter is used in the demonstration, you are not limited to the number you can pass to an application.

In folder UniServerZ, create two new files named testp1.bat and testp1.php with the following content, replacing phpxx with the php version number selected on your system:

testp1.bat
@echo off
rem ### working directory current folder 
pushd %~dp0

rem ### set variables
set HOME=%cd%
set US_ROOTF=%HOME%
set US_ROOTF=%US_ROOTF:\=/%
set US_PHP_EXE=%HOME%\core\phpxx\php.exe

rem #------Test code -----------

rem ### Set environment variable var1
rem ### Run PHP script with parameter school2
echo Test A)
set var1=school1
%US_PHP_EXE% -n -f testp1.php school2

rem #----End test code ---------

rem ### restore original working directory
popd

pause
testp1.php
<?php
$a = getenv('var1');   // Get environment variable
print ($a . PHP_EOL);  // Output this variable 
$b = $argv[1];         // Get argument1 
print ($b . PHP_EOL);  // Output argument1 
?>
  Comments

Code outside the two test lines are the reference template batch script.

rem #------Test code -----------
rem #----End test code ---------

A) The batch command "set var1=school1" sets the environment variable "var1" to value of "school1". The php script testp1.php is run with parameter "school2"

Test
Run the batch file by double-clicking on file test1p.bat. A command window opens.

  • Confirm the php script outputs and displays the environment variable ("school1").
  • Confirm the php script outputs and displays the parameter ("school2").

Note 1: The output from the PHP script is displayed but not captured, hence cannot be used in the batch file. This will be resolved later using FOR /F command.

Note 2: The PHP script is relatively short and can be run directly as a one-liner from the batch file, removing the need for a script:

Replace the following line:

%US_PHP_EXE% -n -f testp1.php school2

With this line:

%US_PHP_EXE% -n -r  "print(getenv('var1') . PHP_EOL . $argv[1] . PHP_EOL);" school2
 

FOR /F overview

The FOR command has many options. Fortunately, only the "/F" option is required to extract variables/parameters from an application.
There are three command line formats for this option, as follows:

Notes:
tokens are strings separated by delimiters. They are strings you are trying to match. Example tokens are shown in lavender.
delims are characters separating tokens. Example delimiters are shown in yellow.

 

token space delimited token

more tokens,comma delimited,12Z

123=ABC,mixed#delimiters

filenameset is one or more file names. Each file is opened, read, and processed before going on to the next file in filenameset.

Processing consists of reading the file one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.

By default, "/F" passes the first blank separated token from each line of each file. Blank lines are skipped.

 
FOR /F ["options"] %%variable IN (filenameset) DO command [command-parameters]

LiteralString - A string of text will be treated just like a single line of input from a command.
Processing of a LiteralString consists of breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.
The string must be enclosed in double quotes.

 
FOR /F ["options"] %%variable IN ("LiteralString") DO command [command-parameters]

command - The named command along with any parameters is executed. Processing of a command consists of reading the output from the command one line at a time and then breaking the line up into individual items of data or 'tokens'. The DO command is then executed with the parameter(s) set to the token(s) found.

 
FOR /F ["options"] %%variable IN ('command') DO command [command-parameters]

You can override the default parsing behavior by specifying the optional "options" parameter. This is a quoted string that contains one or more keywords to specify different parsing options. The keywords are:

Keyword Description
eol=c Specifies an end of line comment character (just one character) - default is ";"
skip=n Specifies the number of lines to skip at the beginning of the file.
delims=xxx Specifies a delimiter set. This replaces the default delimiter set of space and tab.
tokens=x,y,m-n Specifies which tokens from each line are to be passed to the "for" body for each iteration. As a result, additional variable names are allocated. The "m-n" form is a range, specifying the mth through the nth tokens. If the last character in the "tokens=" string is an asterisk (*), an additional variable is allocated and receives the remaining text on the line after the last token that is parsed.
usebackq Specifies that you can use quotation marks to quote file names in filenameset. A back quoted string is executed as a command, and a single quoted string is a literal string command.

FOR /F Passing LiteralString

String processing for the three FOR /F command line formats filenameset, LiteralString and command are identical. This section provides examples using the LiteralString format, primarily because you can see the text is being processed and requires only a single file, making it easier to experiment with the format command options.

In folder UniServerZ create a new file named testp2.bat, replacing phpxx with the php version number selected on your system:

testp2.bat
@echo off
rem ### working directory current folder 
pushd %~dp0

rem ### set variables
set HOME=%cd%
set US_ROOTF=%HOME%
set US_ROOTF=%US_ROOTF:\=/%
set US_PHP_EXE=%HOME%\core\phpxx\php.exe

rem #------Test code -----------

echo A) No token specified defaults to 1 hence only %%%%A assigned
echo FOR /F %%%%A IN ("123 456 xyz") DO echo Result:  ..%%%%A..%%%%B..%%%%C..
echo Expected ..123..%%B..%%C..
FOR /F %%A IN ("123 456 xyz") DO echo Result:  ..%%A..%%B..%%C..

echo.
echo B) Token 1 and 3 specified. Each match is assigned
echo    the next letter in sequence starting from A then B etc.
echo    note %%%%C is never assigned because only two tokens specified
echo FOR /F "tokens=1,3" %%%%A IN ("aaa bbb ccc") DO echo Result:  ..%%%%A..%%%%B..%%%%C..
echo Expected ..aaa..ccc..%%C..  
FOR /F "tokens=1,3" %%A IN ("aaa bbb ccc") DO echo Result:  ..%%A..%%B..%%C..

echo.
echo C) Token 1-3 range specified. Each match is assigned
echo    a letter in sequence starting from A then B then C.
echo FOR /F "tokens=1-3" %%%%A IN ("123 456 xyz") DO echo Result:  ..%%%%A..%%%%B..%%%%C..
echo Expected ..123..456..xyz.. 
FOR /F "tokens=1-3" %%A IN ("123 456 xyz") DO echo Result:  ..%%A..%%B..%%C..


echo.
echo D) Token 1-3 specified. Multi spaces are treated as a single space.
echo FOR /F "tokens=1-3" %%%%A IN ("123   456 xyz  ") DO echo Result:  ..%%%%A..%%%%B..%%%%C
echo Expected ..123..456..xyz.. 
FOR /F "tokens=1-3" %%A IN ("123   456 xyz  ") DO echo Result:  ..%%A..%%B..%%C..


echo.
echo E) Token 1-3 specified. delims specifies a comma and space
echo FOR /F "tokens=1-3 delims=, " %%%%A IN ("a,Z  x") DO echo Result: ..%%%%A..%%%%B..%%%%C
echo Expected..a..Z..x..
FOR /F "tokens=1-3 delims=, " %%A IN ("a,Z  x") DO echo Result: ..%%A..%%B..%%C..
echo.


echo F) Token 1* specified. 1 match first delim, then * match reset of line
echo FOR /F "tokens=1* delims=, " %%%%A IN ("s,z2  p") DO echo Result:  ..%%%%A..%%B..%%%%C
echo Expected ..s..z2  p..%%C.. 
FOR /F "tokens=1* delims=, " %%A IN ("s,z2  p") DO echo Result:  ..%%A..%%B..%%C..

echo.
echo G) Token 1* specified.
echo use an environment variable for the string.
set str=phpxx\php.exe
echo set str=\phpxx\php.exe
echo FOR /F "tokens=1* delims=\" %%A IN ("%str%") DO echo Result:  ..%%A..%%B..
echo Expected ..phpxx..php.exe.. 
FOR /F "tokens=1-3 delims=\" %%A IN ("%str%") DO echo Result:  ..%%A..%%B..
echo.
rem #----End test code ---------

rem ### restore original working directory
popd

pause
  Comments

Each test section A) to G) have the following structure:

  • Character identification, followed by a short description.
  • Display the command line used.
  • Display result expected.
  • Actual FOR /F command line run. Displays results using the captured tokens.

Note 1: The echo format ..%%A..%%B..%%C.. outputs assigned variables %%A, %%B and %%C enclosed between two full stops on either side. These full stops are included for testing. Apart from visually separating the variables, they allow you to see any unexpected characters captured.

Note 2: Variables in a FOR /F command require a starting character, for example %%A. From this starting point, additional letters are assigned in sequence as required by the FOR command. A variable is created for each token captured and assigned the token's value. If you specify a variable in the DO section that has not been created and assigned a value, its name is output instead.

Note 3: The LiteralString format requires a string to be enclosed in quotes. You can use environment variables. Again, these must be enclosed in quotes.

Note 4: In the ["options"] section, you can place the options in any order. However, it is recommended you place "delims" at the end because it makes specifying a space easier; for example "tokens=1* delims=, "

Test
Run the batch file by double-clicking on file test2p.bat. A command window opens.

  • Confirm "Expected:" and "Result:" are identical.

FOR /F Passing filenameset -1

Extracting a single parameter as the first line of a file such as the MySQL password is achieved using either the SET command with redirection operator, or using the FOR /F command with filenameset format. For comparison, an example of both is shown. Folders (ssl and www) can be protected with allowed usernames and password pairs contained in a file. A user's password can be extracted with a FOR /F command.

In folder UniServerZ, create a new file named testp3.bat, replacing phpxx with the php version number selected on your system:

testp3.bat
@echo off
rem ### working directory current folder 
pushd %~dp0

rem ### set variables
set HOME=%cd%
set US_ROOTF=%HOME%
set US_ROOTF=%US_ROOTF:\=/%
set US_PHP_EXE=%HOME%\core\phpxx\php.exe

rem #------Test code -----------

echo A) Directly read password from file using set command
echo set /p passw=^<%%HOME%%\htpasswd\mysql\passwd.txt
echo Expected: Password=root
set /p passw=<%HOME%\htpasswd\mysql\passwd.txt
echo Result:   Password=%passw%

FOR /F %%G IN (%PWD_FILE1%) DO set MYSQL_PWD2=%%G

echo.
echo B) Directly read password from file using FOR /F command
echo FOR /F %%%%G IN (%%HOME%%\htpasswd\mysql\passwd.txt) DO set passw2=%%%%G
echo Expected: Password=root
FOR /F %%G IN (%HOME%\htpasswd\mysql\passwd.txt) DO set passw2=%%G
echo Result:   Password=%passw2%

rem ### Create name:password pairs in a test file
echo mike:abc123  > pwd_file.txt
echo john:abc124 >> pwd_file.txt
echo fred:abc125 >> pwd_file.txt

echo.
echo C) Display contents of file using FOR /F command
echo FOR /F "tokens=1,2 delims=:" %%%%G IN (pwd_file.txt) DO echo %%%%G %%%%H
echo Expected:
echo mike abc123
echo john abc124
echo fred abc125
echo Result:
FOR /F "tokens=1,2 delims=:" %%G IN (pwd_file.txt) DO echo %%G %%H

echo.
echo D) From the above file, extract password for john
echo FOR /F "tokens=1,2 delims=:" %%%%G IN (pwd_file.txt) DO (
echo IF %%%%G==john echo Result:   Johns password=%%%%H)
echo Expected: Johns password=abc124

FOR /F "tokens=1,2 delims=:" %%G IN (pwd_file.txt) DO (
IF %%G==john echo Result:   Johns password=%%H)

echo.
rem #----End test code ---------

pause

rem ### Delete pwd_file.txt
del pwd_file.txt
echo.
echo File pwd_file.txt deleted
echo.

rem ### restore original working directory
popd

pause
  Comments

Extract MySQL password using SET command:
A) The SET command with the "/p" parameter waits for user input. This input is provided from a file using the redirection operator ("<"). A single line of text from the file sets the named environment variable.

  • set /p variable_name=<full_path_of_text_file

Extract MySQL password using FOR command:
B) Alternative to the above is to use a FOR /F command general format as follows:

  • FOR /F %%G IN (full_path_of_text_file) DO set variable_name=%%G

No options specified, defaults are used. Assign the first token and assign it to variable.

Extract password from name/password file:
C) Using the FOR /F command, a specific name can be targeted and the password extracted. We do not have a file to use. For this example, we create a name:password file pwd_file.txt using the redirection operator. File content shown below:

mike:abc123
john:abc124
fred:abc125

The following code extracts the password for john:

FOR /F "tokens=1,2 delims=:" %%G IN (pwd_file.txt) DO (
IF %%G==john echo Result:   Johns password=%%H)
  • The FOR loop reads each line intern.
  • "Delims=:" splits a line at every occurrence of ":", creating tokens.
  • These tokens are assigned to variables; "username" assigned to %%G and "password" to %%H
  • The variable %%G is tested using the IF command. If the variable is equal to string "john", the variable %%H (password) is echoed to the screen. If no match, the next line is read.

Test
Run the batch file by double-clicking on file test3p.bat. A command window opens.

  • Confirm "Expected:" and "Result:" are identical.

FOR /F Passing filenameset -2

This page is about passing parameters between scripts. For completeness, this section covers passing parameters using an intermediary file. The Uniform Server saves user configuration data to a file. This file can be considered an intermediary file. Data in this file is extracted using the FOR /F command with the filenameset format.

In folder UniServerZ, create a new file named testp4.bat, replacing phpxx with the php version number selected on your system:

testp4.bat
@echo off
rem ### working directory current folder 
pushd %~dp0

rem ### set variables
set HOME=%cd%
set US_ROOTF=%HOME%
set US_ROOTF=%US_ROOTF:\=/%
set US_PHP_EXE=%HOME%\core\phpxx\php.exe
set US_USER_INI=%HOME%\home\us_config\us_user.ini

rem #------Test code -----------

echo A) Read lines from config file that will be processed
echo FOR /F %%%%G IN (%%HOME%%\htpasswd\mysql\passwd.txt) DO echo %%%%A
echo.
echo #------ Lines that will be processed ------------
FOR /F "tokens=*" %%A IN (%US_USER_INI%) DO echo %%A
echo #---- End lines that will be processed ----------

echo.
echo B) Read parameters PHP_SELECT and PHP_INI_SELECT and set environment vars.
echo FOR /F "TOKENS=1,2 delims==" %%%%A IN (%%US_USER_INI%%) DO (
echo If %%%%A==PHP_SELECT Set PHP_SELECT=%%%%B
echo If %%%%A==PHP_INI_SELECT Set PHP_INI_SELECT=%%%%B
echo )
echo echo PHP_SELECT     = %%PHP_SELECT%%
echo echo PHP_INI_SELECT = %%PHP_INI_SELECT%%
echo.
echo Expected: PHP_SELECT     = phpxx
echo Expected: PHP_INI_SELECT = php_production.ini


FOR /F "TOKENS=1,2 delims==" %%A IN (%US_USER_INI%) DO (
If %%A==PHP_SELECT Set PHP_SELECT=%%B
If %%A==PHP_INI_SELECT Set PHP_INI_SELECT=%%B
)
echo Result:   PHP_SELECT     = %PHP_SELECT%
echo Result:   PHP_INI_SELECT = %PHP_INI_SELECT%
echo.
rem #----End test code ---------

rem ### restore original working directory
popd

pause
  Comments

Display all lines that will be processed:
A) Before writing code, you can preview all lines that will be processed with the following command line:

 FOR /F "tokens=*" %%A IN (file_path) DO echo %%A

It uses the "*" operator to match all text on a line. The end of line character default is ";". This matches the user configuration line comments. However, files such as Apache and MySQL use character "#". You can change this default behavior using the "eol" parameter, for example:

 FOR /F "tokens=* eol=#" %%A IN (file_path) DO echo %%A

Extract user configuration parameters:
Each line in the user configuration file has the following format:

parameter=value

B) Using the FOR /F command, we can extract one or more parameters from this file. For example, extract parameters PHP_SELECT and PHP_INI_SELECT then set corresponding environment variables.

FOR /F "TOKENS=1,2 delims==" %%A IN (%US_USER_INI%) DO (
If %%A==PHP_SELECT Set PHP_SELECT=%%B
If %%A==PHP_INI_SELECT Set PHP_INI_SELECT=%%B
)
echo Result:   PHP_SELECT     = %PHP_SELECT%
echo Result:   PHP_INI_SELECT = %PHP_INI_SELECT%

Test
Run the batch file by double-clicking on file test4p.bat. A command window opens.

  • Confirm "Expected:" and "Result:" are identical.

FOR /F Passing command

The FOR /F with command format allows you to run a command line script and capture its output to memory. When the script terminates, memory content is read and is processed line-by-line. This allows you to pass parameters from a child process to the parent process. The following examples provide an introduction to its capability.

In folder UniServerZ, create a new file named testp5.bat:

testp5.bat
@echo off
rem ### working directory current folder 
pushd %~dp0

rem ### set variables
set HOME=%cd%
set US_ROOTF=%HOME%
set US_ROOTF=%US_ROOTF:\=/%

rem ### Get PHP version selected
set CFGF=%HOME%\home\us_config\us_user.ini
FOR /F "TOKENS=1,2 delims==" %%A IN (%CFGF%) DO (
If %%A==PHP_SELECT Set PHP_SELECT=%%B
)

rem ### Set path to php.exe
set Path=%HOME%\core\%PHP_SELECT%;%Path%

rem #------Test code -----------

echo A) Run batch command - display all lines
echo FOR /F %%%%G IN ('dir htpasswd /b /s') DO echo %%%%A
echo.
echo #------ Lines that will be processed ------------
FOR /F "tokens=*" %%A IN ('dir htpasswd /b /s') DO echo %%A
echo #---- End lines that will be processed ----------

echo.
echo B) Run PHP code directly - display all lines
echo FOR /F "tokens=*" %&&%A IN (
echo 'php.exe -n -r  "print('name fred'.PHP_EOL.'address myspace');"'
echo ) DO echo %%%%A

echo.
echo #------ Lines that will be processed ------------
FOR /F "tokens=*" %%A IN (
'php.exe -n -r  "print('name=fred'.PHP_EOL.'address=myspace');"'
) DO echo %%A
echo #---- End lines that will be processed ----------

echo.
echo c) Run PHP code - extract and assign variables
echo FOR /F "tokens=1,2 delims==" %%%%A IN (
echo 'php.exe -n -r  "print('name=fred'.PHP_EOL.'address=myspace');"'
echo ) DO (
echo If %%%%A==name Set name=%%%%B
echo If %%%%A==address Set address=%%%%B
echo )
echo Result:   Name is %%name%% and address is %%address%%
echo.
echo Expected: Name is fred and address is myspace 


FOR /F "tokens=1,2 delims==" %%A IN (
'php.exe -n -r  "print('name=fred'.PHP_EOL.'address=myspace');"'
) DO (
If %%A==name Set name=%%B
If %%A==address Set address=%%B
)
echo Result:   Name is %name% and address is %address%

echo.
rem #----End test code ---------

rem ### restore original working directory
popd
pause
  Comments

Run batch command - display all lines:
A) Before writing code, you can preview all lines that will be processed. The following example code uses the "dir" command with parameters "htpasswd /b /s":

 FOR /F "tokens=*" %%A IN ('dir htpasswd /b /s') DO echo %%A

Note 1: The command name and parameters are enclosed in single quotes.
Note 2: If the command line becomes too long, you can use the alternative format:

FOR /F "tokens=*" %%A IN (
'dir htpasswd /b /s'
) DO echo %%A

Run PHP code directly - display all lines:
B) Before writing code, you can preview all lines that will be processed. This example runs PHP code directly using the following format:

FOR /F "tokens=*" %%A IN (
'php.exe -n -r  "print('name=fred'.PHP_EOL.'address=myspace');"'
) DO echo %%A

This outputs the following two lines:

name=fred
address=myspace

Run PHP code - extract and assign variables:
C) This example shows how to split the above lines, extract each parameter and assign result to environment variables:

FOR /F "tokens=1,2 delims==" %%A IN (
'php.exe -n -r  "print('name=fred'.PHP_EOL.'address=myspace');"'
) DO (
If %%A==name Set name=%%B
If %%A==address Set address=%%B
)
echo Result:   Name is %name% and address is %address%

The PHP code is run and the lines processed. Each line is split into tokens at the "equals" character using "delims==". The tokens are assigned to variables A%% and B%% using "tokens=1,2". These assigned variables are used to create and set the environment variable. Finally, the environment variables are displayed using "echo".

Test
Run the batch file by double-clicking on file test5p.bat. A command window opens.

  • A) and B) Confirm "Lines that will be processed" blocks are displayed.
  • C) Confirm "Expected:" and "Result:" are identical.

Note: Generally, your PHP code would be run from a script (see PHP CLI for details). You would replace the following line:

'php.exe -n -r  "print('name=fred'.PHP_EOL.'address=myspace');"'

with one of the following options:

'php test1.php'
'php -n test1.php'
'php -c core\phpxx\my_cli.ini test1.php'

Note: You must enclose the command in single quotes.

Perl passing parameters

Perl is another powerful scripting language that you can run as CLI from a batch file. For additional information, see Perl CLI page. Passing parameters is similar to PHP. This section provides an example using the above techniques to pass environment variables and parameters between a batch file and a Perl script.

Uniform Server is portable; it does not make any changes to a host PC configuration. This means the path to the Perl interpreter is undefined and must be added to the environment path before a Perl script is run from a batch file.

In folder UniServerZ, create two new files named testp6.bat and testp6.pl with the following content:

testp6.bat
@echo off
rem ### working directory current folder 
pushd %~dp0

rem ### set variables
set HOME=%cd%
set PERL_PATH=%HOME%\core\perl\bin
set PERL_EXE=%HOME%\core\perl\bin\perl.exe

IF NOT EXIST %PERL_EXE% (echo Perl not installed! & goto END)
set Path=%PERL_PATH%;%Path%

rem #------Test code -----------

rem -- Set environment vars for Perl script
set perl_var1=pvar1
set perl_var2=pvar2

rem -- run script with parameters
rem -- display output from script
perl testp6.pl pvar3 pvar4

echo.
echo Script is run for second time however this run uses
echo a FOR /F loop to extract and set variables perl_var1 and perl_var2
echo.

FOR /F "tokens=1,2 delims==" %%A IN (
'perl testp6.pl pvar3 pvar4'
) DO (
If %%A==perl_var1 Set perl_var1=%%B
If %%A==perl_var2 Set perl_var2=%%B
)

echo Display values that were updated by script:
echo.
echo perl_var1=%perl_var1%
echo perl_var2=%perl_var2%

echo.

rem #----End test code ---------

:END
rem ### restore original working directory
popd

pause
testp6.pl
print "--------- Output from Perl script ----------\n";
print "Environment variable perl_var1   =".$ENV{"perl_var1"}."\n";
print "Environment variable perl_var2   =".$ENV{"perl_var2"}."\n";
print "Parameter 1 passed by commandline="."@ARGV[0]\n";
print "Parameter 2 passed by commandline="."@ARGV[1]\n";
print "Change and return Environment variables\n";
print "perl_var1=pvar1xxxxx\n"; 
print "perl_var2=pvar2zzzzz\n";
print "------- End output from Perl script ---------\n";
  Comments

Code outside the two test lines are the reference template batch script.

rem #------Test code -----------
rem #----End test code ---------

Common environment variables are initially set.

set HOME=%cd%
set PERL_PATH=%HOME%\core\perl\bin
set PERL_EXE=%HOME%\core\perl\bin\perl.exe

IF NOT EXIST %PERL_EXE% (echo Perl not installed! & goto END)
set Path=%PERL_PATH%;%Path%

If the Perl executable does not exist, it is assumed the Perl module has not been installed and an error message is displayed before exiting. If the executable exists, the path to Perl binaries is added to the environment path variable.

Test code
Two environment variables ("perl_var1" and "perl_var2") are created. The Perl script will modify these and return new values. These variables can be existing variables.

rem -- Set environment vars for Perl script
set perl_var1=pvar1
set perl_var2=pvar2

The Perl script is run using the following line. Note: two parameters ("pvar3" and "pvar4") are passed on the command line. Raw output from the script is displayed.

perl test6.pl pvar3 pvar4

A FOR /F loop runs the Perl script a second time. No output is displayed. Instead, output is captured and processed by the FOR /F loop, which extracts and sets the two variables "perl_var1" and "perl_var2".

FOR /F "tokens=1,2 delims==" %%A IN (
'perl test6.pl pvar3 pvar4'
) DO (
If %%A==perl_var1 Set perl_var1=%%B
If %%A==perl_var2 Set perl_var2=%%B
)

Finally, the two variables are displayed (echoed), allowing you to confirm variables have been changed.

echo perl_var1=%perl_var1%
echo perl_var2=%perl_var2%

Test
Run the batch file by double-clicking on file testp6.bat. A command window opens.

  • Confirm the above is displayed.

--oOo--