Sei sulla pagina 1di 3

Batch Programming .

Scenario: Variables
This batchfile-tutorial will be about variables that are used in batches quite often. Variables can be created using the "set" command. But there are also pre-defined variables under windows that you can take for your code. You can see them by typing "set" on the CLI (command prompt). For example, %ver% describes the windows version (under XP its %os%) or 0% stands for the current filename. These can be used like always, for example with echo: echo %os% or echo 0% etc. Basically, variables can be set like this: @echo off set test="just a test" echo %test% pause So, test is used as a variable for "just a test". This batch is just a simple example of how to set single variables. As with a lot of stuff in batch programming there a quite a few options and more complex alternatives to this. One option is to cut strings of variables and just show a certain length of the string or just hide certain letters in a string. For example: set name=test.exe set name2=%name:~0,4% echo %name2% What happens here is that the variable name2 is only displayed showing its first four letter: test. The rest (.exe) is cut off and not displayed. You can also set negative number here, such as: set name2=%name:~-4% This would just show .bat. The pattern for this is: var:~a,b% Note here that it starts with 0 and not with 1. So from 0 up to some number. In the example it is t es t . 01234 You can even substitute whole commands, for example: set test="command /Y /S filename" set test=%test:/ /Y /S =/L % echo %test% set test sets a command with its parameters /Y and /S and a further filename. Next, the options /Y and /S are replaced with /L and echo correctly shows the least set variable test. Note here: Theres no space between test:/ but there is between test:/ /Y /S =..

Calculating with set: This is possible with set /a. set /a 1+1 or with variables @echo off set /a x=1+1 echo %x% or @echo off set /a x=(1+2)*(1+4) echo %x% For more options, open a command prompt and type set /? set can also be used for user input with the set /p option. @echo off echo Please type something to end this batch! set /p test="Input :" echo Thank You. Good job! pause Now quite a few new modifiers were added with Windows NT 4. They are the following: %~1 expands %1 and removes any surrounding quotation marks %~f1 expands %1 to a fully qualified path name %~d1 expands %1 to a drive letter. %~p1 expands %1 to a path %~n1 expands %1 to a file name %~x1 expands %1 to a file extension %~s1 extended path contains short names only %~a1 expands %1 to file attributes %~t1 expands to date and time of file %~z1 expands %1 to size of file %~$PATH:1 searches the directories listed in the path environment variable and expands %1to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string. Now these modifiers can bes used in batch scripts in various ways and they can be combined. For example, If I combine these with a variable that I set to echo the current directory where the batch is located it looks like this: @echo off set batchlocation=%~dp0% echo %batchlocation% pause So if d stands for the drive, p for the path and, as you know, %0 as a varibale stands for the current

file, dp0 describes the location of the batchfile as used above. And concluding here is an example batch for variables and what you can do with them. @echo off set counter=0 set maximum=3 :WORK echo Working... set /a counter=%counter% + 1 if "%counter%" EQU "%maximum%" goto THATSIT echo %counter% echo Proceeding... ping -n 3 localhost >nul goto WORK :THATSIT echo Thats it! Youve reached the maximum number of tries! Pause >nul Put this in an editor you will see what it does. Its pretty much self-explanatory. Its a good way to use variables for a counter. Ping is used because some command needs to be used in order to realize the counting down of the numbers. And it is not displayed since there is a >nul in the code. ch

Potrebbero piacerti anche