Zero logo

Batch Files- Emulate Server Console

The UniController command console opens a command window with environment variables preconfigured. This command window allows you to run PHP scripts without specifying the full path to php.exe If you find this command window too restrictive an alternative is to create your own. The following page covers emulating UniController to open a command console using a batch file. Although specific to Uniform Server some of the code snippets are applicable to other applications.

Background

Opening a standard command prompt (for details see How to run a standard command window) has one major disadvantage, its environment is inherited. If the inherited environment is missing environment variables a application requires, that application will fail. This is true for Uniform Server which is portable and does not change the host environment.

A standard command prompt inherits all top-level variables. These can be created or changed by a user performing specific operating system tasks. This process is made difficult on purpose to prevent inadvertent changes and to avoid variable clutter. The inherited environment is a copy and easily changeable. At the command prompt you can add new environment variables and change inherited ones to create a new environment. This new environment is passed to applications started from the command prompt. Similarly an application can create a new environment and pass it to other applications it runs.

Important note: A child process (application) cannot change a parents environment variables. A child process can change only the copy (snapshot of the current environment) it inherits from a parent.

Overview

To emulate UniControllers command console we can use the above to our advantage. The following lists basic requirements:

  • Run a batch file – Inherits environment.
    • Locate batch file in folder UniServerZ. - Suitable location for portability and reference.
    • Allow batch file execution from another application for example another batch file.
  • Manipulate the inherited environment.
    • Path environment variable. Add full path to folder containing php.exe file.
    • Path environment variable. Add other full paths as required.
  • Set new environment variables.
    • HOME environment variable. Full path to folder UniServerZ - Folder separator back slash
    • US_ROOTF environment variable. Full path to folder UniServerZ - Folder separator forward slash
    • Set any other new environment variables as required.
  • Open a new command window – Inherits our new environment.
    • Runs batch file command cmd.
  • Close batch file.

Note: Highlighted elements allow running of PHP CLI scripts.

Basic batch file

In folder UniServerZ create a new file named run_cmd.bat with the following content:

Comments

A) The batch file command "@echo off" prevents displaying of command lines.

B) The batch file command pair "pushd %~dp0" and "popd" apart from saving and resorting a callers location forces current working directory to the location of the file containing the command pair.

C) The HOME environment variable is set to the current working directory.
D) The US_ROOTF environment variable is set to the current working directory (%HOME%).
E) The line “set US_ROOTF=%US_ROOTF:\=/%” converts backslashes to forward slashes.

F) The FOR IN DO loop reads the user configuration file us_user.ini and obtains the selected PHP version and sets environment variable PHP_SELECT.
G) The "Path" environment variable is modified to include path to php.exe

H) Command prompt is opened by "cmd". The parameter "/k" keeps the command prompt window open. Commands "cls" clears the window content, "title" sets the window title name and "color" sets the background colour.

  run_cmd.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 CFGF=%HOME%\home\us_config\us_user.ini

rem ### Get PHP version selected
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 ### open command prompt
cmd.exe /k "cls & title Uniserver emulated console & COLOR B0"

:END
rem ### restore original working directory
popd






Test
Run the batch file by double clicking on file run_cmd.bat, a command window opens. Type "set" without the quotes into this window.

  • Inherited environment variables are displayed.
  • Confirm new variables HOME and US_ROOTF show full path to folder UniServerZ and file separators are back slashes and forward slashes respectively.
  • Chack path displays full path to php.exe and original path is appended.

Note: Variables HOME and US_ROOTF are used in Uniform Server PHP configuration files (for example php-cli.ini).


--oOo--