Jump to content
Sign in to follow this  
sliceofpie

Manipulate String for Cygwin

Recommended Posts

sliceofpie

Hello Everyone! Posted Image

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)

EndFunc

1. 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!! Posted Image

Share this post


Link to post
Share on other sites
enaiman

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 :)

Share this post


Link to post
Share on other sites
SublimePorte

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)

Share this post


Link to post
Share on other sites
sliceofpie

Enaiman! This is great! Posted Image 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! Posted Image

Share this post


Link to post
Share on other sites
sliceofpie

I came across a little bit of an issue. Posted Image

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

EndFunc

For some reason I can't get it to work and I'm not sure how to add the "/" at the end of it.

Share this post


Link to post
Share on other sites
sliceofpie

;) 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 Posted Image

Share this post


Link to post
Share on other sites
sliceofpie

I've figured it out the UNIX file format. Posted Image

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]))
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)

EndFunc

Probably not the cleanest way of doing it but it's a start for me.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.