I am learning a bit how to work with parameters in a batch-script and ended up creating some kind of template for reading arguments and setting parameters
@echo off
SetLocal EnableDelayedExpansion
set needextra=
set errstat=
set noflag=
set param_f=Not set yet
set param_g=You didn't use G-flag
:readARGS
IF [%1] == [] goto :endARGS
call :arg_%1 2> nul
IF ERRORLEVEL 1 call :arg_default %1
SHIFT
IF DEFINED needextra (
set %needextra%=%~1
SHIFT
set needextra=
)
goto :readARGS
:endARGS
echo path to directory of batch script: %~dp0
echo - noflag: !noflag!
echo - param_f: !param_f!
echo - param_g: !param_g!
EndLocal
exit /b 0
The first echo that prints the directory is important for my question (see later)
After that I create a function for each flag (arg_/flag
) and one for arguments without a flag (the arg_default
):
:arg_/f -- flag f: set param_f to value of next argument
set needextra=param_f
exit /b 0
:arg_/g -- flag g: just set the param_g to a value
set param_g=You used the G-flag
exit /b 0
:arg_default -- default, neither flag f or g: just set the noflag
echo noflag=%~1
exit /b 0
When I put everything in a batch-file, let's say C:\Users\user\scripts\params.bat
and put the directory in the path I can execute the script:
> params "just an arg"
path to directory of batch script: C:\Users\user\scritpts\
- noflag: just an arg
- param_f: Not set yet
- param_g: You didn't use G-flag
> params another /G /F "this is f"
path to directory of batch script: C:\Users\user\scritpts\
- noflag: another
- param_f: this is f
- param_g: You used the G-flag
The fact I put it in functions allows me to enter the parameters in whatever order I wish but if I put the G-flag as last I get this strange behaviour:
> params /F "this is f again" bizar /G
path to directory of batch script: C:\
- noflag: bizar
- param_f: this is f again
- param_g: You used the G-flag
The %~dp0
returns only C:\
! I tried it with other parameters, moved the batch-file to another directory, called it within the directory, the %~dp0
kept returning only C:\
. In fact each time the last argument contains a "/" the %~dp0
will behave "strangely" like in this example:
> params /G /F stranger "what happens /here"
path to directory of batch script: C:\Users\user\script\what happens \
- noflag: what happens /here
- param_f: stranger
- param_g: You used the G-flag
Can someone please explain me why this is happening? I cannot figure out why and could not find anything on the web either. I use Windows 10
I really appreciate any help you can provide.