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.
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%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With