Jump to content

can I edit a line in a .txt file?


Recommended Posts

I am trying to edit a config file (cfg file below)

Say I want to edit maxplayers = 8 (line 24) to 'maxplayers = 12'

How do I do this? I've tried FileWrite & FileLineWrite but they add a line at the bottom

// DISPLAY
video_type     = 0
game_res         = 4
no_anim       = 0
window_size   = 1
music_volume     = 255
gamma           = 128
turbo           = 0

// CONTROLS
crosshairs     = 1
showgun       = 1
picinpic         = 0

// MULTIPLAYER
login_password   = ""
game_password   = ""
squad_password   = ""
game_name       = "server test2"
phone_number     = "and"
internet_address = "0.0.0.0"
mp_service     = 4
mp_gametype   = 2
maxplayers     = 8
dedicated       = 0
com_port         = 0
baud_rate       = 0
max_team_lives   = 100
max_kills       = 82
team_req         = 0
attributes     = 131
startdelay     = 0
destroybuild     = 1
deathmes         = 1
replay         = 2
time_limit     = 32
koth_limit     = 5
timeout       = 0
mp_numteams   = 2
gpsicon       = 1
sp_wind       = 0
showtags         = 1
contest       = 0

// MAP
map_infrared     = 1
map_iff       = 1
map_AWAC         = 1

// SYSTEM
preempt_pff   = 0
whichbrowser     = 0
country       = "no land"
red_password     = ""
blue_password   = ""
yellow_password  = ""
violet_password  = ""
mp_verbose     = 1

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
Share on other sites

this file is being use by another application and that is how it writes it out,

I'd like to be able to control the way it works by editing its config file directly

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
Share on other sites

You could probably change the // to [] like the above poster said. Thats actually the only thing you need to change. However the script would haveto change it back to the way it was. I assume thats a server config for a game and the game wouldnt understand [] i guess.

The problem with using IniRead/IniWrite is that you need the [] or it wont find what you are looking for :P

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

yeah, it's server config for a game.

How do I rewrite the // to [] ?

I figure I have to open/edit the file first to rewrite the // to [] , then use IniWrite to edit the settings, then switch [] to //

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
Share on other sites

You only haveto add a [] at the beginning of the file. For example [GAME], then you have IniWrite do something like this:

IniWrite($File, "GAME", "Maxplayers", "12")

The thing is i dont know how to make FileWrite change the first line. The config never repeats a variable, which is a good thing... thats why you only need 1 [GAME] at top.

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

  • Moderators

Here try this, should at least get you started before someone makes it better:

#include <file.au3>
$File = FileOpenDialog("FILE TO WORK WITH", @ScriptDir, "ALL (*.*)")

Dim $Change = "maxplayers"
Dim $Value = 7 
ChangeSomething($File, $Change, $Value)

Func ChangeSomething(ByRef $FilePath, ByRef $ChangeWhat, $Value)
    Local $nArray
    Local $NewFile = @ScriptDir & "/TempTest.txt"
    _FileReadToArray($FilePath, $nArray)
    _FileCreate($NewFile)
    For $i = 1 To UBound($nArray) - 1
        If StringInStr($nArray[$i], $ChangeWhat) Then
            $Split = StringSplit($nArray[$i], '=')
            $nArray[$i] = StringReplace($nArray[$i], $Split[2], " " & $Value)
        EndIf
        FileWriteLine($NewFile, $nArray[$i])
    Next
    FileDelete($FilePath)
    FileMove($NewFile, $FilePath)
EndFunc

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

When I try to call the function I get an error, is there a different way to call functions with refences?

GUICtrlCreateLabel ("Maximum Players:", 30,60,90,20)
$MaxPlayers = GUICtrlCreateCombo ("", 117,57,40,80)
GUICtrlSetData(-1,"1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16", $gMaxPlayers)
        
Dim $Change = "maxplayers"
Dim $Value = $MaxPlayers

GUICtrlSetOnEvent($MaxPlayers, "ChangeSomething($File, $Change, $Value)" )

Func ChangeSomething(ByRef $FilePath, ByRef $ChangeWhat, $Value)
    Local $nArray
    Local $NewFile = @ScriptDir & "/TempTest.txt"
    _FileReadToArray($FilePath, $nArray)
    _FileCreate($NewFile)
    For $i = 1 To UBound($nArray) - 1
        If StringInStr($nArray[$i], $ChangeWhat) Then
            $Split = StringSplit($nArray[$i], '=')
            $nArray[$i] = StringReplace($nArray[$i], $Split[2], " " & $Value)
        EndIf
        FileWriteLine($NewFile, $nArray[$i])
    Next
    FileDelete($FilePath)
    FileMove($NewFile, $FilePath)
EndFunc
Edited by cyanidemonkey

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
Share on other sites

  • Moderators

Does this work?

GUICtrlCreateLabel ("Maximum Players:", 30,60,90,20)
$MaxPlayers = GUICtrlCreateCombo ("", 117,57,40,80)
GUICtrlSetData(-1,"1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16", $gMaxPlayers)

GUICtrlSetOnEvent($MaxPlayers, "CallChanges")

Func CallChanges()
    Local $File = 'FilePath'
    Local $Change = 'maxplayers'
    Local $Value = GUICtrlRead($MaxPlayers)
    ChangeSomething($File, $Change, $Value)
EndFunc

Func ChangeSomething(ByRef $FilePath, ByRef $ChangeWhat, $Value)
    Local $nArray
    Local $NewFile = @ScriptDir & "/TempTest.txt"
    _FileReadToArray($FilePath, $nArray)
    _FileCreate($NewFile)
    For $i = 1 To UBound($nArray) - 1
        If StringInStr($nArray[$i], $ChangeWhat) Then
            $Split = StringSplit($nArray[$i], '=')
            $nArray[$i] = StringReplace($nArray[$i], $Split[2], " " & $Value)
        EndIf
        FileWriteLine($NewFile, $nArray[$i])
    Next
    FileDelete($FilePath)
    FileMove($NewFile, $FilePath)
EndFunc

Edit: I'm assuming your only showing partial code (I hope so anyway)...

Don't forget you need #inlude <File.au3>

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

Yes that works, thanks Smoke N

And yeah, I had just dropped in the snippet of code before, the includes and stuff are @ the top of my script.

Cheers for your help. Now it's going for one control I need to get it to update maybe half the lines in the config file, each from a different control in the GUI.

Would it be better to put a GUICtrlSetOnEvent on each of the controls or would all the opening/copying/rewritng/deleting/moving of the file on every event change be a problem and maybe I should run the function once and update all options from all controls at the same time triggerd by a single event?

My AutoIt Scripts.- AutoHost and Password Enabler for Delta Force 2 Demo.| Caffine for Winamp 2.9x and WRS 2.0 | mp3 directory cleaner | CRAP DJ | A:B:J Radio Automation Software | FFMPEG batch conversion automator

Link to comment
Share on other sites

  • Moderators

I'm not a fan of GUICtrlSetOnEvent, I'd rather just use: $msg = GUIGetMsg() and a case statement.

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