Jump to content

Reading a line of a specific file then changing the information


Recommended Posts

I have a file that I am trying to read a specific line in that file, change some text in it, and then save it. The file is a text file with the .php extension. I have attached the file. I am trying to have the the program read line 21 and then have it look for the words "green" and "ok" and then change them to "red" and "down".

I have been able to read the file just fine using the FileOpen and FileReadLine, but I have not been able to edit any of the lines that it does read. Any help would be greatly appreciated

header.inc.php

Link to comment
Share on other sites

Use _FileWriteToLine().

To use that, would the code be something like this?

#include <file.au3>

$file = FileOpen("test.txt", 1)
$quote = Chr(034)
$line21DOWN = "         <i>MAX</i> - <b><font color="& $quote & "red" & $quote & ">DOWN</font></b><br>"
_FileWriteToLine($file, 21, $line21DOWN, 1)
Link to comment
Share on other sites

  • Moderators

I have a file that I am trying to read a specific line in that file, change some text in it, and then save it. The file is a text file with the .php extension. I have attached the file. I am trying to have the the program read line 21 and then have it look for the words "green" and "ok" and then change them to "red" and "down".

I have been able to read the file just fine using the FileOpen and FileReadLine, but I have not been able to edit any of the lines that it does read. Any help would be greatly appreciated

Local $s_file = "header.inc.php"
Local $s_find = '"green",>ok<'
Local $s_change = '"red",>down<'
Local $n_search_line = 21

_myCustomFunc($s_file, $n_search_line, $s_find, $s_change)
If @error Then
    MsgBox(16, "Error", "Check the error code in the function: " & @error)
Else
    MsgBox(64, "Success", "If any changes were to be made, they were made and written.")
EndIf

Func _myCustomFunc($s_file, $n_line_num, $s_if_is, $s_change_to, $v_delim = ",")
    Local $s_string = FileRead($s_file)
    Local $a_split = StringSplit(StringStripCR($s_string), @LF)
    If $a_split[0] < $n_line_num Then Return SetError(1, 0, 0)
    
    Local $a_if_is = StringSplit($s_if_is, $v_delim, 1)
    Local $a_change_to = StringSplit($s_change_to, $v_delim, 1)
    If $a_change_to[0] <> $a_if_is[0] Then Return SetError(2, 0, 0)
    
    Local $s_hold_line, $i, $n_extended
    
    For $i = 0 To $a_if_is[0]
        If StringInStr($a_split[$n_line_num], $a_if_is[$i]) Then
            $a_split[$n_line_num] = StringReplace($a_split[$n_line_num], $a_if_is[$i], $a_change_to[$i])
            $n_extended += @extended
        EndIf
    Next
    
    If $n_extended = 0 Then Return 1 ;No changes need to be made

    For $i = 1 To $a_split[0]
        $s_hold_line &= $a_split[$i] & @CRLF
    Next
    
    FileClose(FileOpen($s_file, 2))
    Return SetError(0, $n_extended, FileWrite($s_file, StringTrimRight($s_hold_line, 2)))
EndFunc

Edit:

Added extended info so you can see how many changes were made.

By adding the extended info, we can eliminate also the need to re-write the file if the desired information to change was not found.

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

To use that, would the code be something like this?

#include <file.au3>

$file = FileOpen("test.txt", 1)
$quote = Chr(034)
$line21DOWN = "         <i>MAX</i> - <b><font color="& $quote & "red" & $quote & ">DOWN</font></b><br>"
_FileWriteToLine($file, 21, $line21DOWN, 1)
If you want to overwrite line 21, your code should work. ;)

When the words fail... music speaks.

Link to comment
Share on other sites

  • Moderators

After looking at all this again... looks like we could have just done:

Local $s_file = "header.inc.php"
Local $s_string = FileRead($s_file)
$s_string = StringRegExpReplace($s_string, "(<i>MAX</i>.+?color="")(.+?)("">)(.+?)(<)", "\1red\3DOWN\5")
FileClose(FileOpen($s_file, 2))
FileWrite($s_file, $s_string)

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

Sorry for the delay in responding, but I ended up utilizing the _FileWriteToLine.

I have just finished with the program and I am very happy how it turned out in the end. Thanks for everyones help! ;)

Here is the source of the final product:

#include <File.au3>
#include <GUIConstants.au3>

$quote = chr(034)

Opt("GUIOnEventMode", 1)

$guiMAIN = GUICreate("Status Updater", 209, 276, 399, 268, BitOR($WS_SYSMENU,$WS_CAPTION,$WS_POPUP,$WS_POPUPWINDOW,$WS_BORDER,$WS_CLIPSIBLINGS))
$lblPROGRAM = GUICtrlCreateLabel("Program", 8, 24, 100, 17, $SS_CENTER)
$lblUP = GUICtrlCreateLabel("UP", 128, 24, 27, 17, $SS_CENTER)
$lblDOWN = GUICtrlCreateLabel("DOWN", 160, 24, 39, 17, $SS_CENTER)
$lblMAX = GUICtrlCreateLabel("MAX", 48, 48, 27, 17)
$lblCLIENTELE = GUICtrlCreateLabel("Clientele", 40, 80, 44, 17)
$lblGREATPLAINS = GUICtrlCreateLabel("Great Plains", 32, 112, 61, 17)
$lblEMAIL = GUICtrlCreateLabel("Email", 48, 144, 29, 17)
$lblINTERNET = GUICtrlCreateLabel("Internet", 40, 176, 40, 17)
$lblPROGRAM = GUICtrlCreateLabel("PROGRAM", 32, 24, 63, 17, $SS_CENTER)
$lblCOPYWRITE = GUICtrlCreateLabel("SAFLOK (c) 2008", 128, 256, 76, 13)
GUICtrlSetFont(-1, 7, 400, 0, "Dotum")

$MAX_UP = GUICtrlCreateCheckbox("", 136, 48, 17, 17)
GUICtrlSetOnEvent($MAX_UP, "MAX_UP")
$MAX_DOWN = GUICtrlCreateCheckbox("", 176, 48, 17, 17)
GUICtrlSetOnEvent($MAX_DOWN, "MAX_DOWN")
$CLIENTELE_UP = GUICtrlCreateCheckbox("", 136, 80, 17, 17)
GUICtrlSetOnEvent($CLIENTELE_UP, "CLIENTELE_UP")
$CLIENTELE_DOWN = GUICtrlCreateCheckbox("", 176, 80, 17, 17)
GUICtrlSetOnEvent($CLIENTELE_DOWN, "CLIENTELE_DOWN")
$GREATPLAINS_UP = GUICtrlCreateCheckbox("", 136, 112, 17, 17)
GUICtrlSetOnEvent($GREATPLAINS_UP, "GREATPLAINS_UP")
$GREATPLAINS_DOWN = GUICtrlCreateCheckbox("", 176, 112, 17, 17)
GUICtrlSetOnEvent($GREATPLAINS_DOWN, "GREATPLAINS_DOWN")
$EMAIL_UP = GUICtrlCreateCheckbox("", 136, 144, 17, 17)
GUICtrlSetOnEvent($EMAIL_UP, "EMAIL_UP")
$EMAIL_DOWN = GUICtrlCreateCheckbox("", 176, 144, 17, 17)
GUICtrlSetOnEvent($EMAIL_DOWN, "EMAIL_DOWN")
$INTERNET_UP = GUICtrlCreateCheckbox("", 136, 176, 17, 17)
GUICtrlSetOnEvent($INTERNET_UP, "INTERNET_UP")
$INTERNET_DOWN = GUICtrlCreateCheckbox("", 176, 176, 17, 17)
GUICtrlSetOnEvent($INTERNET_DOWN, "INTERNET_DOWN")

$UPDATE = GUICtrlCreateButton("Update", 8, 216, 81, 33, 0)
GUICtrlSetOnEvent($UPDATE, "UPDATE")
$CLOSE = GUICtrlCreateButton("Exit", 120, 216, 81, 33, 0)
GUICtrlSetOnEvent($CLOSE, "CLOSE")

GUISetState(@SW_SHOW)


While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit



Func MAX_UP()
    If GUICtrlRead($MAX_UP) = 1 Then
        GUICtrlSetState($MAX_DOWN, $GUI_UNCHECKED)
    EndIf
EndFunc
Func MAX_DOWN()
    If GUICtrlRead($MAX_DOWN) = 1 Then
        GUICtrlSetState($MAX_UP, $GUI_UNCHECKED)
    EndIf
EndFunc
Func CLIENTELE_UP()
    If GUICtrlRead($CLIENTELE_UP) = 1 Then
        GUICtrlSetState($CLIENTELE_DOWN, $GUI_UNCHECKED)
    EndIf
EndFunc
Func CLIENTELE_DOWN()
    If GUICtrlRead($CLIENTELE_DOWN) = 1 Then
        GUICtrlSetState($CLIENTELE_UP, $GUI_UNCHECKED)
    EndIf
EndFunc
Func GREATPLAINS_UP()
    If GUICtrlRead($GREATPLAINS_UP) = 1 Then
        GUICtrlSetState($GREATPLAINS_DOWN, $GUI_UNCHECKED)
    EndIf
EndFunc
Func GREATPLAINS_DOWN()
    If GUICtrlRead($GREATPLAINS_DOWN) = 1 Then
        GUICtrlSetState($GREATPLAINS_UP, $GUI_UNCHECKED)
    EndIf
EndFunc
Func EMAIL_UP()
    If GUICtrlRead($EMAIL_UP) = 1 Then
        GUICtrlSetState($EMAIL_DOWN, $GUI_UNCHECKED)
    EndIf
EndFunc
Func EMAIL_DOWN()
    If GUICtrlRead($EMAIL_DOWN) = 1 Then
        GUICtrlSetState($EMAIL_UP, $GUI_UNCHECKED)
    EndIf
EndFunc
Func INTERNET_UP()
    If GUICtrlRead($INTERNET_UP) = 1 Then
        GUICtrlSetState($INTERNET_DOWN, $GUI_UNCHECKED)
    EndIf
EndFunc
Func INTERNET_DOWN()
    If GUICtrlRead($INTERNET_DOWN) = 1 Then
        GUICtrlSetState($INTERNET_UP, $GUI_UNCHECKED)
    EndIf
EndFunc

Func UPDATE()
    If GUICtrlRead($MAX_UP) = 1 Then
        _FileWriteToLine("header.inc.php", 21, "            <i>MAX</i> - <b><font color=" & $quote & "green" & $quote & ">OK</font></b><br>", 1)
    EndIf
    If GUICtrlRead($MAX_DOWN) = 1 Then
        _FileWriteToLine("header.inc.php", 21, "            <i>MAX</i> - <b><font color=" & $quote & "red" & $quote & ">DOWN</font></b><br>", 1)
    EndIf
    If GUICtrlRead($CLIENTELE_UP) = 1 Then
        _FileWriteToLine("header.inc.php", 22, "            <i>Clientele</i> - <b><font color=" & $quote & "green" & $quote & ">OK</font></b><br>", 1)
    EndIf
    If GUICtrlRead($CLIENTELE_DOWN) = 1 Then
        _FileWriteToLine("header.inc.php", 22, "            <i>Clientele</i> - <b><font color=" & $quote & "red" & $quote & ">DOWN</font></b><br>", 1)
    EndIf
    If GUICtrlRead($GREATPLAINS_UP) = 1 Then
        _FileWriteToLine("header.inc.php", 23, "            <i>Great Plains</i> - <b><font color=" & $quote & "green" & $quote & ">OK</font></b><br>", 1)
    EndIf
    If GUICtrlRead($GREATPLAINS_DOWN) = 1 Then
        _FileWriteToLine("header.inc.php", 23, "            <i>Great Plains</i> - <b><font color=" & $quote & "red" & $quote & ">DOWN</font></b><br>", 1)
    EndIf
    If GUICtrlRead($EMAIL_UP) = 1 Then
        _FileWriteToLine("header.inc.php", 24, "            <i>Email</i> - <b><font color=" & $quote & "green" & $quote & ">OK</font></b><br>", 1)
    EndIf
    If GUICtrlRead($EMAIL_DOWN) = 1 Then
        _FileWriteToLine("header.inc.php", 24, "            <i>Email</i> - <b><font color=" & $quote & "red" & $quote & ">DOWN</font></b><br>", 1)
    EndIf
    If GUICtrlRead($INTERNET_UP) = 1 Then
        _FileWriteToLine("header.inc.php", 25, "            <i>Internet</i> - <b><font color=" & $quote & "green" & $quote & ">OK</font></b><br>", 1)
    EndIf
    If GUICtrlRead($INTERNET_DOWN) = 1 Then
        _FileWriteToLine("header.inc.php", 25, "            <i>Internet</i> - <b><font color=" & $quote & "red" & $quote & ">DOWN</font></b><br>", 1)
    EndIf
    MsgBox(0, "Complete", "Statuses have been successfully updated")
    Exit
EndFunc

Func CLOSE()
    Exit
EndFunc

EndSwitch
WEnd

You can test it out by putting the header.inc.php file in the same directory as the script/compiled exe file.

Thanks once again for all the help!

header.inc.php

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