Sign in to follow this  
Followers 0
hellemon

Change a entry in a other textfile

7 posts in this topic

Hello together,

This is my first Script with AutoIT,

and i have a little problem and hope you can help me.

Example

i have a CFG File (test.cfg) with the following entries

#wcontrol

server:<servername1>,<10.0.0.100>

computerid:1234g0j

macadresse:001100110022

and i have a other textfile (servername.txt) with the new servername entries

<servername2>,<10.0.0.102>

I would like to change the <servername> and <ip adresse> in the CFG File

how can i make that

This is my Script but it is not working

$servername = FileOpen("servername.txt", 0)
$var = FileReadLine($servername)
FileReadLine ("servername.txt", 1)
FileClose($servername)

;##############################

$cfgfile = FileOpen("test.cfg", 1)
FileWriteLine($cfgfile, "server:$var")
FileClose($cfgfile)

thank you very much

Hellemon

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hello together,

This is my first Script with AutoIT,

and i have a little problem and hope you can help me.

Example

i have a CFG File (test.cfg) with the following entries

#wcontrol

server:<servername1>,<10.0.0.100>

computerid:1234g0j

macadresse:001100110022

and i have a other textfile (servername.txt) with the new servername entries

<servername2>,<10.0.0.102>

I would like to change the <servername> and <ip adresse> in the CFG File

how can i make that

This is my Script but it is not working

$servername = FileOpen("servername.txt", 0)
$var = FileReadLine($servername)
FileReadLine ("servername.txt", 1)
FileClose($servername)

;##############################

$cfgfile = FileOpen("test.cfg", 1)
FileWriteLine($cfgfile, "server:$var")
FileClose($cfgfile)

thank you very much

Hellemon

First, you shouldn't have posted this request in this Forum! It should have been in the Support Forum! If you continue to post help requests in the wrong place, you'll just get a lot of people yelling at you and no help!

That said, you might try:

$var1 = FileRead("servername.txt", FileGetSize ( "servername.txt" ))

;##############################

$var2 = FileRead("test.cfg", FileGetSize ( "test.cfg" ))
$iVar1 = StringInStr($var2,"<",0,1)
$iVar2 = StringInStr($var2,">",0,2)
$iStrLen = $iVar2 -$iVar1 + 1
$sSearchStr = StringMid($var2,$iVar1,$iStrLen)
$sVar3 = StringReplace ( $var2, $sSearchStr, $var1 )
FileMove("test.cfg","Test.cfg.old",9)
FileWrite("test.cfg",$sVar3)

This worked for me with your example files, I didn't do any error checking but you should.

From looking at your examples, I'm not sure this is what you want, but it is what I understood you to ask for.

B)

Edited by Gene

[font="Verdana"]Thanks for the response.Gene[/font]Yes, I know the punctuation is not right...

Share this post


Link to post
Share on other sites

Hello Gene,

Thanks for you help, but it´s not really what i want. But it´s OK.

Thanks and Have a Lot of fun

Hellemon

Share this post


Link to post
Share on other sites

how can i make that

Get the new and old server names by grabbing the lines in each file that start with "server:", then use _ReplaceStringInFile to replace oldstring with newstring.

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format

Share this post


Link to post
Share on other sites

Hello Gene,

Thanks for you help, but it´s not really what i want. But it´s OK.

Thanks and Have a Lot of fun

Hellemon

Maybe you should give a more complete example of what you want to do.

If your files have more data than you showed, give more records of the set for each file, so it's clear what end result you want, and what the environment is. B)


[font="Verdana"]Thanks for the response.Gene[/font]Yes, I know the punctuation is not right...

Share this post


Link to post
Share on other sites

Hello Gene,

Thank you, but i found the solution.....

; Neuen Server holen und in Variable $newserver speichern
$fileserver = FileOpen("server.txt",0)
; Überprüft ob die server.txt geöffnet werden kann
If $fileserver = -1 Then
    MsgBox(0, "Fehler", "Kann server.txt nicht öffnen.")
    Exit
EndIf
$newserver = FileReadLine($fileserver)
FileClose($fileserver)


$file = FileOpen("wcontrol.cfg", 0)
; Überprüft ob die wcontrol.cfg geöffnet werden kann
If $file = -1 Then
    MsgBox(0, "Fehler", "Kann wcontrol.cfg nicht öffnen.")
    Exit
EndIf

; Legt neue Datei wcontrol.new an
$filenew = fileOpen("wcontrol.new", 2)
; Überprüft ob die wcontrol.new geöffnet werden kann
If $filenew = -1 Then
    MsgBox(0, "Fehler", "Kann wcontrol.new nicht öffnen.")
    Exit
EndIf

; Neue Datei wcontrol.new schreiben
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringLeft($line,6)="Server" Then
        $line="Server=" & $newserver
    EndIf
    $result=FileWriteLine($filenew, $line)  
    if $result=0 then
        MsgBox(0, "Fehler", "Kann wcontrol.new nicht schreiben.")
        FileClose($file)
        FileClose($filenew)
        FileDelete("wcontrol.new")
        Exit
    EndIf
Wend

FileClose($file)
FileClose($filenew)

; Der Schreibschutz von der datei wcontrol.cfg wird empfernt
FileSetAttrib("wcontrol.cfg", "-R", 0)
If @error Then 
    MsgBox(4096,"Fehler", "Schreibschutz von der wcontrol.cfg kann nicht empfernen werden.")
    Exit
EndIf

FileMove("wcontrol.new", "wcontrol.cfg",1)

Hava Fun

regards, Helmut

Share this post


Link to post
Share on other sites

Hello Gene,

Thank you, but i found the solution.....

; Neuen Server holen und in Variable $newserver speichern
$fileserver = FileOpen("server.txt",0)
; Überprüft ob die server.txt geöffnet werden kann
If $fileserver = -1 Then
    MsgBox(0, "Fehler", "Kann server.txt nicht öffnen.")
    Exit
EndIf
$newserver = FileReadLine($fileserver)
FileClose($fileserver)
$file = FileOpen("wcontrol.cfg", 0)
; Überprüft ob die wcontrol.cfg geöffnet werden kann
If $file = -1 Then
    MsgBox(0, "Fehler", "Kann wcontrol.cfg nicht öffnen.")
    Exit
EndIf

; Legt neue Datei wcontrol.new an
$filenew = fileOpen("wcontrol.new", 2)
; Überprüft ob die wcontrol.new geöffnet werden kann
If $filenew = -1 Then
    MsgBox(0, "Fehler", "Kann wcontrol.new nicht öffnen.")
    Exit
EndIf

; Neue Datei wcontrol.new schreiben
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    If StringLeft($line,6)="Server" Then
        $line="Server=" & $newserver
    EndIf
    $result=FileWriteLine($filenew, $line)    
    if $result=0 then
        MsgBox(0, "Fehler", "Kann wcontrol.new nicht schreiben.")
        FileClose($file)
        FileClose($filenew)
        FileDelete("wcontrol.new")
        Exit
    EndIf
Wend

FileClose($file)
FileClose($filenew)

; Der Schreibschutz von der datei wcontrol.cfg wird empfernt
FileSetAttrib("wcontrol.cfg", "-R", 0)
If @error Then 
    MsgBox(4096,"Fehler", "Schreibschutz von der wcontrol.cfg kann nicht empfernen werden.")
    Exit
EndIf

FileMove("wcontrol.new", "wcontrol.cfg",1)

Hava Fun

regards, Helmut

Great! B)

[font="Verdana"]Thanks for the response.Gene[/font]Yes, I know the punctuation is not right...

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