开发者

How to use batch parameter modifiers on a variable rather than a parameter

开发者 https://www.devze.com 2023-02-15 05:48 出处:网络
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 par

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: %~f1

But 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: %~funqualifiedFilename


The 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
0

精彩评论

暂无评论...
验证码 换一张
取 消