How to use batch parameter modifiers on a variable rather than a parameter
The syntax %~f1 modifies a parameter representing a filename into its fully qualified path. Is there a way to get that functionality for variables defined within the batch script, and not just for parameter values?
For example, if a user provides a command line parameter "test.txt", the following script works:
echo Qualified filename: %~f1But if I try to do the same thing with a variable instead of a parameter, how can I get the same functio开发者_JS百科nality? This attempt is invalid syntax and does not work:
set unqualifiedFilename="test.txt" echo Qualified filename: %~funqualifiedFilenameThe simplest way I can think of is to just use a FOR
command.
Sample batch script:
@echo off
setlocal
set FileName=test.cmd
for %%i in (%FileName%) do set FullPath=%%~fi
echo Original param was '%FileName%'; full path is '%FullPath%'
Sample output:
Original param was 'test.cmd'; full path is 'C:\test.cmd'
A second way is to call a function and use the %1 ... %n parameter
@echo off
set FileName=test.cmd
call :GetFullPath %FileName%
echo Original param was '%FileName%'; full path is '%FullPath%'
goto :eof
:GetFullPath
set "FullPath=%~f1"
goto :eof
精彩评论