Thank you, yes, I knew about the expansion rules (interesting in themselves).
And thank you for pointing out that the "*.*" case is regular after all - I had totally overlooked "." and ".." . But that was not the reason for my post.
The effect I am curious about appeared in a simple script to pass wildcard file specifications to DIR. I wanted some enhancements that meant first separating the parameter's name and extension parts. My example code was cut down from this project, though not by enough, and I could have reduced it to a simple one-liner:
splitarg.bat
Code: Select all
@echo arg = %~1, name = %~n1, ext = %~x1
Code: Select all
C:\bat\testing>splitarg *.bat
arg = *.bat, name = ExampleCallSub, ext = .bat
My point was to note that %~n1 is automatically expanded (if possible) by the DOS shell to an actual file name, though %~1 is never expanded. I can now see that this behaviour is inevitable if the %~px and %~fx syntaxes are going to work. But it does mean that designers who want to pass wildcard filenames to a script cannot simply use %~n1 and %~x1 to split them.
My own workaround, for anybody else who may have this use case, is to prefix the file spec with an illegal character and remove it after doing the name splitting:
Code: Select all
@echo off
setlocal
set arg=%~1
call :wildcard_func ":%arg%"
exit /b 0
:wildcard_func
set s=%~n1
set name=%s:~1%
set ext=%~x1
echo arg = %arg%, name = %name%, ext = %ext%
goto :eof
Code: Select all
C:\bat\testing>safesplitarg *.bat
arg = *.bat, name = *, ext = .bat
This at least is simpler than trying to use bat file string manipulations