Reputation: 1131
I was trying to convert current working directory of a .bat script into linux format by using wsl wslpath
. To show you it works on CMD:
However, when I put it in a .bat file, and changed %cd%
to %~dp0
, the path is empty:
test.bat
contains:
FOR /F %%i IN ('wsl wslpath -a %~dp0') DO set lp=%%i
echo %lp%
Any idea why?
Upvotes: 1
Views: 1271
Reputation: 57252
Try this:
echo "%cd%" -- "%~dp0"
%cd%
returns the path without ending backslash. So you can add a second variable that clears it.
set "scriptDir=%~dp0"
set "scriptDir=%scriptDir:~0,-1%"
UPDATE (with string substitution only - use the toLinuxPath subroutine)
@echo off
call ::toLinuxPath "%userprofile%\AppData\Local\Temp" tempF
echo %tempF%
exit /b 0
:toLinuxPath [returnVariable - the result will be stored in it; If omitted will be only echoed]
setlocal
set "_path=%~p1"
set "name=%~nx1"
set "drive=%~d1"
set "rtrn=%~2"
set "result=/mnt/%drive:~0,1%%_path:\=/%%name%"
endlocal & (
if "%~2" neq "" (
set "%rtrn%=%result%"
) else (
echo %result%
)
)
Upvotes: 2