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 exam开发者_如何学JAVAple.
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.
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%
精彩评论