Advertisement
defect122

Windows batch - Using variables

Feb 16th, 2012
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Batch – Programming .
  2.  
  3. Scenario: Variables
  4.  
  5. This batchfile-tutorial will be about variables that are used in batches quite often.
  6. 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).
  7. 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.
  8. Basically, variables can be set like this:
  9.  
  10. @echo off
  11. set test="just a test"
  12. echo %test%
  13. pause
  14.  
  15. 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.
  16. 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.
  17. For example:
  18.  
  19. set name=test.exe
  20. set name2=%name:~0,4%
  21. echo %name2%
  22.  
  23. What happens here is that the variable name2 is only displayed showing its first four letter: test.
  24. The rest (.exe) is cut off and not displayed.
  25. You can also set negative number here, such as: set name2=%name:~-4%
  26. This would just show .bat.  
  27.  
  28. The pattern for this is:
  29.  
  30. var:~a,b%
  31.  
  32. Note here that it starts with 0 and not with 1. So from 0 up to some number. In the example it is
  33. t  e s  t  .
  34. 0 1 2 3 4
  35.  
  36. You can even substitute whole commands, for example:
  37.  
  38. set test="command /Y /S filename"
  39. set test=%test:/ /Y /S =/L %
  40. echo %test%
  41.  
  42. 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.
  43. Note here: There´s no space between test:/ but there is between test:/ /Y /S =..
  44. Calculating with set:
  45. This is possible with set /a.
  46.  
  47. set /a 1+1
  48.  
  49. or with variables
  50.  
  51. @echo off
  52. set /a x=1+1
  53. echo %x%
  54.  
  55. or
  56.  
  57. @echo off
  58. set /a x=(1+2)*(1+4)
  59. echo %x%
  60.  
  61. For more options, open a command prompt and type set /?
  62. set can also be used for user input with the set /p option.
  63.  
  64. @echo off
  65. echo Please type something to end this batch!
  66. set /p test="Input :"
  67. echo Thank You. Good job!
  68. pause
  69.  
  70. Now quite a few new modifiers were added with Windows NT 4. They are the following:
  71.  
  72. %~1     expands %1 and removes any surrounding quotation marks
  73. %~f1        expands %1 to a fully qualified path name
  74. %~d1        expands %1 to a drive letter.
  75. %~p1        expands %1 to a path
  76. %~n1        expands %1 to a file name
  77. %~x1        expands %1 to a file extension
  78. %~s1        extended path contains short names only
  79. %~a1        expands %1 to file attributes
  80. %~t1        expands to date and time of file
  81. %~z1        expands %1 to size of file
  82. %~$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.
  83.  
  84. Now these modifiers can bes used in batch scripts in various ways and they can be combined.
  85. 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:
  86.  
  87. @echo off
  88. set batchlocation=%~dp0%
  89. echo %batchlocation%
  90. pause
  91.  
  92. 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.
  93.  
  94. And concluding here is an example batch for variables and what you can do with them.
  95.  
  96.  
  97. @echo off
  98. set counter=0
  99. set maximum=3
  100.  
  101. :WORK
  102. echo Working...
  103.  
  104. set /a counter=%counter% + 1
  105. if "%counter%" EQU "%maximum%" goto THATSIT
  106.  
  107. echo %counter%
  108. echo Proceeding...
  109. ping -n 3 localhost >nul
  110.  
  111. goto WORK
  112.  
  113. :THATSIT
  114. echo Thats it! You´ve reached the maximum number of tries!
  115.  
  116. Pause >nul
  117.  
  118.  
  119. 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.
  120.  
  121. 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.
  122.  ch
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement