3

I'm trying to write a batch file for svnsync, which needs urls to svn repositories. The rest of the batch file uses %~dp0 to get the path of the batch file, but that doesn't work with svnsync.

What is the best way to convert a path (say %~dp0repo, which gets expanded to c:\backup\repo) to a uri suitable for svnsync (file:///c:/backup/repo)?

Ideally it would be able to handle spaces and what not in the path too, so I'd prefer avoid having to use some explicit character replacement to convert from path to URL -- but if that's the only way, oh well.

Thanks!

hippietrail
  • 13,703
  • 15
  • 87
  • 133
Jimmy
  • 4,610
  • 8
  • 51
  • 71

1 Answers1

4

From your recipe is seems you only need to:

  • Replace \ with /
  • Stick file:/// on the front

Here we go:

set DOSPATH=%~dp0repo
set URI=file:///%DOSPATH:\=/%
bobbogo
  • 13,257
  • 3
  • 44
  • 53
  • And to be secure against special characters in the file/pathname you could use quotes: set "DOSPATH=%dp0repo" ... set "URI=file://%DOSPATH:\=/%" – jeb Feb 10 '11 at 18:16
  • @jeb: Nope, remarkably you don't need quotes in the two lines above. – bobbogo Feb 10 '11 at 18:28
  • 2
    @bobbogo: In many situation your solution works, but it fails with C:\Documents & Settings – jeb Feb 10 '11 at 18:57
  • @jeb: Hah! I was waiting for someone to say that (not!). Yes of course, you are right. – bobbogo Feb 10 '11 at 19:06
  • 1
    Put both assignments into double quotes (i.e. like `set "var=value"`). That seems to fix the `&` issue. I still do not know how to output the result without using double quotes, but otherwise I think the var gets the correct URI and is usable. – Andriy M Feb 10 '11 at 22:01
  • @Andriy M: You could use always delayed expansion for secure access of variables. For more in-depth [SO how the cmd interpreter parse scripts](http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/4095133#4095133) – jeb Feb 10 '11 at 22:42
  • You still need to escape all characters that are legal in file names but illegal in URIs. And those can be quite a lot. – Joey Feb 14 '11 at 21:30