Windows Scripting: Difference between revisions

From Andreida
Line 37: Line 37:
echo xxx %%A xxx
echo xxx %%A xxx
)
)
</pre>

Output:
<pre>
>test 1 2 3 4 5 6 7 8 9 10 11 12 13
x 1 x
x 2 x
x 3 x
x 4 x
x 5 x
x 6 x
x 7 x
x 8 x
x 9 x
x 10 x
x 11 x
x 12 x
x 13 x
</pre>
</pre>



Revision as of 17:19, 19 September 2020

directory of current script

change to the directory of the executed script:

chdir /D %~dp0

The /D allows to change drives too.

functions

@echo off
echo 1
call :func_1 2
call :func_1 3
echo 4
exit /B

:func_1
echo %1 (called in a function)
exit /B 0

Output:

>test
1
2 (called in a function)
3 (called in a function)
4

parameters

  • Parameters to a script are %1 to %9
  • %0 is the call and should contain the script name
  • %10 does not exist
  • %* "is" all parameters, even if there are 15
  • if you want to have more than 9 parameters, google for shift or use something like
FOR %%A IN (%*) DO (
  echo xxx %%A xxx
)

Output:

>test 1 2 3 4 5 6 7 8 9 10 11 12 13
x 1 x
x 2 x
x 3 x
x 4 x
x 5 x
x 6 x
x 7 x
x 8 x
x 9 x
x 10 x
x 11 x
x 12 x
x 13 x

test for parameters

@echo off

if [%1]==[] (
  echo Parameter 1 missing!
) else (
  echo Paramater 1 found: %1
)
  • '[' and ']' around "%1" are just for the case it is empty. You can use anything you want:
 if xx%1xx==xxxx (
  • you need a space between "[]" and '(' in the if condition
  • the "(" must be on the same line as the if
  • the ") else (" can not be on different lines