Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a relative path to a fully qualified path in a DOS batch file?

I am writing a batch file that does a number of operations in a folder that is specified relative to the first argument passed in to the batch file. Within the batch file, I would like to echo to the user the the folder I am working in. However, every time I echo the path, it contains the ....\ that I used to determine where to place my folder. For example.

set TempDir=%1\..\Temp
echo %TempDir%

So, if I run my batch file with a parameter \FolderA, the output of the echo statement is FolderA\..\Temp instead of \Temp as I would expect.

like image 762
John Visser Avatar asked Jul 06 '11 04:07

John Visser


1 Answers

SET "TempDir=%~1\..\Temp"
CALL :normalise "%TempDir%"
ECHO %TempDir%
…

:normalise
SET "TempDir=%~f1"
GOTO :EOF

…

The :normalise subroutine uses the %~f1 expression to transform the relative path into the complete one and store it back to TempDir.


UPDATE

Alternatively, you could use a FOR loop, like this:

SET "TempDir=%~1\..\Temp"
FOR /F "delims=" %%F IN ("%TempDir%") DO SET "TempDir=%%~fF"
ECHO %TempDir%
like image 85
Andriy M Avatar answered Sep 29 '22 08:09

Andriy M