I’m using Rsync and ICACLS to sync two (windows) directories and to do so, I must the same path translated to several ‘styles’: cygwin *nix, remote *nix, UNC. (see examples below)
I’m using the following code to do so, and while it works, the regexp I’m using could be surely made more robust and better working (as you can see, I’m doing a replace of a replace, wich i find ugly at best…)
$remote="remotesrv"
$path="g:\tools\example\"
$local_dos=$path
$remote_dos="\\$remote\"+(($local_dos -replace "^\w","$&$") -replace "(:\\)|(\\)","\")
$local_nix="/cygdrive/"+($local_dos -replace "(:\\)|(\\)","/")
$remote_nix="//$remote/"+(($local_dos -replace "^\w","$&$") -replace "(:\\)|(\\)","/")
"Local DOS : $local_dos"
"Remote DOS : $remote_dos"
"Local *nix : $local_nix"
"Remote *nix: $remote_nix"
the output is:
Local DOS : g:\tools\example\
Remote DOS : \\remotesrv\g$\tools\example\
Local *nix : /cygdrive/g/tools/example/
Remote *nix: //remotesrv/g$/tools/example/
Can someone please help me with the regexes above? Many thanks!
How about this:
The dos ones are pretty simple and aren’t really using regex much.
For the nix ones,
:?\\means “\ optionally preceded by :”. We’re replacing that with a forward slash.This should work fine for the vast majority of non-pathological cases. However, it’s not bulletproof. Crazy file names with slashes or colons in them could easily break this.