Sign in to follow this  
Followers 0
Greenhorn

Compare & choose lines of a file

6 posts in this topic

#1 ·  Posted (edited)

Moin Moin@allFreax,

I have a problem by creating a Script with the function to

=> read a (*.reg) file

=> read the first $line of (*.reg)

=> compare the lines

==> if any line of (*.reg) includes the same String of $line Then

===> write only $line into a destination file

==> elseif $line does'nt compare to any line of (*.reg) Then

===> read next line of (*.reg)

etc., etc.

I want to readout the [KEYs] of a (*.reg) file and create a (*.au3) file wth an unregister function (RegDelete...).

So, for that I don't need the Subkeys of the (*.reg) file.

That's what I want to "Filter": Only writing the "HeadKey" into an AutoIt.au3 -> ignore the SubKeys !

Here is the general Script...

The Bug is the Func Unregister()

Global $regsource
Global $regdest
Global $file
Global $RegScript
Global $UnRegScript
Global $msg, $line, $keyname, $value, $value1, $value2, $filename

#include
#include
#include
#include


; GUI Reg2Au3 Converter
GUICreate("Reg2Au3 Converter", 392, 316, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$Group_1 = GUICtrlCreateGroup("", 10, 20, 370, 150)
$Label_1 = GUICtrlCreateLabel("Quelldatei auswählen", 30, 50, 140, 20)
$Input_1 = GUICtrlCreateInput("", 30, 70, 200, 20)
$Button_1 = GUICtrlCreateButton("&Durchsuchen...", 260, 70, 100, 20)
$Label_2 = GUICtrlCreateLabel("Zieldatei auswählen", 30, 110, 120, 20)
$Input_2 = GUICtrlCreateInput("", 30, 130, 200, 20)
$Button_2 = GUICtrlCreateButton("&Durchsuchen...", 260, 130, 100, 20)

$Unreg = GUICtrlCreateCheckbox("UnRegScript erstellen", 20, 180)

$SameFile = GUICtrlCreateRadio('In "RegScript.au3" schreiben', 20, 220)
$OwnFile = GUICtrlCreateRadio('"UnRegScript.au3" erstellen', 20, 250)

$Button_Start = GUICtrlCreateButton("&Start", 280, 240, 80, 20)
$Button_Beenden = GUICtrlCreateButton("&Beenden", 280, 280, 80, 20)

GUISetState()
GUICtrlSetState($SameFile, $GUI_DISABLE)
GUICtrlSetState($OwnFile, $GUI_DISABLE)

While 1

$msg = GUIGetMsg()
Select
Case $msg = 0
ContinueLoop
Case $msg = $Button_1
$regsource = FileOpenDialog("Registrierungsdatei wählen", @MyDocumentsDir, "(*.reg)", 1 + 4)
ControlSetText("Reg2Au3 Converter", "", $Input_1, $regsource)
Case $msg = $Button_2
DestDir()
If @error Then MsgBox(4096, "", "Save cancelled.")
Case $msg = $Unreg
If GUICtrlRead($Unreg) = $GUI_CHECKED Then
GUICtrlSetState($SameFile, $GUI_ENABLE)
GUICtrlSetState($OwnFile, $GUI_ENABLE)
Endif

Case $msg = $SameFile
If GUICtrlRead($SameFile) = $GUI_CHECKED Then $UnRegScript = FileOpen($regdest, 1)
Case $msg = $OwnFile
If GUICtrlRead($OwnFile) = $GUI_CHECKED Then
$array = StringSplit($regdest, "\")
_ArrayPop($array)
$string = _ArrayToString($array, "\", 1)
$UnRegScript = FileOpen($string & "\UnRegScript.au3", 2)
EndIf
Case $msg = $Button_Start
If GUICtrlRead($Unreg) = $GUI_CHECKED Then
Register()
Unregister()
Else
Register()
EndIf
FileClose($file)
FileClose($RegScript)
FileClose($UnRegScript)
Case $msg = $Button_Beenden
Exit
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case Else
;;;
EndSelect
WEnd

Exit

; Funktion .reg zu .au3
Func Register()

$file = FileOpen($regsource, 0)
$RegScript = FileOpen($regdest, 2)


If $file = -1 Then
MsgBox(0, "Error", "Unable to open .reg file.")
Exit
ElseIf $RegScript = -1 Then
MsgBox(0, "Error", "Unable to open RegScript.au3.")
Exit
Else
If $file = -1 And $RegScript = -1 Then
MsgBox(0, "Error", "Unable to open any file.")
Exit
EndIf
EndIf

$RegASCI = StringReplace(FileGetShortName($regsource), ".reg", ".old")
RunWait(@ComSpec & " /c type " & FileGetShortName($regsource) & ">" & $RegASCI, @MyDocumentsDir)
FileCopy($RegASCI, $regsource, 1)
FileDelete($RegASCI)

While 1
$line = FileReadLine($file)
If @error = -1 Then ExitLoop
Local $sPattern, $vResult, $clip, $clip1

$sPattern = "[][]"
$vResult = StringRegExp($line, $sPattern, 1)
If @extended Then
$keyname = StringRegExpReplace($line, $sPattern, '"')
FileWriteLine($RegScript, "RegWrite(" & $keyname & ")")
EndIf

$sPattern = "(@=)"
$vResult = StringRegExp($line, $sPattern, 1)
If @extended Then
$clip = StringRegExpReplace($line, $sPattern, "")
$clip_0 = StringRegExpReplace($clip, "\\" & '"', "")
$value = StringRegExpReplace($clip_0, "\\\\", "\\", 0)
FileWriteLine($RegScript, "RegWrite(" & $keyname & ', "", "REG_SZ", ' & $value & ")")
EndIf

$sPattern = '("=")'
$vResult = StringRegExp($line, $sPattern, 1)
If @extended Then
$clip_1 = StringRegExpReplace($line, '"="', '", "REG_SZ", "')
$value1 = StringRegExpReplace($clip_1, "\\\\", "\\", 0)
FileWriteLine($RegScript, "RegWrite(" & $keyname & "," & $value1 & ")")
EndIf
$sPattern = "(=dword:)"
$vResult = StringRegExp($line, $sPattern, 1)
If @extended Then
$hex = StringRight($line, 8)
$dec = Dec($hex)
$value2 = StringRegExpReplace($line, "=dword:" & $hex, ', "REG_DWORD", "' & $dec)
FileWriteLine($RegScript, "RegWrite(" & $keyname & "," & $value2 & '")')
EndIf
WEnd

EndFunc ;==>Register

; Funktion Unregister

Func Unregister()

; aktuelle Prod oder Beta benutzen


Dim $arFile, $write, $tmp, $last


If _FileReadToArray($regsource, $arFile) = 0 Then
MsgBox(0, 'Achtung', "Datei konnte nicht eingelesen werden!")
Exit
EndIf

For $i = 1 To UBound($arFile)
$tmp = $arFile[$i]
If StringInStr($arFile[$i], "[") Then
$tmp1 = _StringBetween($tmp, "[", "]")
$last1 = _StringBetween($last, "[", "]")

If StringInStr($tmp1, $last1) Then
$last = $arFile[$i]
_ArrayDelete($arFile, $i)

Else
If Not StringInStr($tmp1, $last1) Then
FileWriteLine($UnRegScript, $arFile[$i])
$last = $arFile[$i]

EndIf
EndIf
EndIf

Next
FileClose($UnRegScript)
Exit

EndFunc ;==>Unregister

; Funktion Zieldatei
Func DestDir()
; option 2 = dialog remains until valid path/file selected
$regdest = FileSaveDialog("Zielpfad wählen", @MyDocumentsDir, "Scripts (*.aut;*.au3)", 2, "RegScript.au3")
$check = StringRegExp($regdest, "(.au3)", 1)
; Dateiendung .au3 anhängen
Select
Case @error = 0
if @extended then ControlSetText("Reg2Au3 Converter", "", $Input_2, $regdest)
Case @error = 1
ControlSetText("Reg2Au3 Converter", "", $Input_2, $regdest & ".au3")
EndSelect
EndFunc

Example.reg :

CODE
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3]

[HKEY_LOCAL_MACHINE\SOFTWARE\AutoIt v3\AutoIt]

"InstallDir"="D:\\Programme\\AutoIt3"

"Version"="v3.2.2.0"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script]

@="AutoIt v3 Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\DefaultIcon]

@="D:\\Programme\\AutoIt3\\Icons\\filetype1.ico"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell]

@="Edit"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Compile]

@="Compile Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Compile\Command]

@="D:\\Programme\\AutoIt3\\Aut2Exe\\Aut2Exe.exe /in \"%l\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Compile with Options]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Compile with Options\Command]

@="\"D:\\Programme\\AutoIt3\\SciTE\\AutoIt3Wrapper\\AutoIt3Wrapper.exe\" /in \"%l\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Edit]

@="Edit Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Edit\Command]

@="\"D:\\Programme\\AutoIt3\\SciTE\\SciTE.exe\" \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Open]

@="Open"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Open\Command]

@="\"D:\\Programme\\AutoIt3\\SciTE\\SciTE.exe\" \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Run]

@="Run Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Run\Command]

@="D:\\Programme\\AutoIt3\\AutoIt3.exe \"%1\" %*"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Tidy]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3Script\Shell\Tidy\Command]

@="\"D:\\Programme\\AutoIt3\\SciTE\\Tidy\\SciTE.exe\" \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3XScript]

@="AutoIt v3 Encoded Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3XScript\DefaultIcon]

@="D:\\Programme\\AutoIt3\\Icons\\filetype3.ico"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3XScript\Shell]

@="Run"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3XScript\Shell\Run]

@="Run Script"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoIt3XScript\Shell\Run\Command]

@="D:\\Programme\\AutoIt3\\AutoIt3.exe \"%1\" %*"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoItX3.Control]

@="AutoItX3 Class"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoItX3.Control\CLSID]

@="{1A671297-FA74-4422-80FA-6C5D8CE4DE04}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoItX3.Control\CurVer]

@="AutoItX3.Control.1"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoItX3.Control.1]

@="AutoItX3 Class"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AutoItX3.Control.1\CLSID]

@="{1A671297-FA74-4422-80FA-6C5D8CE4DE04}"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AutoIt3.exe]

@="D:\\Programme\\AutoIt3\\AutoIt3.exe"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Aut2Exe.exe]

@="D:\\Programme\\AutoIt3\\Aut2Exe\\Aut2Exe.exe"

[HKEY_CURRENT_USER\Software\Classes\Applications\AutoIt3.exe]

[HKEY_CURRENT_USER\Software\Classes\Applications\AutoIt3.exe\shell]

[HKEY_CURRENT_USER\Software\Classes\Applications\AutoIt3.exe\shell\open]

[HKEY_CURRENT_USER\Software\Classes\Applications\AutoIt3.exe\shell\open\command]

@="\"D:\\Programme\\AutoIt3\\AutoIt3.exe\" \"%1\""

[HKEY_CURRENT_USER\Software\AutoIt v3]

[HKEY_CURRENT_USER\Software\AutoIt v3\AU3Info]

"WinX"=dword:0000037b

"WinY"=dword:0000007e

"WinW"=dword:0000012c

"WinH"=dword:000001c2

"ColorMode"=dword:00000001

"CoordMode"=dword:00000000

"HighlightControls"=dword:00000000

"Magnify"=dword:00000000

"AlwaysOnTop"=dword:00000001

"HighlightColor"=dword:00000000

[HKEY_CURRENT_USER\Software\AutoIt v3\Aut2Exe]

"LastExeDir"="My Documents"

"LastScriptDir"="My Documents"

"LastIconDir"="D:\\Programme\\AutoIt3\\Aut2Exe\\Icons"

"LastIcon"=""

"LastCompression"=dword:00000002

"AllowDecompile"=dword:00000001

"UseUPX"=dword:00000001

[HKEY_CURRENT_USER\Software\AutoIt v3\Exe2Aut]

"LastScriptDir"="C:\\Dokumente und Einstellungen\\Administrator\\Desktop\\"

"LastExeDir"="F:\\Temp\\AutoIt3portable\\Aut2Exe\\"

I'll become very happy, if anybody could help me...

Nomen est Omen => I'm a bloody Newbie !

Greetz

p.s.: Please excuse my bad english :)

Edited by Greenhorn

Share this post


Link to post
Share on other sites



Autoit does not support writting to the specific line. Only append and rewrite.

There is a _FileToArray or _ArrayFromFile (I dont have access to the help file at the moment).

Use that and loop through the arrays change the values as you desire. Then write the array to the file again. Maybe make a backup with FIleMove or FileCopy.

If you dont know arrays studdy this tutorial.

Share this post


Link to post
Share on other sites

@Uten

Thanx@first !!!

Share this post


Link to post
Share on other sites

Juhuuuuu! :)

I've got it :

#include <File.au3>
#include <String.au3>
#include <Array.au3>

$RegASCI = StringReplace(FileGetShortName("D:\Eigene Dateien\Register-1.reg"), ".reg", ".old")
    RunWait(@ComSpec & " /c type " & FileGetShortName("D:\Eigene Dateien\Register-1.reg") & ">" & $RegASCI, @MyDocumentsDir)
    FileCopy($RegASCI, "D:\Eigene Dateien\Register-1.reg", 1)
    FileDelete($RegASCI)

Dim $arFile, $str, $v, $tmp
Dim $pfadRead = FileOpen("D:\Eigene Dateien\Register-1.reg", 0)
Dim $pfadWrite = FileOpen("Temp.txt", 2) 
Dim $oldline = "w"

$result = FileOpen(@MyDocumentsDir & "\RegScript.txt", 2)

While 1    
    $line = FileReadLine($pfadRead)
    If @error = -1 Then ExitLoop
    
    Local $sPattern, $vResult

    $sPattern = "[][]"
    $vResult = StringRegExp($line, $sPattern, 1)
    If @extended Then
        $keyname = StringRegExpReplace($line, $sPattern, "")
        FileWriteLine($pfadWrite, $keyname)        
    EndIf    
WEnd
FileClose($pfadWrite)
If _FileReadToArray("Temp.txt", $arFile) = 0 Then
    MsgBox(0, 'Achtung', "Datei konnte nicht eingelesen werden!")
    Exit
EndIf
For $i = 1 To UBound($arFile)-1
    $tmp = $arFile[$i]
    $tmp1 = $tmp & "\"
    $Pos = _ArraySearch ($arFile, $tmp1, 0, 0, 0, True)
    Select
        Case $Pos = -1
            Sleep(0)
        Case Else
            If $tmp1 = "\" Then ExitLoop
            $v = _ArrayDelete($arFile, $Pos)
            $i = $i-1
    EndSelect
Next
_ArrayDelete($arFile, 0)
_FileWriteFromArray("Temp1.txt", $arFile)

FileClose("Temp1.txt")
FileClose($pfadRead)
Exit

Greetz

Share this post


Link to post
Share on other sites

Should not these two lines change place?

_ArrayDelete($arFile, 0)
_FileWriteFromArray("Temp1.txt", $arFile)

If it works _ArrayDelete does not work as I expect from the name. Don't know the function and don't have the helpfile available so I'm just wondering..:)

Share this post


Link to post
Share on other sites

Without reading the script, I'm guessing he's deleting '0' so it doesn't write the array size. If that's the case, he should instead, use the base option in _filewritefromarray.

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