How to convert Windows path to file: URL, in a batch file, suitable for SVN command line use

前端 未结 2 347
梦谈多话
梦谈多话 2021-01-13 11:23

In a Windows-based SVN installation (using CollabNet Subversion Edge), I have a post-commit hook batch file where I construct a repository folder name, and I need to call

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 12:02

    This answer is a good start, but as noted it doesn't handle special characters. The following fulfills that shortcoming.

    Create a batch file, say, unc2url.bat with this content:

    powershell -Command "write-host ([System.Uri]""%1"").AbsoluteUri"

    Then from a Windows command prompt:

    > unc2url.bat "\\serverX\a long\and tedious\yet unexciting\path to\some Random #64# file.txt"
    file://serverx/a%20long/and%20tedious/yet%20unexciting/path%20to/some%20Random%20%2364%23%20file.txt
    

    To put the result in a variable that can be used in the rest of the batch file, you can use the FOR syntax:

    SET UNC2URL_CMD=powershell -Command "write-host ([System.Uri]""%CONDA_CHANNEL_PATH%"").AbsoluteUri"
    FOR /f "delims=" %%X IN ('%UNC2URL_CMD%') do set "FILE_URL=%%X"
    
    REM Now use it for whatever
    ECHO %FILE_URL%
    

    Drawback: This powershell command cannot handle relative paths. To remediate this, we can add a command that converts relative paths to absolute paths, if applicable, such as the first line in the aforementioned other answer, subject to the drawback that the "%%~fR%" does not quite work as advertised to ensure a fully qualified path: it prepends %CD% to a path that starts "//server/...".

提交回复
热议问题