Jump to content

Modify A String Value


fu2m8
 Share

Recommended Posts

Hey Guys,

I was hoping someone here might be able to point me in the right direction with how i should go about the following:

Basically we are updating a registry key that contains some DNS entries here at work. Long story short some people already have settings in this registry key and we would like to keep these while adding in our changes. I have got this functionality to work so far but if possible to make the script tidier i would like to know how i can check for duplicate entries in this registry key and remove them.

I tried splitting it to an array but i didn't really know what i hoped to achieve by this (just seemed like the thing to do) and couldn't work out how to check each of the entries for duplicates via a loop (i was trying stuff with StringInStr but no luck as loops and arrays are still slightly out of my grasp).

The working code without the other stuff:

;users orginal setting
$originalsearchlist = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList")

;the additions that we would like to add 
$dnsadditions = "domain.com,domain.com.au,anotherdomain.com,testdomain.com"

;if the $originalsearchlist is not empty this will be used as the variable that holds $dnsadditions & $originalsearchlist 
$mergeddns = "" ; <<<-- I BELIEVE THIS IS THE STRING THAT IF POPULATED LATER IN THE SCRIPT I WOULD LIKE TO CHECK IF IT HAS DUPLICATE VALUES IN IT

;flag file that will be used to stop the app running more than once per machine
$flagfile = FileOpen("c:\WINDOWS\system32\soe\flags\DNS-Suffix-Change.txt", 9)


If $originalsearchlist = "" Then ;if blank just write the settings it's fine
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList", "REG_SZ", $dnsadditions)
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "UseDomainNameDevolution", "REG_DWORD", "1")
        FileWrite($flagfile, 'This file is used as a flag file to stop the DNS Suffix being added multiple times by the program. As of the completion of this program the value of the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList" registry key was: ' & $dnsadditions)
        FileClose($flagfile)
        Exit
    Else ;if not blank merge's the 2 together
        $mergeddns = $dnsadditions & "," & $originalsearchlist
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList", "REG_SZ", $mergeddns)
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "UseDomainNameDevolution", "REG_DWORD", "1")
        FileWrite($flagfile, 'This file is used as a flag file to stop the DNS Suffix being added multiple times by the program. As of the completion of this program the value of the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList" registry key was: ' & $mergeddns)
        FileClose($flagfile)
        Exit
EndIf

The $mergeddns var is the one i believe i need to do the checking (checking for duplicate entries) on but i don't have much of an idea of how to go about it :) . If anyone has any idea's or could point me in the right direction it would be greatly appreciated :P .

Thanks in advance!

Link to comment
Share on other sites

this might help give some direction

$var = RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\au3", "MRUList")
MsgBox(4096, "Program files in:", $var & "  ", 3)

$split = StringSplit($var, "")

For $x = 1 to $split[0]
        $log = RegRead("HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\au3", $split[$x])
        MsgBox(0, "region - Log", $log & "  ", 3)
Next

8)

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

Splitting it up into an array could be a good idea... but StringInStr() is what I believe you are looking for.

$sString = 'a a b b c c d d'
MsgBox(0, '', _RemoveDuplicateChars($sString)) ; should result in (a bcd)
Func _RemoveDuplicateChars($sString)
    Local $sHold, $sMid
    For $iCC = 1 To StringLen($sString)
        $sMid = StringMid($sString, $iCC, 1)
        If Not StringInStr($sHold,$sMid) Then
            $sHold &= $sMid
        EndIf
    Next
    Return $sHold
EndFunc

;For Words etc...using StringSplit to make the return unique
$sString = 'apple,orange,apple,bannana,pear,orange'

MsgBox(0, '', _RemoveDupes($sString, ','));example shows our delimeter as a comma

Func _RemoveDupes($sString, $vDelim)
    Local $aSplit, $sHold
    If $vDelim = @CRLF Then
        $aSplit = StringSplit(StringStripCR($sString), @LF)
    Else
        $aSplit = StringSplit($sString, $vDelim)
    EndIf
    
    For $iCC = 1 To $aSplit[0]
        If Not StringInStr($vDelim & $sHold, $vDelim & $aSplit[$iCC] & $vDelim) Then
            $sHold &= $aSplit[$iCC] & $vDelim
        EndIf
    Next
    Return StringTrimRight($sHold, StringLen($vDelim))
EndFunc

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Splitting it up into an array could be a good idea... but StringInStr() is what I believe you are looking for

Thanks for the quick replys guys :D . I borrowed :P SmOke_N's _RemoveDupe Function code and it seems to be working great.

Here's the finalised script.

;users orginal setting
$originalsearchlist = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList")

;the additions that we would like to add 
$dnsadditions = "domain.com,anotherdomain.com,thebestdomainever.com,hello.com,example.com"

;if the $originalsearchlist is not empty this will be used as the variable that holds $dnsadditions & $originalsearchlist 
$mergeddns = ""

;flag file that will be used to stop the app running more than once per machine
$flagfile = FileOpen("c:\WINDOWS\system32\soe\flags\DNS-Suffix-Change.txt", 9)

If $originalsearchlist = "" Then
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList", "REG_SZ", $dnsadditions)
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "UseDomainNameDevolution", "REG_DWORD", "1")
        FileWrite($flagfile, 'This file is used as a flag file to stop the DNS Suffix being added multiple times by the program. As of the completion of this program the value of the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList" registry key was: ' & $dnsadditions)
        FileClose($flagfile)
        Exit
    Else
        $mergeddns = $dnsadditions & "," & $originalsearchlist
        $mergeddns = _RemoveDupes($mergeddns, ',') ;should remove duplicate entries with ',' being a delimiter
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList", "REG_SZ", $mergeddns)
        RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "UseDomainNameDevolution", "REG_DWORD", "1")
        FileWrite($flagfile, 'This file is used as a flag file to stop the DNS Suffix being added multiple times by the program. As of the completion of this program the value of the "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters", "SearchList" registry key was: ' & $mergeddns)
        FileClose($flagfile)
        Exit
EndIf

;code borrowed from Smoke_N =)
Func _RemoveDupes($sString, $vDelim)
    Local $aSplit, $sHold
    If $vDelim = @CRLF Then
        $aSplit = StringSplit(StringStripCR($sString), @LF)
    Else
        $aSplit = StringSplit($sString, $vDelim)
    EndIf
    
    For $iCC = 1 To $aSplit[0]
        If Not StringInStr($vDelim & $sHold, $vDelim & $aSplit[$iCC] & $vDelim) Then
            $sHold &= $aSplit[$iCC] & $vDelim
        EndIf
    Next
    Return StringTrimRight($sHold, StringLen($vDelim))
EndFunc

Thanks Again Guys~! :)

Link to comment
Share on other sites

  • Moderators
:D ... Just be glad I could afford the boat on that venture ( :) Gary :P )

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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...