[2005] Converting Directory string to a cygwin compatible format
cygwin is a program that allows you to use the unix bash shell on your xp machine. I need my vb program to send it some commands (rsync rsh files to and from a device connected by usb and detected as a network server) , but since it doesn't understand Windows directory syntax it doesn't work. So here is what I do to get around it:
Code:
Dim OutDirectory As String = My.Settings.LogOutDirectory
Dim CygWinDirectory As String
CygWinDirectory = OutDirectory.Replace("\", "/")
OutDirectory = CygWinDirectory.Replace(" ", "\ ")
CygWinDirectory = OutDirectory.Replace(":", "")
OutDirectory = CygWinDirectory.Insert(0, "/")
CygWinDirectory = OutDirectory.Replace("(", "\(")
OutDirectory = CygWinDirectory.Replace(")", "\)")
CygWinDirectory = OutDirectory.Replace("!", "\!")
Anyone have any suggestions on a better way to do this string manipulation?
Basically, Unix doesn't understand spaces, characters like "(,!,$,^,)", and usualy starts the line with "/c/" as in "/c/Programs\ and\ settings/".
In the code above the variable Fliping from one to another looks kinda janky, but it does work, so if you need to send directories to your unix this will do it, but I'm sure there is a better way.
Re: [2005] Converting Directory string to a cygwin compatible format
actually, unix does understand spaces...you just have to quote the line.
It's perfectly calid to have a directory:
/c/Programs and settings
as long as it's quoted
Code:
[matt@silvertip ~]$ mkdir 'programs and settings'
[matt@silvertip ~]$ ls -la
total 96
drwx------ 3 matt matt 4096 Jul 13 23:15 .
drwxr-xr-x 4 root root 4096 May 14 10:12 ..
-rw------- 1 matt matt 789 Jul 12 01:10 .bash_history
-rw-r--r-- 1 matt matt 24 Nov 9 2007 .bash_logout
-rw-r--r-- 1 matt matt 176 Nov 9 2007 .bash_profile
-rw-r--r-- 1 matt matt 124 Nov 9 2007 .bashrc
-rw------- 1 matt matt 26 Jun 2 13:05 .dmrc
-rw------- 1 matt matt 35 Nov 9 2007 .lesshst
drwxr-xr-x 2 matt matt 4096 Jul 13 23:15 programs and settings
-rw------- 1 matt matt 680 Jun 2 12:14 .viminfo
-rw------- 1 matt matt 55 Jun 2 14:33 .Xauthority
-rw-r--r-- 1 matt matt 658 Nov 9 2007 .zshrc
Re: [2005] Converting Directory string to a cygwin compatible format
Cool! does it also handle strange characters?
Re: [2005] Converting Directory string to a cygwin compatible format
Do you have a unix machine to try out?
Looking at your list - "(,!,$,^,)", i was able to create files and directories with - ( ^ ) characters.
Code:
[matt@silvertip programs and settings]$ mkdir 'kds(as^df)saasfd'
[matt@silvertip programs and settings]$ ls -la
total 32
drwxr-xr-x 4 matt matt 4096 Jul 14 02:12 .
drwx------ 4 matt matt 4096 Jul 13 23:18 ..
drwxr-xr-x 2 matt matt 4096 Jul 14 02:12 kds(as^df)saasfd
How "strange" I can't tell...your code above seemed fine for most conversions. I just wanted to mention that some characters are fine as long as the string is quoted, which you can plan on doing with all strings. It won't hurt.
Besides, I'm not sure why you would do any conversion...if you are doing an rsh or rsync, then you should cd into the directory you want and then just give the filename as the target parameter.
Or maybe there's something else to it?
Re: [2005] Converting Directory string to a cygwin compatible format
Yeah, the user selects what directory to download the file to using a standard OpenFileDialog which I set to my.settings.outfile. When the user clicks on the button, it starts a process opening up the cygwin bash command prompt and uses that directory to determine where the download goes to. Rsync is actually getting run on the device(server) not on my machine so I can't control the location unless I do it this way.
very confusing, but it works, I just hope it works for any directory that a user can pick.
Re: [2005] Converting Directory string to a cygwin compatible format
Okay, I remembered hearing about cygwin, but couldn't remember what it was, so I looked it up. :)
Found this:
Cygwin supports both Win32- and POSIX-style paths, where directory delimiters may be either forward or back slashes. UNC pathnames (starting with two slashes and a network name) are also supported.
You MAY be able to get by with using regular windows path names, from what I'm reading.
Having said that, there may be issues with other programs that know nothing about cygwin....rsh/rsync being a good example. However, I found in the cygwin API a function cygwin_conv_to_full_posix_path that"Converts a Win32 path to a POSIX path." Maybe that's what you're looking for?