Windows Scripting: Difference between revisions

From Andreida
No edit summary
Line 26: Line 26:
4
4
</pre>
</pre>

=== test for parameters ===
<pre>
@echo off

if [%1]==[] (
echo Parameter 1 missing!
) else (
echo Paramater 1 found: %1
)
</pre>

* '[' 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

Revision as of 17:05, 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

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