Thursday, August 29, 2019

CMD script Variables

Window/DOS environment does not require the initialization or declaration of variable types.   The convention is to use lower case names. 

The default value of a variable is an empty string, e.g  "", susceptible to errors like misspelled variable names. 

Variable Assignment
           SET foo=bar             <= Assignment
           echo %foo%             < = output
           
           SET /A  four=2+2 4        "/A" supports arthhmetic operations
           SET                            No arguments, lists all existing variables.


WARNING: SET will always overwrite (clobber) any existing variables. It’s a good idea to verify you aren’t overwriting a system-wide variable when writing a script. A quick ECHO %foo% will confirm that the variable foo isn’t an existing variable. For example, it might be tempting to name a variable “temp”, but, that would change the meaning of the widely used “%TEMP%” environmental varible. DOS includes some “dynamic” environmental variables that behave more like commands. These dynamic varibles include %DATE%, %RANDOM%, and %CD%. It would be a bad idea to overwrite these dynamic variables.
Reading the Value of a Variable





Listing Existing Variables

The SET command with no arguments will list all variables for the current command prompt session. Most of these varaiables will be system-wide environmental variables, like %PATH% or %TEMP%.



Variable Scope (Global vs Local)

By default, variables are global to your entire command prompt session. Call the SETLOCALcommand to make variables local to the scope of your script. After calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL, calling EXIT, or when execution reaches the end of file (EOF) in your script.



Special Variables

There are a few special situations where variables work a bit differently. The arguments passed on the command line to your script are also variables, but, don’t use the %var% syntax. Rather, you read each argument using a single % with a digit 0-9, representing the ordinal position of the argument. You’ll see this same style used later with a hack to create functions/subroutines in batch scripts.

There is also a variable syntax using !, like !var!. This is a special type of situation called delayed expansion. You’ll learn more about delayed expansion in when we discuss conditionals (if/then) and looping.
Command Line Arguments to Your Script

MyScript.bat first dog turtle

%0 is equal to "MyScript.bat" 
%1 is equal to "first"
%2 is equal to "dog"


SHIFT function to move arguments in a list of command-line arguments.


Tricks with Command Line Arguments

Command Line Arguments also support some really useful optional syntax to run quasi-macros on command line arguments that are file paths. These macros are called variable substitution support and can resolve the path, timestamp, or size of file that is a command line argument. The documentation for this super useful feature is a bit hard to find – run ‘FOR /?’ and page to the end of the output.

%~I     I tyhink this removes quotations from path variables

SET myvar=%~I 

%~fI is the full path to the folder of the first command-line argument


%~fsI   references the "short name" of the file


%~dpI is the full path to the parent folder of the first command-line argument.

SET parent=%~dp0 will put the path of the script file in the variable %parent%.

%~nxI      file name and extension of the first command-line argument. 

Can be used to prefix a message with the script’s name, like 
             ECHO %~n0: some message
So end-user knows where the message cam from.  

Some Final Polish



Top of all batch scripts.

SETLOCAL ENABLEEXTENSIONS
SET me=%~n0            <= Stores name of script.
SET parent=%~dp0  <<== Stored path to script

First makes all script variable local so that no system variables get clobbered. 
ENABLEEXTENSIONS   No idea yet
The second allows messages to be identified by the script name
     example:   ECHO %me%: some message). 


posted by Y.H.N.

No comments:

Post a Comment