How to use batch parameter modifiers for a variable, not a parameter

The syntax% ~ f1 changes the parameter representing the file name to its full path. Is there a way to get this functionality for variables defined in a script package, and not just for parameter values?

For example, if the user provides the command line parameter "test.txt", the following script works:
echo Qualified file name:% ~ f1

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

+7
source share
2 answers

The easiest way I can come up with is to simply use the FOR command.

Example 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%' 

Output Example:

  Original param was 'test.cmd';  full path is 'C: \ test.cmd' 
+4
source

The second way is to call the function and use the parameter% 1 ...% n

 @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 
+4
source

All Articles