Batch script: convert path to linux format using wslpath


Gregor Isack

I am trying to convert the current working directory of a .bat script to linux format using wsl wslpath. To show you it works on CMD:

enter image description here

However, when I put it in a .bat file and change %cd%to %~dp0, the path is empty:enter image description here

test.batInclude:

FOR /F %%i IN ('wsl wslpath -a %~dp0') DO set lp=%%i
echo %lp%

Any idea why?

empokomaka

try this:

echo "%cd%" -- "%~dp0"

%cd%Return the path without ending backslashes. So you can add a second variable that clears it.

set "scriptDir=%~dp0"
set "scriptDir=%scriptDir:~0,-1%"

UPDATE (with string replacement only - use 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%
    )
)

Related


Batch script: convert path to linux format using wslpath

Gregor Isack I am trying to convert the current working directory of a .bat script to linux format using wsl wslpath. To show you it works on CMD: However, when I put it in a .bat file and change %cd%to %~dp0, the path is empty: test.batInclude: FOR /F %%i IN

Batch script: convert path to linux format using wslpath

Gregor Isack I am trying to convert the current working directory of a .bat script to linux format using wsl wslpath. To show you it works on CMD: However, when I put it in a .bat file and change %cd%to %~dp0, the path is empty: test.batInclude: FOR /F %%i IN

Batch script: convert path to linux format using wslpath

Gregor Isack I am trying to convert the current working directory of a .bat script to linux format using wsl wslpath. To show you it works on CMD: However, when I put it in a .bat file and change %cd%to %~dp0, the path is empty: test.batInclude: FOR /F %%i IN

Batch script: convert path to linux format using wslpath

Gregor Isack I am trying to convert the current working directory of a .bat script to linux format using wsl wslpath. To show you it works on CMD: However, when I put it in a .bat file and change %cd%to %~dp0, the path is empty: test.batInclude: FOR /F %%i IN

Batch script: convert path to linux format using wslpath

Gregor Isack I am trying to convert the current working directory of a .bat script to linux format using wsl wslpath. To show you it works on CMD: However, when I put it in a .bat file and change %cd%to %~dp0, the path is empty: test.batInclude: FOR /F %%i IN

How to convert wslpath /home/user/ to windows path

User 504909 I'm using the Linux Subsystem for Windows 10 (Windows 10 version 1803) I can use the command line: user@laptop:~$ wslpath -w /c/ C:\ but when I try to use user@laptop:~$ wslpath -w ~ wslpath: /home/user: Result not representable Even if I use: us

Bash script to convert Windows path to Linux path

username I'm new to shell scripting and trying to do the following, converting a windows path to a linux path and navigating to that location: Input: cdwin "J:\abc\def"Action:cd /usr/abc/def/ So I will change the following: "J:" -> "/usr" and "\" -> "/" Here

Get absolute path of file using batch script

Hardware 135 I have a Java application that generates output that is stored in a file myfile.txtusing a batch script . Now I want to pass the absolute path of this myfile.txtfile as a command line argument to another Java application. So something like: java -

Convert date format using batch file

nimble dave I have a text file with many date entries in the following format : YYYY/MM/DD How do you convert it to the following format? YYYY-MM-DD This file contains many date entries, so it should change the date format for all entries... thanks Debenham

Convert date format using batch file

nimble dave I have a text file with many date entries in the following format : YYYY/MM/DD How do you convert it to the following format? YYYY-MM-DD This file contains many date entries, so it should change the date format for all entries... thanks Debenham

Convert .log to .txt using batch script

username Hi all, I have a list of log files and want to convert it to a text file. In my case, I was able to convert a single log file (input: Trace0.log output: output0.txt) using java script commands... start java -jar USB_Trace_Decoder.jar -t Trace.tr -l Tr

Convert .log to .txt using batch script

username Hi all, I have a list of log files and want to convert it to a text file. In my case, I was able to convert a single log file (input: Trace0.log output: output0.txt) using java script commands... start java -jar USB_Trace_Decoder.jar -t Trace.tr -l Tr

Using Linux environmental variables as a path in Perl script

AMcNall I am trying to use an environmental variable to point to a file, run it through a subroutine and associate it with a variable. I managed it with Windows but I cannot get the syntax working for Linux.. This is what I have: my $config = read_config("$EN

Batch Convert Images to Another Format in Windows Using Gimp

Alex Marshall As the title suggests, does anyone know how to batch convert images between formats (with default settings) in Windows using the gimp-console-[version].exe program? November Better than Gimp or Irfanview is ImageMagick . For example, try: mogrify

Batch Convert Images to Another Format in Windows Using Gimp

Alex Marshall As the title suggests, does anyone know how to batch convert images between formats (with default settings) in Windows using the gimp-console-[version].exe program? November Better than Gimp or Irfanview is ImageMagick . For example, try: mogrify

Convert batch script to Powershell

RPR I'm new to Powershell and we're in the middle of migrating an application that requires us to convert an old legacy batch script to Powershell. Batch scripts are using many old legacy tools such as RCMD, XCOPY and SOON to execute the requirements. Can anyo

Convert batch script to Powershell

RPR I'm new to Powershell and we're in the middle of migrating an application that requires us to convert an old legacy batch script to Powershell. Batch scripts are using many old legacy tools such as RCMD, XCOPY and SOON to execute the requirements. Can anyo

How to change system date format using batch file/script?

username I'm a software tester and I often need to change my short date format from M/dd/yy to d/M/yy as Australian/New Zealand clients use other date formats. The trouble is to open the clock, click change date and time, click change calendar settings, choose

How can a user convert a file to Linux format in a bash script?

Jenny I have many files in *.cand *.hdirectories. I want to convert all these files to Linuxcompatible format. I tried the following script and it works, but nothing converts. And I also need to check if everything was converted successfully. So I filter and c

How can a user convert a file to Linux format in a bash script?

Jenny I have many files in *.cand *.hdirectories. I want to convert all these files to Linuxcompatible format. I tried the following script and it works, but nothing converts. And I also need to check if everything was converted successfully. So I filter and c

Search and convert files using .vb script and Ironpython (batch)

Kiranka I have to convert a lot of files from one format (.DAT) to another (.CSV) using software. The software follows COM (Component Object Model), which provides an in-process interface for scripting using VBS (Visual Basic Scripting) or Iron python. Here's