Jump to content

String replace question..


lyledg
 Share

Recommended Posts

"Command"="\\Servername\Sharename$\Path\to\apps\Appname.EXE"

The above is a line from a Registry file that I need to re-import back into the registry.

Problem is that the line needs to have "\\" rather than just "\".

How do I replace all "\" with "\\" ONLY in this line and nowehere else in the file. A String replace won't work as far as I can see? I could be wrong though??

Also, this line can vary in length, as it is a variable path. I want to make sure that all the "\" get replaced with "\\" for this line only..

Thanks

Link to comment
Share on other sites

NOT tested, but might help:

$filename = "regFile.reg"
$text = FileRead($filename, FileGetSize($filename))
;;;$pos = StringReplace($text, '"Command"="\\', '"Command"="\')

$startPos = StringInStr($text, '"Command"="\\')
$endPos = StringInStr(StringTrimLeft($text,$startPos), @CRLF)

$oldSection = StringMid($text, $startPos, $endPos - $startPos)
$newSection = StringReplace($oldSection, "\", "\\")

$text = StringReplace($text, $oldSection, $newSection)
; alternatively could say $text = StringLeft($text, $startPos) & $newSection & StringTrimLeft($text, $endPos)

FileWrite("newFile.reg", $text)
Edited by CyberSlug
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

NOT tested, but might help:

$filename = "regFile.reg"
$text = FileRead($filename, FileGetSize($filename))
;;;$pos = StringReplace($text, '"Command"="\\', '"Command"="\')

$startPos = StringInStr($text, '"Command"="\\')
$endPos = StringInStr(StringTrimLeft($text,$startPos), @CRLF)

$oldSection = StringMid($text, $startPos, $endPos - $startPos)
$newSection = StringReplace($oldSection, "\", "\\")

$text = StringReplace($text, $oldSection, $newSection)
; alternatively could say $text = StringLeft($text, $startPos) & $newSection & StringTrimLeft($text, $endPos)

FileWrite("newFile.reg", $text)

<{POST_SNAPBACK}>

Almost there!

Thanks Cyberslug, this is the result I am getting now

Works fine, but does not replace the first two "\\"

"Command"="\\\\Servername\\Sharename\\Path\\to\\App\\Appname.exe"

??

Link to comment
Share on other sites

EDIT:

Agh, looked in the helpfile, StringRegExp ( "test", "pattern" ) I THINK that will do what you want.

{x} Repeat the previous character, set or group exactly x times.

/EDIT

You can try to do something like check each parenthesis, see if there is a parenthesis to its direct left/right, and delete it if so. THEN replace all \'s with \\.

StringMid will be the function you'll use for this method.

OR

Just start string replacing after the first \\, by splitting the string into 2 seperate strings.

Edited by Insolence
"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Link to comment
Share on other sites

Still not sure what you're trying to do.

Not a registry person.

If you give example in and example desired out...

;^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v

Func AllBackslashTwice($AString)
  $NewString = StringReplace($AString, "\\", "\")
  $NewString = StringReplace($NewString, "\", "\\")
  Return $NewString
EndFunc
;^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v

Func AllBackslashDouble($AString)
  $NewString = StringReplace($AString, "\", "\\")
  Return $NewString
EndFunc
;^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v

;TEST OUTPUT
$TestStr = "Test\\test1\test2\\test3\"
MsgBox(0,"", AllBackslashTwice($TestStr))
MsgBox(0,"", AllBackslashDouble($TestStr))
;^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v^v

J

If I am too verbose, just say so. You don't need to run on and on.

Link to comment
Share on other sites

$regString = inputBox ("Registry Key", "Input the key to convert, in English..")

$regString = stringReplace($regString,"\\","¬")

$regString = stringReplace($regString,"\","\\")

$regString = stringReplace($regString,"¬","\\\\")

msgbox (0, "Registry Key, in Micro$oftese", $regString)

$regString = stringReplace($regString,"\\\\","¬")

$regString = stringReplace($regString,"\\","\")

$regString = stringReplace($regString,"¬","\\")

msgbox (0, "Registry Key in English", $regString)

Link to comment
Share on other sites

Ok...

I need to do 2 things here

!) I want to import a registry key, which contains a path that is obtained from an "Fileopen" function...

So, something like this

"Command"="\\Servername\Sharename$\Path\to\apps\Appname.EXE"

But because I want to import this key back into the Registry, I need to have "\\" for every single "\" in this string specified above, remembering that this path can vary depending what is chosen in the "Fileopen" function..

So basically, I want to change any occurence of a "\" with a "\\" whenever it happens , and

2) As well as strip off the \\Servername\Sharename$ evertime and rewrite this line

Hopefully this makes more sense

The registry line is needed for an automated application deployment mechanism we have implemented in our organisation, and needs the conditions I have mentioned above

Any help would be greatly appreciated!

Cheers

:lmao:

Link to comment
Share on other sites

The double-slash conversion I've outlined above.

Removing the \\server\share part is probably best done by searching for the fourth backslash in the string, as that will separate it from the rest.

Something like:

$relpath = stringmid($fullpath,stringinstr($fullpath,"\",0,4) +1 ,255)

(remove the "+1" term if you want the initial backslash included)

Also might be useful:

$filename = stringmid($fullpath,stringinstr($fullpath,"\",0,-1) +1 ,255)

will return the filename and extension only (that is, the part after the last backslash)

To return the relative path in relation to a known root-folder:

func relpath ($pathname, $rootfolder)
 if stringright($rootfolder,1) <> "\" then $rootfolder = $rootfolder & "\"
 $relpath = stringmid($pathname,stringlen($rootfolder)+1,254)
 Return $relpath
endfunc

Hope this helps,

Ian.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...