Windows Scripting: Difference between revisions

From Andreida
Line 25: Line 25:
3 (called in a function)
3 (called in a function)
4
4
</pre>

=== 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
<pre>
FOR %%A IN (%*) DO (
echo xxx %%A xxx
)
</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
)

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