Sign in to follow this  
Followers 0
castman

Need Help Building an App with StringRegExp

13 posts in this topic

I'm developing an application on AutoIt that will convert REG_EXPAND_SZ, REG_MULTI_SZ and REG_BINARY values from a .reg file to strings, replace file paths through the interface and then write the new values back to a .reg file.

I need to recognize file paths using a command like StringRegExp.

I want to recognize a file protocol path like this: X:\ and like this X:\something.

I also want to recognize a file protocol path like this: %ProGraMFiles%\something and: %APpDaTa%\something and: %Localsettings%\something and: %winDIR%\something

I also want to recognize a UNC path like this: \\something and: \\something\something2

Here is my source code

$file = FileOpen("file.reg", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
Dim $keyname[500], $valuename[500], $Temp[10000], $values[500]
Dim $kn = 1, $vn = 1, $v, $r, $c, $d = 0, $output = ""
; Read in lines of text until the EOF is reached
While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
    $result = StringInStr($line, "[HKEY_")
        if $result <> 0 then
        $keyname[$kn] = $line
        endif
    $result = StringInStr($line, "=hex(2):")
        if $result<>0 then
            $result = StringInStr($line, chr(34)&"=")
        $kn = $kn + 1
        $valuename[$vn] = StringLeft($line,$result)
        $vn = $vn + 1
                $v = $v + 1
        $result2 = StringTrimLeft($line,$result+8)
        $result = StringInStr($result2,chr(44)&chr(92))
        MsgBox(0, "Line read:",$result&"  "&asc(.))
        if $result = StringInStr($result2,",\") <> 0 then        
        While $result <> 0
            $result2 = StringTrimRight($result2,1)
            $result3 = StringTrimLeft(FileReadLine($file),2)
            $result2 = $result2 & $result3
            $result = StringInStr($result2,",\")
        Wend
            endif
            $Temp = StringSplit($result2,",")
            MsgBox(0,"Line read aaa",chr($Temp[$d])&" "&UBound($Temp,1)&" "&$Temp[104]&$Temp[103]&$Temp[102])
        For $c = 1 to (UBound($Temp,1)) - 1
                If $Temp[$c] = "00" Then
            $d = $d + 1
            If $d = 2 then $output = $output & @CRLF
        EndIf
        If $Temp[$c] <> "00" Then
                        $output = $output & chr(dec($Temp[$c]))
            $d = $d - 1
        Endif
                Next
            MsgBox(0,"Line read -",$output)
        if StringRegExp($output,"[a-z][A-Z]\\x5C\\x3A") <> 0 or StringInStr($output,"\\") <> 0 Then
            $values[$v] = $output
            $values = StringSplit($output,"")
                    MsgBox(0,"oi",$values[1]&$values[2]&$values[3])
        endif
        endif
Wend

FileClose($file)

Share this post


Link to post
Share on other sites



I also want to recognize the end of the file paths, like this: \something.ext

Off course, all file paths may contain spaces. And That's all.

One alpha character, two known characters, somethings (ANSI) and file path end.

% character, text case insensitive, % character, somethings (ANSI) and end of file path.

Please Help. I really don't understand well StringRegExp.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

There is my AutoIt Source that I reviewed many times and finally I finished part 1 with some adjustments (using mode 3)

#include <File.au3>

AutoItSetOption("MustDeclareVars", 1)

Dim $keyname[5000], $valuename[5000], $modifiedvalues[5000], $paths[5000], $filepaths[5000]
Dim $tkey, $tvname, $tvalue, $separator, $pathkind[5000], $lnc[5000], $separators[5000], $TempString[10000], $TmpString[2] 
Dim $pathnumber = 0, $r, $c, $d = 0, $output = "", $k, $v, $ln = 0, $pattern[10]
Dim $mode, $answer, $file, $line, $result, $result2, $result3
$pattern[0] = "[[:alpha:]]\x3A\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[1] = "\x5C\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x20\x3B]*"
$pattern[2] = "\x25(?i)programfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[3] = "\x25(?i)commonprogramfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[4] = "\x25(?i)programfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[5] = "\x25(?i)windir\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[6] = "\x25(?i)systemroot\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[7] = "\x25(?i)appdata\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[8] = "\x25(?i)userprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[9] = "\x25(?i)allusersprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$answer = MsgBox(4, "What kind of value you want to convert ?", "REG_EXPAND_SZ or REG_MULTI_SZ ?")
If $answer = 7 Then
    $mode = "REG_MULTI_SZ"
else
    $mode = "REG_EXPAND_SZ"
EndIf
MsgBox(0,"Mode",$mode)
$file = FileOpen("file.reg", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
; Read in lines of text until the EOF is reached
Func AssignPaths($d)
    For $c = 0 to (UBound($TempString,1)) - 1
            $pathnumber = $pathnumber + 1
        $keyname[$pathnumber] = $tkey
        $valuename[$pathnumber] = $tvname
        If $TempString[$c] <> "" Then
        $tvalue = StringRight($TempString[$c],1)
        endif
        If $tvalue = chr(34) or $tvalue = "," or $tvalue = " " or ";" then
            $tvalue = StringTrimRight($TempString[$c],1)
            $separators[$pathnumber] = StringRight($TempString[$c],1)   
            $filepaths[$pathnumber] = StringTrimRight($TempString[$c],1)
        Else
            $tvalue = $TempString[$c]
            $filepaths[$pathnumber] = $TempString[$c]
        EndIf
        $TmpString = StringRegExp($tvalue,"[^\x2E\x22\x2C]*\x5C",1)
        If UBound($TmpString,1) <> 0 Then
        $tvalue = $TmpString[0]
        endif
        $paths[$pathnumber] = $tvalue
        $modifiedvalues[$pathnumber] = $output
        $lnc[$pathnumber] = $ln
        $pathkind[$pathnumber] = $d
        MsgBox(0,"hi",$paths[$pathnumber])
    Next    
EndFunc
While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
    $result = StringInStr($line, "[HKEY_")
        if $result <> 0 then $tkey = $line
    if $mode = "REG_EXPAND_SZ" then
        $result = StringInStr($line, "=hex(2):")
    else
        $result = StringInStr($line, "=hex(7):")
    endif
        if $result<>0 then
            $tvname = StringLeft($line,$result-1)
        $result = StringInStr($line, chr(34)&"=")
        $result2 = StringTrimLeft($line,$result+8)
        $result = StringInStr($result2,chr(44)&chr(92))
        if $result = StringInStr($result2,",\") <> 0 then        
        While $result <> 0
            $ln = $ln + 1
            $result2 = StringTrimRight($result2,1)
            $result3 = StringTrimLeft(FileReadLine($file),2)
            $result2 = $result2 & $result3
            $result = StringInStr($result2,",\")
        Wend
            else 
            $ln = 1
        endif
    $TempString = StringSplit($result2,",")
        For $c = 1 to (UBound($TempString,1)) - 1
        If $TempString[$c] = "00" Then
        $d = $d + 1
        If $d = 2 then $output = $output & @CRLF
    EndIf
    If $TempString[$c] <> "00" Then
                $output = $output & chr(dec($TempString[$c]))
        $d = $d - 1
    Endif
        Next
    $d = 10
    $TempString = StringRegExp($output,"[[:alpha:]]\x3A\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(0)
    endif
        $TempString = StringRegExp($output,"\x5C\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x20\x3B]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(1)
    endif
        $TempString = StringRegExp($output,"\x25(?i)programfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    $c = @error
    $r = @extended
        MsgBox(0,"hi",$c&$r)
        AssignPaths(2)
    $TempString = StringRegExp($output,"\x25(?i)commonprogramfiles\x25\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(3)
    endif
        $TempString = StringRegExp($output,"\x25(?i)windir\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(4)
    endif
    $TempString = StringRegExp($output,"\x25(?i)systemroot\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(5)
    endif
    $TempString = StringRegExp($output,"\x25(?i)appdata\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(6)
    endif
    $TempString = StringRegExp($output,"\x25(?i)userprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(7)
    endif
    $TempString = $TempString = StringRegExp($output,"\x25(?i)allusersprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(8)
    endif
    $TempString = StringRegExp($output,"\x25(?i)systemdrive\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?",3)
    if UBound($TempString,1) <> 0 then
        AssignPaths(9)
    endif
    endif
Wend

FileClose($file)
$output = FileSaveDialog( "Choose a name.", @ScriptDir, "Temp File (*.tmp)", 2)
If @error Then Exit
MsgBox(4096,"","You chose " & $output&".tmp")
$file = FileOpen($output&".tmp", 1)
; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
For $c = 1 to $pathnumber
    FileWriteLine($file,$paths[$c])
    MsgBox(0,$paths[$c],$paths[$c])
Next
FileClose($file)

Run("Notepad.exe "&chr(34)&$output&".tmp"&chr(34))
$d = @error
MsgBox(0,"Edit the output file and Confirm",chr(34)&$output&".tmp"&chr(34)&$d)
$file = FileOpen($output&".tmp", 0)
; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
For $c = 1 to $pathnumber
    $line = FileReadLine($file)
    If @error = -1 Then Exit
    MsgBox(0,"hi",$line&$paths[$c])
    $d = @error
    $v = @extended
    $output = StringReplace($filepaths[$c],$paths[$c],$line,1)
    MsgBox(0,"hi",$output&$d&$v&$filepaths[$c])
    $r = $pathkind[$c]
    Select
    case $separators[$c] = ","
        $separator = "\x2C"
    case $separators[$c] = " "
        $separator = "\x20"
    case $separators[$c] = ";"
        $separator = "\x3B"
    case $separators[$c] = chr(34)
        $separator = "\x22"
    EndSelect
    $result2 = StringReplace($modifiedvalues[$c],$filepaths[$c],$output)
    $filepaths[$c] = $output
    $paths[$c] = $line
    if $modifiedvalues[$c] = $modifiedvalues[$c+1] then
        $modifiedvalues[$c] = $output
        $modifiedvalues[$c+1] = $modifiedvalues[$c]
    endif
    $d = @error
    $v = @extended
    MsgBox(0,"hi",$modifiedvalues[$c]&$d&$v)
Next
FileClose($file)
MsgBox(0,"Replace","OK")
$file = FileOpen("file.reg", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
For $c = 1 to $pathnumber
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $lc = $lc + 1
    If $line = $keyname[$c] Then $tkey = $line
    if $mode = "REG_EXPAND_SZ" then
        $result = StringInStr($line, "=hex(2):")
    else
        $result = StringInStr($line, "=hex(7):")
    endif
    if $result <> 0 then
    $tvname = StringLeft($line,$result-1)
    if $tvname = $valuename[$c] then
        $TempString = StringSplit($paths[$c],"")
        _FileWriteToLine($file,$lc,$sText,1)
    endif
    endif
Next

Now I'm stuck into this line:

$line = FileReadLine($file)

Exactly, by some reason the file I temporarily created isn't being read. The file is placed under C:\Documents and Settings\User\Mu documents\Folder. Now this looks bizarre.

Edited by castman

Share this post


Link to post
Share on other sites

About which FileReadLine() are you talking? There are more than one in the code and your code is a mess, sorry, not to insult you of course, but it's terribly hard to read and analyze this kind of source, by cleaning it and moving the offending code into another new fresh *.au3 file you may find the problem easier and faster.

Share this post


Link to post
Share on other sites

First,.... you are a very bad programmer! naming a variable is wrong...

Slow down a bit. He's only been a forum member for about 2 weeks and the very fact that he's trying complex SREs says something to his benefit. Let's consider him a Noob instead of "a very bad programmer" for now and see how that goes. In other words we give him the benefit of the doubt until he proves he doesn't deserve it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Adding to what Authenticity said, you need some better structure to your program. Throw your functions to the bottom, lay out well, use tidy, use better var names and I would seriously add more functions to break up the scrip. It is just too had to follow. Just by playing layout I made it a little more clear but it still needs work. The only thing I added was "AutoItSetOption("MustDeclareVars", 1)". I strongly recommend using it and getting it to compile using it, atm it does not. This will greatly help you reduce errors and bugs in your code.

#include <File.au3>

AutoItSetOption("MustDeclareVars", 1)

Dim $keyname[5000], $valuename[5000], $modifiedvalues[5000], $paths[5000], $filepaths[5000]
Dim $tkey, $tvname, $tvalue, $separator, $pathkind[5000], $lnc[5000], $separators[5000], $TemString[10000], $TmpString[2]
Dim $pathnumber = 0, $r, $c, $d = 0, $output = "", $mode, $k, $v, $ln = 0, $pattern[10]

$pattern[0] = "[[:alpha:]]\x3A\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[1] = "\x5C\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x20\x3B]*"
$pattern[2] = "\x25(?i)programfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[3] = "\x25(?i)commonprogramfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[4] = "\x25(?i)programfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[5] = "\x25(?i)windir\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[6] = "\x25(?i)systemroot\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[7] = "\x25(?i)appdata\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[8] = "\x25(?i)userprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$pattern[9] = "\x25(?i)allusersprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*"
$answer = MsgBox(4, "What kind of value you want to convert ?", "REG_EXPAND_SZ or REG_MULTI_SZ ?")

If $answer = 7 Then
    $mode = "REG_MULTI_SZ"
Else
    $mode = "REG_EXPAND_SZ"
EndIf

MsgBox(0, "Mode", $mode)
$file = FileOpen("file.reg", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $result = StringInStr($line, "[HKEY_")
    If $result <> 0 Then $tkey = $line
    If $mode = "REG_EXPAND_SZ" Then
        $result = StringInStr($line, "=hex(2):")
    Else
        $result = StringInStr($line, "=hex(7):")
    EndIf
    If $result <> 0 Then
        $tvname = StringLeft($line, $result - 1)
        $result = StringInStr($line, Chr(34) & "=")
        $result2 = StringTrimLeft($line, $result + 8)
        $result = StringInStr($result2, Chr(44) & Chr(92))
        If $result = StringInStr($result2, ",\") <> 0 Then
            While $result <> 0
                $ln = $ln + 1
                $result2 = StringTrimRight($result2, 1)
                $result3 = StringTrimLeft(FileReadLine($file), 2)
                $result2 = $result2 & $result3
                $result = StringInStr($result2, ",\")
            WEnd
        Else
            $ln = 1
        EndIf
        $TempString = StringSplit($result2, ",")
        For $c = 1 To (UBound($TempString, 1)) - 1
            If $TempString[$c] = "00" Then
                $d = $d + 1
                If $d = 2 Then $output = $output & @CRLF
            EndIf
            If $TempString[$c] <> "00" Then
                $output = $output & Chr(Dec($TempString[$c]))
                $d = $d - 1
            EndIf
        Next
        $d = 10
        $TempString = StringRegExp($output, "[[:alpha:]]\x3A\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(0)
        EndIf
        $TempString = StringRegExp($output, "\x5C\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x20\x3B]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(1)
        EndIf
        $TempString = StringRegExp($output, "\x25(?i)programfiles\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        $c = @error
        $r = @extended
        MsgBox(0, "hi", $c & $r)
        AssignPaths(2)
        $TempString = StringRegExp($output, "\x25(?i)commonprogramfiles\x25\x5C[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(3)
        EndIf
        $TempString = StringRegExp($output, "\x25(?i)windir\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(4)
        EndIf
        $TempString = StringRegExp($output, "\x25(?i)systemroot\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(5)
        EndIf
        $TempString = StringRegExp($output, "\x25(?i)appdata\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(6)
        EndIf
        $TempString = StringRegExp($output, "\x25(?i)userprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(7)
        EndIf
        $TempString = $TempString = StringRegExp($output, "\x25(?i)allusersprofile\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(8)
        EndIf
        $TempString = StringRegExp($output, "\x25(?i)systemdrive\x25\x5C(?-i)[^\x2E\x22\x2C\x5C\x3A\x3B][^\x2E\x22\x2C\x3A\x3B]*\x5C[^\x2E\x22\x2C\x5C\x3A\x3B]*\x2E[^\x2E\x22\x2C\x5C\x3A\x3B\x20]*[\x22\x2C\x20\x3B\x3A]?", 3)
        If UBound($TempString, 1) <> 0 Then
            AssignPaths(9)
        EndIf
    EndIf
WEnd

FileClose($file)
$output = FileSaveDialog("Choose a name.", @ScriptDir, "Temp File (*.tmp)", 2)
If @error Then Exit
MsgBox(4096, "", "You chose " & $output & ".tmp")
$file = FileOpen($output & ".tmp", 1)
; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
For $c = 1 To $pathnumber
    FileWriteLine($file, $paths[$c])
    MsgBox(0, $paths[$c], $paths[$c])
Next
FileClose($file)

Run("Notepad.exe " & Chr(34) & $output & ".tmp" & Chr(34))
$d = @error
MsgBox(0, "Edit the output file and Confirm", Chr(34) & $output & ".tmp" & Chr(34) & $d)
$file = FileOpen($output & ".tmp", 1)
; Check if file opened for writing OK


If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $c = 1 To $pathnumber
    $line = FileReadLine($file)
    If @error = -1 Then Exit
    $paths[$c] = $line
    MsgBox(0, "hi", $line & $paths[$c])
    StringRegExpReplace($filepaths[$c], "[^\x2E\x22\x2C]*\x5C", $paths[$c])
    $r = $pathkind[$c]
    Select
        Case $separators[$c] = ","
            $separator = "\x2C"
        Case $separators[$c] = " "
            $separator = "\x20"
        Case $separators[$c] = ";"
            $separator = "\x3B"
        Case $separators[$c] = Chr(34)
            $separator = "\x22"
    EndSelect
    StringRegExpReplace($modifiedvalues[$c], $pattern[$r] & $separator, $filepaths[$c])
    MsgBox(0, "hi", $modifiedvalues[$c])
Next
FileClose($file)
MsgBox(0, "Replace", "OK")
$file = FileOpen("file.reg", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

For $c = 1 To $pathnumber
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    $lc = $lc + 1
    If $line = $keyname[$c] Then $tkey = $line
    If $mode = "REG_EXPAND_SZ" Then
        $result = StringInStr($line, "=hex(2):")
    Else
        $result = StringInStr($line, "=hex(7):")
    EndIf
    If $result <> 0 Then
        $tvname = StringLeft($line, $result - 1)
        If $tvname = $valuename[$c] Then
            $TempString = StringSplit($paths[$c], "")
            _FileWriteToLine($file, $lc, $sText, 1)
        EndIf
    EndIf
Next


; Read in lines of text until the EOF is reached
Func AssignPaths($d)
    For $c = 0 To (UBound($TempString, 1)) - 1
        $pathnumber = $pathnumber + 1
        $keyname[$pathnumber] = $tkey
        $valuename[$pathnumber] = $tvname
        If $TempString[$c] <> "" Then
            $tvalue = StringRight($TempString[$c], 1)
        EndIf
        If $tvalue = Chr(34) Or $tvalue = "," Or $tvalue = " " Or ";" Then
            $tvalue = StringTrimRight($TempString[$c], 1)
            $separators[$pathnumber] = StringRight($TempString[$c], 1)
            $filepaths[$pathnumber] = StringTrimRight($TempString[$c], 1)
        Else
            $tvalue = $TempString[$c]
            $filepaths[$pathnumber] = $TempString[$c]
        EndIf
        $TmpString = StringRegExp($tvalue, "[^\x2E\x22\x2C]*\x5C", 1)
        If UBound($TmpString, 1) <> 0 Then
            $tvalue = $TmpString[0]
        EndIf
        $paths[$pathnumber] = $tvalue
        $modifiedvalues[$pathnumber] = $output
        $lnc[$pathnumber] = $ln
        $pathkind[$pathnumber] = $d
        MsgBox(0, "hi", $paths[$pathnumber])
    Next
EndFunc   ;==>AssignPaths

Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites

Slow down a bit. He's only been a forum member for about 2 weeks and the very fact that he's trying complex SREs says something to his benefit. Let's consider him a Noob instead of "a very bad programmer" for now and see how that goes. In other words we give him the benefit of the doubt until he proves he doesn't deserve it.

Oops, I just got mad after reading his code.

It's like reading a obfuscated code no worst.

>_< Anyway what is he trying to do? It just confuses me. Here... he is trying to convert one type of reg to another. What what does the %system.>% need to do with this?

Share this post


Link to post
Share on other sites

Thanks by reading. I'm not trying to convert a value to another (My Program won't have this function), I just want replace these values inside the .reg file.

I discovered what was wrong, I had to set FileOpen with the 0 parameter, like: $file = FileOpen($output&".tmp", 0).

Since I don't want post my code all again to avoid cluttering, I decided to edit it in my previous post.

My sincerely thanks for all the attention. :(

And I even didn't know that this function already existed "_PathSplit". And also I think they cleared out some error messages to make AutoIt faster. >_< My BIG Problem with this function is that I would need to analyze, if it supports all file paths I need.

Now I'm stuck into this line:

$output = StringRegExpReplace($filepaths[$c],"([^\x2E\x22\x2C\x3B]*\x5C)",$paths[$c],0)

Try it with this value:

C:\Documénts ând Sàttings\BRAR\FOOT.ext"

Imagining you substitute this folder name BRAR to BRA789, it becomes

C:Documénts ând SàttingsBRA789FOOT.ext"

Why this StringRegExpReplace is Eating the backslashes ??

Share this post


Link to post
Share on other sites

Depending on the value of the replacement string. Don't forget that what participate in the match gets replaced as well, so you may want to add another \ instead of the replacement. By the way, why are you using all the ASCII hexadecimal values instead of the clean character representations?

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

It's because I want to support most ANSI values. I have the ANSI table that is 256 characters, regedit works this way. And I might need it work this way to deal with non-english languages.

Replying answering what I need: There is no way to join these two parts of the pattern, so it replaces only both as the whole?

Edited by castman

Share this post


Link to post
Share on other sites

No matter how you'll capture or otherwise not capture. What participates in the match is subject to the replacement clause.

; Whether it's: 
; "(\d+)\\"
; "(\d+)(?>\\)"
; ...
ConsoleWrite(StringRegExpReplace("123\456\", "\d+\\", "AAA\\") & @LF)

Read the function description. You have a predefined set of character classes. You can match all non printing this way [^[:print:]]+ for example.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Thanks :( ! I substituted this line by:

$output = StringRegExpReplace($filepaths[$c],"[^\x2E\x22\x2C\x3B]*\z\x5C",$paths[$c],0)

Edit2: I thought it had work but it didn't. However, I realized that I didn't need to use StringRegExpReplace, StringReplace satisfied my needs.

Now I'm stucked in the next line: $modifiedvalues[$c] = StringRegExpReplace($modifiedvalues[$c],$pattern[$r]&$separator,$filepaths[$c], 1)

Same behavior to solve.

The concept is harder now. The modified value may have two different file paths with similar patterns. I suppose I have to think more!

Edit1: I modified that one source code post according. Edit2: Again!

The RegExp Noobie >_< can take some rest now!

Edit3: I was thinking about how can I put all this into arrays without repeating strings, then I had the idea of dimensional loops to categorize the arrays.

$keyname and $valuesperkey are 1st dimension

$valuename, $linescount, $modifiedvalues and $pathspervalue are 2nd dimension

$filepaths, $paths, $pathkind and $separator (I am thinking about removing these two last) are 3rd dimension

LIKE

for $k = 1 to UBound($keyname)
for 1 to $valuesperkey[$k]
$v = $v + 1
for 1 to $pathspervalue[$v]
$p = $p + 1
;filereadline from tempfile and $modifiedvalues replace
next
;knowledge only after further tests
next
next
Edited by castman

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  
Followers 0