Linux – Convert Windows path for Windows Ubuntu Bash

windows-subsystem-for-linux

I have a windows batch script that uses Windows Ubuntu Bash. It receives a full Windows path as an argument and then passes that path to a command in Ubuntu Bash.

@echo off
bash -lic 'ffmpeg -i "%1" output.avi'

Here "%1" is the full Windows path, like "C:\some path\file.avi"

The command gives the error:

C:some pathfile.avi: Protocol not found

What can I do to have this Windows path convert to a path like /mnt/c/some\ path/file.avi which the Windows Bash would actually understand?

Best Answer

Windows Build 17046 [1] contains new wslpath utility that can translate paths from/to WSL/Windows. This was known missing WSL feature. [2]

Example usage:

$ echo $0
/bin/bash

$ which wslpath
/bin/wslpath

$ wslpath -a 'C:\\aaa\\bbb\\ccc\\foo.zip'
/mnt/c/aaa/bbb/ccc/foo.zip

You can call wslpath from Powershell on Windows:

>>> wsl wslpath -a 'C:\\aaa\\bbb\\ccc\\foo.zip'
/mnt/c/aaa/bbb/ccc/foo.zip

wslpath options and parameters:

-a    force result to absolute path format
-u    translate from a Windows path to a WSL path (default)
-w    translate from a WSL path to a Windows path
-m    translate from a WSL path to a Windows path, with ‘/’ instead of ‘\\’
Related Question