Unexpected DOS shell expansion of %~n1 and %~x1
Posted: 28 May 2021 15:51
I always supposed that the DOS shell has no automatic wildcard expansion. But I have just noticed such an effect with the %~n and %~x syntaxes, which are expanding to the first matching file name and/or extension IF there is such a match, except if the passed parameter is '*.*', which goes totally haywire.
Surely somebody would have documented this before, since I find the same effect in Win7 and above, but I can't find anything online (I would appreciate links if anybody has any).
test-shell-expansion.bat:
C:\bat\testing>dir /b
@notes.txt
ExampleCallSub.bat
inline-comments.bat
test-args.bat
test-choice.bat
...
Surely somebody would have documented this before, since I find the same effect in Win7 and above, but I can't find anything online (I would appreciate links if anybody has any).
test-shell-expansion.bat:
Code: Select all
@echo off
call :splitarg *.bat
call :splitarg test*.*
call :splitarg :test*.*
call :splitarg
call :splitarg *.*
call :splitarg :*.*
exit /b 0
:splitarg
echo arg = %~1, name = %~n1, ext = %~x1
goto :eof
@notes.txt
ExampleCallSub.bat
inline-comments.bat
test-args.bat
test-choice.bat
...
Code: Select all
C:\bat\testing>.\test-shell-expansion.bat
arg = *.bat, name = ExampleCallSub, ext = .bat
arg = test*.*, name = test-args, ext = .bat
arg = :test*.*, name = :test*, ext = .*
arg = , name = , ext =
arg = *.*, name = , ext = .
arg = :*.*, name = :*, ext = .*