How to convert relative path to full path in a DOS batch file?

I am writing a batch file that performs several operations in the folder that is specified relative to the first argument passed to the batch file. Inside the batch file, I would like to repeat to the user the folder in which I work. However, every time I repeat the path, it contains .... \, which I used to determine where to place my folder. For instance.

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

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

+6
source share
1 answer
 SET "TempDir=%~1\..\Temp" CALL :normalise "%TempDir%" ECHO %TempDir% … :normalise SET "TempDir=%~f1" GOTO :EOF … 

Subroutine :normalise uses the expression %~f1 to convert the relative path to full and save it back to TempDir .


UPDATE

Alternatively, you can use the FOR loop, for example:

 SET "TempDir=%~1\..\Temp" FOR /F "delims=" %%F IN ("%TempDir%") DO SET "TempDir=%%~fF" ECHO %TempDir% 
+8
source

Source: https://habr.com/ru/post/892096/


All Articles