如何转换的Windows文件路径:URL,在一个批处理文件,适合SVN命令行使用 [英] How to convert Windows path to file: URL, in a batch file, suitable for SVN command line use
问题描述
在(使用CollabNet的Subversion的边缘)基于Windows的SVN的安装,我有一个post-commit钩子批处理文件,我建立一个资源库文件夹名称,然后我需要调用 svnsync的
与文件:
URL指向的Windows文件夹
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 svnsync
with a file:
URL pointing to that Windows folder.
现在的问题是:如何转换的Windows文件夹或文件名到文件:
URL,以这样一种方式,该URL至少是可以接受的SVN命令行工具?
The question now is: How can I convert a Windows folder or file name to a file:
URL, in such a way that that URL is at least acceptable for the SVN command line tools?
推荐答案
在一个批处理文件,如果变量 FILE_OR_FOLDER_NAME
包含(绝对或相对的,本地或UNC,带或不带空格,现有的或不存在)的Windows文件或目录名,那么下面的命令可以把相应的文件:变量
FILE_URL
网址code>:
In a batch file, if the variable FILE_OR_FOLDER_NAME
contains the (absolute or relative, local or UNC, with or without spaces, existing or not existing) Windows file or directory name, then the following commands put the corresponding file:
URL in variable FILE_URL
:
for /f "delims=" %%R in ("%FILE_OR_FOLDER_NAME%") do set FILE_URL=%%~fR%
set FILE_URL=file:///%FILE_URL%
set FILE_URL=%FILE_URL:///\\=//%
set FILE_URL=%FILE_URL:\=/%
1行扩展相对文件名到绝对的; 2号线prepends 的file:///
;线3处理的UNC路径的特殊情况;与4号线确保我们只用斜杠。
Line 1 expands the relative file name to an absolute one; line 2 prepends file:///
; line 3 handles the special case of a UNC path; and line 4 makes sure we only use forward slashes.
总之,这些变换 \\\\ REMOTEHOST \\共享\\文件夹
到文件://远程主机/共享/文件夹
和 D:\\文件夹
到的file:/// D:/文件夹
Taken together, these transform \\remotehost\share\folder
to file://remotehost/share/folder
, and d:\folder
to file:///d:/folder
.
至于我的测试中去,上面的命令总是导致一个文件:
URL,它是可以接受的一个SVN命令行,也可能用于其他用途。
As far as my testing goes, the above commands always result in to a file:
URL that is acceptable for an SVN command line, and probably also for other uses.
这是不是真的正确,唯一的一点是,空格和其他特殊字符,如#
没有正确的URL-CN $ C $在生成<$ C CD $ C>文件:网址: D:/我的测试#回购
变成的file:/// D:/我的测试#回购
,这在技术上是不正确的。然而,在我的具体使用情况下,这不会带来任何问题,因为SVN命令行分析器无论找到版本库。
The only thing that is not really correct, is that spaces and other special characters like #
are not properly URL-encoded in the resulting file:
URL: D:/my test#repo
becomes file:///D:/my test#repo
, which is technically not correct. However, in my specific use case this poses no problem, since the SVN command line parser finds the repository regardless.
这篇关于如何转换的Windows文件路径:URL,在一个批处理文件,适合SVN命令行使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!