Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

But if I try to do the same thing with a variable instead of a parameter, how can I get the same functionality? This attempt is invalid syntax and does not work:
set unqualifiedFilename="test.txt"
echo Qualified filename: %~funqualifiedFilename

like image 819
Blaine Avatar asked Mar 08 '11 05:03

Blaine


1 Answers

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'
like image 148
bobbymcr Avatar answered Oct 30 '22 21:10

bobbymcr