sliceofpie Posted August 15, 2010 Share Posted August 15, 2010 Hello Everyone! I've come across an issue with StringReplace and need a bit of help. When transferring information over from Windows file format to Cygwin disk drive, I'd like to manipulate the string. Here's my example:Global $array[3][1], $Output $array [0][0] = "C:\Windows\System32\" $array [1][0] = "C:\Program Files\Super man\" $array [2][0] = "X:\Important\" ReplaceSlashes() Func ReplaceSlashes() $Output = StringReplace($array [0][0], "\", "/") $Output = StringReplace($Output, "C:", "/cygdrive/C") ConsoleWrite($Output) EndFunc1. At times I have a drive letter other than "C". How can I tell it to to change whatever the drive letter is (i.e. "X") to something like "/cygdrive/X"?2. In order to keep the file structure on the Cygwin drive, if the folder I'm transferring has a space in the name (i.e. "Super man", it has to be changed to "/cygdrive/C/Program Files/Super\man/". How would I accomplish that?If someone can point me in the right direction I would greatly appreciate it!! Link to comment Share on other sites More sharing options...
enaiman Posted August 15, 2010 Share Posted August 15, 2010 Not too difficult: 1. Get the first character (drive letter) in a variable and use that 2. replace any white space with \ Global $array[3][1], $Output $array [0][0] = "C:\Windows\System32\" $array [1][0] = "C:\Program Files\Super man\" $array [2][0] = "X:\Important\" MsgBox(0, "Result 1", ReplaceSlashes($array [0][0])) MsgBox(0, "Result 2", ReplaceSlashes($array [1][0])) MsgBox(0, "Result 3", ReplaceSlashes($array [2][0])) Func ReplaceSlashes($param) Local $driveLetter = StringLeft($param, 1) Local $driveName = StringLeft($param, 2) $Output = StringReplace($param, "\", "/") $Output = StringReplace($Output, $driveName, "/cygdrive/"&$driveLetter) $Output = StringReplace($Output, " ", "\") ConsoleWrite($Output) Return $Output EndFunc SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
SublimePorte Posted August 15, 2010 Share Posted August 15, 2010 It has to be changed to "/cygdrive/C/Program Files/Super\man/".Unix paths actually use a "\ " to represent a space in a path, not a mere "\" (note the space) Link to comment Share on other sites More sharing options...
sliceofpie Posted August 16, 2010 Author Share Posted August 16, 2010 Enaiman! This is great! And SublimePorte thank you for correcting me. You are correct. It should read "/Super\ man/" instead of "/Super\man/"I'm modifying Enaiman's update a little bit to look like this:Global $array[3][1], $Output $array [0][0] = "C:\Windows\System32\" $array [1][0] = "C:\Program Files\Super man\" $array [2][0] = "X:\Important\" MsgBox(0, "Result 1", ReplaceSlashes($array [0][0])) MsgBox(0, "Result 2", ReplaceSlashes($array [1][0])) MsgBox(0, "Result 3", ReplaceSlashes($array [2][0])) Func ReplaceSlashes($param) Local $driveLetter = StringLeft($param, 1) Local $driveName = StringLeft($param, 2) $Output = StringReplace($param, "\", "/") $Output = StringReplace($Output, $driveName, "/cygdrive/"&$driveLetter) $Output = StringReplace($Output, " ", "\ ") ConsoleWrite($Output) Return $Output EndFunc..looking forward to testing this on the server! Link to comment Share on other sites More sharing options...
sliceofpie Posted August 16, 2010 Author Share Posted August 16, 2010 I came across a little bit of an issue. Everything worked but the Unix-style target path. I'm trying to get a 4th result with an outcome of "Super \man/"Global $array[3][1], $Output, $uDirectory, $uOutput $array [0][0] = "C:\Windows\System32\" $array [1][0] = "C:\Program Files\Super man\" $array [2][0] = "X:\Important\" MsgBox(0, "Result 1", ReplaceSlashes($array [0][0])) MsgBox(0, "Result 2", ReplaceSlashes($array [1][0])) MsgBox(0, "Result 3", ReplaceSlashes($array [2][0])) MsgBox(0, "Result 4", UNIXStyle($array [1][0])) Func ReplaceSlashes($param) Local $driveLetter = StringLeft($param, 1) Local $driveName = StringLeft($param, 2) $Output = StringReplace($param, "\", "/") $Output = StringReplace($Output, $driveName, "/cygdrive/"&$driveLetter) $Output = StringReplace($Output, " ", "\ ") ConsoleWrite($Output) Return $Output EndFunc Func UNIXStyle($param) $uDirectory = StringSplit($param, '\', 1) Local $i For $i = 0 To UBound($uDirectory) -1 ConsoleWrite($uDirectory[$i]) Next $i = $i -1 $uOutput = StringReplace($uDirectory[$i], " ", "\ ") Return $uOutput EndFuncFor some reason I can't get it to work and I'm not sure how to add the "/" at the end of it. Link to comment Share on other sites More sharing options...
enaiman Posted August 16, 2010 Share Posted August 16, 2010 ... my-my-my you're kinda lazy $Output &= "/" SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
sliceofpie Posted August 16, 2010 Author Share Posted August 16, 2010 Don't know why I'm over complicating it. Thanks again Enaiman!On the other question, I will try it some more before I come back and post Link to comment Share on other sites More sharing options...
sliceofpie Posted August 17, 2010 Author Share Posted August 17, 2010 I've figured it out the UNIX file format. expandcollapse popupGlobal $array[3][1], $Output, $uDirectory, $uOutput $array [0][0] = "C:\Windows\System32\" $array [1][0] = "C:\Program Files\Super man\" $array [2][0] = "X:\Important\" MsgBox(0, "Result 1", ReplaceSlashes($array [0][0])) MsgBox(0, "Result 2", ReplaceSlashes($array [1][0])) MsgBox(0, "Result 3", ReplaceSlashes($array [2][0])) UNIXStyle() Func ReplaceSlashes($param) Local $driveLetter = StringLeft($param, 1) Local $driveName = StringLeft($param, 2) $Output = StringReplace($param, "\", "/") $Output = StringReplace($Output, $driveName, "/cygdrive/"&$driveLetter) $Output = StringReplace($Output, " ", "\ ") ConsoleWrite($Output) Return $Output EndFunc Func UNIXStyle() $uDirectory = StringSplit($array [1][0], '\', 1) For $i = 0 To UBound($uDirectory) -1 ConsoleWrite($uDirectory[$i]& @CRLF) Next Local $s = $uDirectory[0] - 1 ConsoleWrite($uDirectory[$s]) $result = $uDirectory[$s] $uOutput = StringReplace($result, " ", "\ " ) & "/" MsgBox(0, "Result", $uOutput) EndFuncProbably not the cleanest way of doing it but it's a start for me. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now