Jump to content

Recommended Posts

Junior Programmer here... 

Not much experience with opening, changing and closing files.

I am trying to replace strings in a Text file except StringReplace does not actually replace the text.

Here is a sample of my code...

#include <File.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

Local $iStrReturn = 0
Local $hFile
Local $sText
Local $sNewText

; Location of File to be read
$sFileName = "C:\Temp\MyPlayer.exe.config"

; The default is FALSE. We want to change this to TRUE
$bLoopChecked = True

CheckBox()


Func Checkbox()
    $hFile = FileOpen($sFileName,$FO_READ) ; Open file in read mode to get text
    If $hFile = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when Opening the file.")
        Exit
    EndIf
    FileSetPos($hFile, 0, 0)    ; No idea if I need to do this, grasping at straws
    $sText = FileRead($hFile)   ; Read the file into $sText
    If $sText = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading/writing the file.")
        Exit
    Else
        FileClose($hFile)       ; Finished reading the file into $sText, so close the file.
        FileFlush($hFile)       ; Manual says to use FileFlush between File Close and Open so here it is
    EndIf

    MsgBox(0,"Before Replacement",$sText)   ; Displays the text read from the file to make sure something is there.

    ; Loop Check
    If $bLoopChecked = True Then
        ; Find the string return > 0 for success
        $iStrReturn = StringInStr('"<add key="LoopCheckbox" value=""False" />"', "False")
        ;MsgBox(0,"", "LoopCheckBox is " & $iStrReturn)
        If $iStrReturn > 0 Then     ; If StringInStr returned > 0 the it found the string!
            ; The Meat of the code. This is where we have to replace "False" with "True"
            $sNewText = StringReplace($sText, '"<add key="LoopCheckbox" value="False" />"', '"<add key="LoopCheckbox" value="True" />"')
            MsgBox(0,"After Replacement",$sNewText) ; Display the text to see if it worked.
            $hFile = FileOpen($sFileName,$FO_OVERWRITE) ; Reopen the file to write to it, overwriting everything.
            FileWrite($hFile,$sNewText)                 ; Write the text to the file
            FileClose($hFile)                           ; Close the file
        EndIf
    EndIf
EndFunc

This is the file I am reading...

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
  </startup>
  <appSettings>
    <add key="LoopCheckbox" value="false"/>
  </appSettings>
</configuration>

I tried opening the file with $FO_UTF8 and $FO_UTF8_NOBOM but got errors opening the file.

The MsgBox "After Replacement" shows the value is still false.

Share this post


Link to post
Share on other sites

@Jibberish one of our Regex experts will doubtless wander along and provide something more elegant, but for a simple method this works well for me. For this example my file is sitting on the desktop, change your file path accordingly. Let me know if you have any questions:

#include <File.au3>

Local $aFile, $sText, $sFile = @DesktopDir & "\test.xml"
    _FileReadToArray($sFile, $aFile)

    For $a = 1 To $aFile[0]
        If StringInStr($aFile[$a], '"LoopCheckbox" value="false"') Then
            $sText = StringReplace(FileReadLine($sFile, $a), '="false"', '="true"')
            _FileWriteToLine($sFile, $a, $sText, 1)
        EndIf
    Next

 

Share this post


Link to post
Share on other sites

maybe:

Dedug version:

Global $sFile = "C:\Temp\MyPlayer.exe.config"
Global $bLoopChecked = True

Local $sValueTrue = '<add key="LoopCheckbox" value="true"/>'
Local $sValueFalse = '<add key="LoopCheckbox" value="false"/>'

Local $xFileEncoding = FileGetEncoding($sFile), $xContent = FileRead($sFile)
If @error Or Not FileExists($sFile) Then Exit MsgBox(48, "ERROR", "Read file error: " & $sFile)


Local $sCheckTrue = StringInStr($xContent, $sValueTrue)
Local $sCheckFalse = StringInStr($xContent, $sValueFalse)
ConsoleWrite("- Before:" & @CRLF & $xContent & @CRLF)
If $sCheckFalse And Not $bLoopChecked Then
    Exit MsgBox(48, "Set value to: " & $bLoopChecked, "But cureent LoopCheckbox=false")
ElseIf $sCheckTrue And $bLoopChecked Then
    Exit MsgBox(48, "Set value to: " & $bLoopChecked, "But cureent LoopCheckbox=true")
EndIf

If $sCheckFalse And $bLoopChecked Then
    MsgBox(64, "Will Set value to: " & $bLoopChecked, "Cureent LoopCheckbox=false")
    $xContent = StringReplace($xContent, $sValueFalse, $sValueTrue)
;~  ConsoleWrite("-" & @extended & @CRLF)
Else
    MsgBox(64, "Will Set value to: " & $bLoopChecked, "Cureent LoopCheckbox=true")
    $xContent = StringReplace($xContent, $sValueTrue, $sValueFalse)
;~  ConsoleWrite("-" & @extended & @CRLF)
EndIf
Local $hOpen = FileOpen($sFile, 2 + 8 + $xFileEncoding)
FileWrite($hOpen, $xContent)
FileClose($hOpen)

ConsoleWrite("+ After:" & @CRLF & $xContent & @CRLF)

If (StringInStr($xContent, $sValueTrue) And $bLoopChecked) Or (StringInStr($xContent, $sValueFalse) And Not $bLoopChecked) Then
    Exit MsgBox(64, "Set value to: " & $bLoopChecked, "Set value OK")
Else
    Exit MsgBox(48, "Set value to: " & $bLoopChecked, "Set value ERROR")
EndIf

 

Product version:

Global $sFile = "C:\Temp\MyPlayer.exe.config"
Global $bLoopChecked = True

If _SetLoopChecked($bLoopChecked, $sFile) Then
    MsgBox(64, "Set value to: " & $bLoopChecked, "Set value OK" & @CRLF & "File: " & $sFile)
Else
    MsgBox(48, "Set value to: " & $bLoopChecked, "Set value ERROR" & @CRLF & "File: " & $sFile)
EndIf

Func _SetLoopChecked($iTrue, $sFile = @ProgramFilesDir & "\MyApp\MyPlayer.exe.config")
    Local $xFileEncoding = FileGetEncoding($sFile), $xContent = FileRead($sFile)
    If @error Or Not FileExists($sFile) Then Return SetError(1, 0, 0)
    Local $sValueTrue = '<add key="LoopCheckbox" value="true"/>'
    Local $sValueFalse = '<add key="LoopCheckbox" value="false"/>'
    Local $sCheckTrue = StringInStr($xContent, $sValueTrue)
    Local $sCheckFalse = StringInStr($xContent, $sValueFalse)
    If $sCheckFalse And Not $iTrue Then
        Return SetError(0, 1, 1)
    ElseIf $sCheckTrue And $iTrue Then
        Return SetError(0, 1, 1)
    EndIf
    If $sCheckFalse And $iTrue Then
        $xContent = StringReplace($xContent, $sValueFalse, $sValueTrue)
    Else
        $xContent = StringReplace($xContent, $sValueTrue, $sValueFalse)
    EndIf
    Local $hOpen = FileOpen($sFile, 2 + 8 + $xFileEncoding)
    Local $WOK = FileWrite($hOpen, $xContent)
    FileClose($hOpen)
    If $WOK And ((StringInStr($xContent, $sValueTrue) And $iTrue) Or (StringInStr($xContent, $sValueFalse) And Not $iTrue)) Then Return SetError(0, 0, 1)
    Return SetError(2, 0, 0)
EndFunc   ;==>_SetLoopChecked

 

Edited by Trong
add version

Share this post


Link to post
Share on other sites
6 hours ago, JLogan3o13 said:

@Jibberish one of our Regex experts will doubtless wander along and provide something more elegant, but for a simple method this works well for me. For this example my file is sitting on the desktop, change your file path accordingly. Let me know if you have any questions:

#include <File.au3>

Local $aFile, $sText, $sFile = @DesktopDir & "\test.xml"
    _FileReadToArray($sFile, $aFile)

    For $a = 1 To $aFile[0]
        If StringInStr($aFile[$a], '"LoopCheckbox" value="false"') Then
            $sText = StringReplace(FileReadLine($sFile, $a), '="false"', '="true"')
            _FileWriteToLine($sFile, $a, $sText, 1)
        EndIf
    Next

 

@JLogan3o13

Always the smartest code :) :bye:


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

Wow! The support here is amazing! I'll try all these out and try to understand why these work and mine doesn't. Always learning...!

Cheers,

Jibberish

Share this post


Link to post
Share on other sites
On 7/20/2017 at 8:40 PM, JLogan3o13 said:

@Jibberish one of our Regex experts will doubtless wander along and provide something more elegant, but for a simple method this works well for me. For this example my file is sitting on the desktop, change your file path accordingly. Let me know if you have any questions:

#include <File.au3>

Local $aFile, $sText, $sFile = @DesktopDir & "\test.xml"
    _FileReadToArray($sFile, $aFile)

    For $a = 1 To $aFile[0]
        If StringInStr($aFile[$a], '"LoopCheckbox" value="false"') Then
            $sText = StringReplace(FileReadLine($sFile, $a), '="false"', '="true"')
            _FileWriteToLine($sFile, $a, $sText, 1)
        EndIf
    Next

about if I just wanted to replace value value=" 10.23.25.16" and the value may not necessarily be value=" 10.23.25.16", it could change

there the idea is to replace anything after value=" "

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
  </startup>
  <appSettings>
    <add key="LoopCheckbox" value="10.23.25.16"/>
  </appSettings>
</configuration

 

 

Edited by antonioj84
correction

Share this post


Link to post
Share on other sites
<!-- BEGIN connector for Tender Retail -->
        <CONNECTOR name="TenderRetailConnector"
            javaclass="rci.retail.stores.commext.connector.tenderretail.RCITenderRetailConnector">
            <PROPERTY propname="hostname" propvalue="127.1.1.1" />
            <PROPERTY propname="portNumber" propvalue="3858" proptype="INTEGER" />
            <PROPERTY propname="timeout" propvalue="180000" proptype="INTEGER" />
        </CONNECTOR>
        
    </CONNECTORS>

here is my xml pattern, the value i m after to replace is "127.1.1.1"   thanks for your help
2 hours ago, mikell said:

?

$txt = StringRegExpReplace(FileRead("test.xml"), '(?<=LoopCheckbox" value=")[^"]*', "anything")

Msgbox(0,"test", $txt)

 

 

Edited by antonioj84
error

Share this post


Link to post
Share on other sites
$sFile = @ScriptDir & '\Manager.xml'
FileSetAttrib($sFile & '\Manager.xml', "-R") ; remove flag make file editable





  _FileReadToArray($sFile, $aFile)
 ;_ArrayDisplay($aFile)

    For $a = 1 To $aFile[0]

        If StringInStr($aFile[$a], '"hostname" propvalue="127.1.1.1"') Then

        $sText = StringReplace(FileReadLine($sFile, $a), '="127.1.1.1"','="127.2.2.2"')
        MsgBox(0,'',$sText)
            _FileWriteToLine($sFile, $a, $sText, 1)
        EndIf
    Next

this current code works, all i need is to be able to replace anything after propvalue="" with another value example 127.2.2.2 thankd

Share this post


Link to post
Share on other sites

Because it's an xml-file so why not handle it as an xml-file?:

; load XMLDOM-object
Local $o_XML = ObjCreate("Microsoft.XMLDOM")

; try to load xml from file
If Not $o_XML.load("MyPlayer.exe.config") Then Exit MsgBox(48, "Error", "error when opening file")

; pick node (with XPath-selector) and directly set the new attribute value
$o_XML.SelectSingleNode('//add[@key="LoopCheckbox"]').setAttribute("value", "true")

; write out the xml:
$o_XML.save("Out.config")
ConsoleWrite($o_XML.xml() & @CRLF)

 

Share this post


Link to post
Share on other sites
On 1/22/2018 at 8:26 AM, mikell said:

Why didn't you provide the real xml first ?  :rolleyes:

$new = "127.2.2.2"

$txt = StringRegExpReplace(FileRead("test.xml"), '(?<=hostname" propvalue=")[^"]*', $new)

Msgbox(0,"test", $txt)

 

if i were to read a second  node adding same ip, this does not work below. thanks ahead

$new = "127.2.2.2"


while 1
$txt = StringRegExpReplace(FileRead("test.xml"), '(?<=hostname" propvalue=")[^"]*', $new)

$txt = StringRegExpReplace(FileRead("test.xml"), '(?<=hostname3" propvalue3=")[^"]*', $new)   ; how  do i read next


Msgbox(0,"test", $txt)

if @error = -1 then exitloop

wend

 

Edited by antonioj84
error

Share this post


Link to post
Share on other sites

Hey, you posted just a part of the xml and asked a partial question, so you got a partial answer
As I said before, post the whole xml with all the replacements to do and you will get a complete answer

Share this post


Link to post
Share on other sites
#RequireAdmin
  #include<file.au3>
  #include<array.au3>

  local $Jpos = @ScriptDir & "\Jpos.xml", $aFile,$sText
  FileSetAttrib($Jpos, "-R")


  _FileReadToArray($Jpos, $aFile)
 ;_ArrayDisplay($aFile)

    For $a = 1 To $aFile[0]

        If StringInStr($aFile[$a], '"PortName" "String" value="0.0.0.0"') Then

        $sText = StringReplace(FileReadLine($Jpos, $a), '="0.0.0.0"','="127.2.2.2"')
        MsgBox(0,'',$sText)
            _FileWriteToLine($Jpos, $a, $sText, 1)
        EndIf
    Next

the Problem here is what I am trying to do is to change the whole xlm file at the 2 locations anywhere it encounter PortName value=" anything"

<JposEntry logicalName="CashDrawer">
 <prop name="PortName" type="String" value="0.0.0.0"/>
 </JposEntry>

 <JposEntry logicalName="POSPrinter">
 <prop name="PortName" type="String" value="0.0.0.0"/>
 </JposEntry>

 

Edited by antonioj84
error

Share this post


Link to post
Share on other sites

let me share this piece of snippet, the regex look for the ip pattern in the file and replace the ip address. The 1 is the count,  if left empty it will change ip until the end of the file

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



_ChangeIP("127.1.1.1")

Func _ChangeIP($sNewIPAddress)
    Local $iRet = 0
    Local $sFile = @ScriptDir & '\Manager.xml'
    FileSetAttrib($sFile & '\Manager.xml', "-R") ; remove flag make file editable
    Local $sData = FileRead($sFile)
    Local $hFile = FileOpen($sFile, 2)
    $sData = StringRegExpReplace($sData, '\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b', $sNewIPAddress, 1)  ; 2 count change ip twice
    $iRet = @extended
    FileWrite($hFile, $sData)
    FileClose($hFile)
    Return $iRet
EndFunc   ;==>_ChangeIP

 

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

  • Similar Content

    • By jmp
      I am adding labour charge to total paid amount using : 
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData3 = _IETableWriteToArray ($oTable) Local $sitem1 = $aTableData3[5][1] Local $sitem2 = $aTableData3[5][2] Local $lcharge = "10" ;add manualy using inputbox, becuase not generating online Local $atotPric = "Payable Total Price " Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.Innertext = $atotPric Then $iatotPric = $oTd.NextElementSibling.innertext MsgBox (0, "2", $iatotPric) EndIf Next $irCtotal = StringFormat("%.2f", $sitem1 + $sitem2 + $lcharge) $crTotp = StringReplace(_IEBodyReadHTML($oIE), $iatotPric, $irCtotal) _IEBodyWriteHTML ($oIE, $crTotp) But, It was also changing Total price, I want to change only Payable Total Price.

    • By dascondor
      So basically need to pull text from a .txt file or .sql and use that text to replace something within a different file.
       
      First File:
      jumps
       
      Or:
      Text1 = jumps
      Text2 = fence
      Second File:
      My Dog XXXXX over the YYYYY.
       
       
      After Script:
      My Dog jumps over the fence
       
      I have found a good start, I'm basic programmer at best, I have some experience but this gets out of my wheel house.
       
      Any help is appreciated.
       
       
    • By milos83
      Default keyword for optional parameter is interpreted wrongly.
      ConsoleWrite(StringReplace("aa", "a", "b", Default, 1) & @CRLF) StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] ) The code above will output ab even thou the default value for the occurrence is 0 (replace all).
      Of course using zero instead of Default will work fine.
    • By smartkey
      Hi All,
           I have written a UDF for one of my requirement which replaces a single character in string with a sub string/another character. 
           I am using this for my requirement by calling below function as StrReplace("C:\Software\Autoit\Substr","\","\\") and gives result as C:\\Software\\Autoit\\Substr
           Please let me know if this can be improvised or any mistakes to correct.   
      ;===============================================================================
      ;
      ; Function Name:    StrReplace($INPUT_STRING)
      ; Description:      This function is to replace a character with another in a string.
      ; Parameter(s):     $INPUT_STRING     - Original String Value
      ;                    $STR_2_FIND     - Single Character to find the $INPUT_STRING
      ;                    $STR_2_REPLACE     - Substring/Multiple Characters to replace in place of     $STR_2_FIND value
      ; Requirement(s):   Replacing one single Character in a string with multiple Characters
      ; Return Value(s):  success - Output string after replacing a character with required character
      ;                    failure - 0
      ; Author(s):        smartkey
      ;
      ;===============================================================================
      Func StrReplace($INPUT_STRING, $STR_2_FIND, $STR_2_REPLACE)
          Local $OUTPUT_STRING = ""
          If StringLen($INPUT_STRING) > 0 Then
              If StringMid($INPUT_STRING,1,1) = $STR_2_FIND Then
                  $OUTPUT_STRING = $OUTPUT_STRING & $STR_2_REPLACE
              Else
                  $OUTPUT_STRING = StringMid($INPUT_STRING,1,1)
              EndIf
              For $i=2 to StringLen($INPUT_STRING)
                  If StringMid($INPUT_STRING,$i,1) = $STR_2_FIND Then
                      $OUTPUT_STRING= $OUTPUT_STRING & $STR_2_REPLACE
                  Else
                      $OUTPUT_STRING= $OUTPUT_STRING & StringMid($INPUT_STRING,$i,1)
                  EndIf
              Next
              Return $OUTPUT_STRING
          Else
              Return 0
          EndIf
      EndFunc
×
×
  • Create New...