SOLVED: Pass windows paths from batch script to perl script (backslash double-quote issue)

Posted by zauberer777 on 2017-09-07 08:20

When attempting to pass a windows path with spaces from a batch script, we have to double-quote the path string. A windows path usually ends with a backslash. Surrounding the path with double-quotes creates a trailing \", which is interpreted as double-quote. Example:

===== 8< test.bat ====
C:\perl\bin\perl.exe "%~dp0%"
===== 8< =====
print $ARGV[0],"\n";
C:\my test> .\test.bat
C:\my test"

Note that there is a trailing " at the end of the output, which was created during Interpolation of "C:\test\", where the trailing \" is converted into ".

Solution: Pro-actively remove the trailing backslash before passing it to perl in quotes.

===== 8< test.bat ====
@set dir=%~dp0
@if %dir:~-1%==\ set dir=%dir:~0,-1%
C:\perl\bin\perl.exe "%dir%"
C:\my test> .\test.bat
C:\my test

Any better solution is highly regarded.