Jump to content
Jibberish

Replacing String in a text file

Recommended Posts

Jibberish

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
JLogan3o13

@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

 

  • Like 2

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
VIP

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

Regards,
 

Share this post


Link to post
Share on other sites
FrancescoDiMuro
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

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.

Share this post


Link to post
Share on other sites
JLogan3o13

Or you could go with @Trong's Rube Goldberg script :)

 

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Jibberish

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
antonioj84
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
antonioj84
<!-- 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
antonioj84
$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
antonioj84

thanks works

Share this post


Link to post
Share on other sites
AspirinJunkie

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
antonioj84
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
mikell

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
antonioj84
#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
mikell

Hmmm it's different from the previous requirement again ... :)

$new = "127.2.2.2"

$txt = StringRegExpReplace(FileRead("test.xml"), '(?m)^.*PortName.*value="\K[^"]*', $new)

Msgbox(0,"test", $txt)

 

  • Like 1

Share this post


Link to post
Share on other sites
antonioj84
14 hours ago, mikell said:

Hmmm it's different from the previous requirement again ... :)

$new = "127.2.2.2"

$txt = StringRegExpReplace(FileRead("test.xml"), '(?m)^.*PortName.*value="\K[^"]*', $new)

Msgbox(0,"test", $txt)

Thanks

 

Share this post


Link to post
Share on other sites
JLogan3o13

@antonioj84 please stop quoting the previous text every...single...response. Just hit reply; we know what we said, and it pads the thread needlessly.

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
antonioj84

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

    • smartkey
      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
    • Amixg
      By Amixg
      Hi! I have another problem with AutoIT.  You see, I'm still the one who's just starting out with this great programming language. I have a problem with AutoIT, it seems not to recognize the "StringReplace" function even when it is by default. The code was made half in KODA and half of my own. It is a software programmed for the Italian language (I am Italian) and translate the SMS language into Italian correct. Would you help me? Thanks in advance.
       
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $hull = "hello" StringReplace($hull, "hello", "hellx") $Form1 = GUICreate("Linguaggio SMS A Italiano", 507, 498, 192, 124) $Edit1 = GUICtrlCreateEdit("", 24, 40, 465, 177) GUICtrlSetData(-1, "") $Label1 = GUICtrlCreateLabel("Inserisci qui il testo che non riesci a capire:", 24, 8, 205, 17) $Label2 = GUICtrlCreateLabel("Ecco il testo tradotto(non toccare questo campo):", 27, 237, 237, 17) $Edit2 = GUICtrlCreateEdit("", 24, 264, 465, 137) GUICtrlSetData(-1, "") $Button1 = GUICtrlCreateButton("Traduci", 104, 424, 249, 49) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 StringReplace(GUICtrlGetData($Label1), "qlc", "qualcuno") StringReplace(GUICtrlGetData($Label1), "qls", "qualcosa") StringReplace(GUICtrlGetData($Label1), "ke", "che") StringReplace(GUICtrlGetData($Label1), "x", "per") StringReplace(GUICtrlGetData($Label1), "pls", "per favore") StringReplace(GUICtrlGetData($Label1), "tu6", "tu sei") StringReplace(GUICtrlGetData($Label1), "zzz", "mi fai dormire") StringReplace(GUICtrlGetData($Label1), "hagn", "Buonanotte") StringReplace(GUICtrlGetData($Label1), "tvtb", "Ti voglio tanto bene") StringReplace(GUICtrlGetData($Label1), "tat", "Ti amo tanto") StringReplace(GUICtrlGetData($Label1), "lafs", "Amore a prima vista") StringReplace(GUICtrlGetData($Label1), "fli?", "Flirtiamo?") StringReplace(GUICtrlGetData($Label1), "msidt", "Mi sono innamorato di te") StringReplace(GUICtrlGetData($Label1), "ba", "Bacio") StringReplace(GUICtrlGetData($Label1), "midi", "Mi dispiace.") StringReplace(GUICtrlGetData($Label1), "ntm", "Non ti merito") StringReplace(GUICtrlGetData($Label1), "tdp", "Togliti dai piedi.") StringReplace(GUICtrlGetData($Label1), "amò", "Amore") StringReplace(GUICtrlGetData($Label1), "ap", "A presto!") StringReplace(GUICtrlGetData($Label1), "cmq", "comunque") StringReplace(GUICtrlGetData($Label1), "cvd", "Ci vediamo dopo") StringReplace(GUICtrlGetData($Label1), "Tvb", "Ti voglio bene") StringReplace(GUICtrlGetData($Label1), "nn", "non") StringReplace(GUICtrlGetData($Label1), "risp", "rispondimi") StringReplace(GUICtrlGetData($Label1), "cel", "cellulare") StringReplace(GUICtrlGetData($Label1), "dom", "qualcosa") StringReplace(GUICtrlGetData($Label1), "nm", "numero") StringReplace(GUICtrlGetData($Label1), "fv", "favore") StringReplace(GUICtrlGetData($Label1), "-male", "meno male") StringReplace(GUICtrlGetData($Label1), "disc", "discoteca") StringReplace(GUICtrlGetData($Label1), "se#", "settimana") StringReplace(GUICtrlGetData($Label1), "+ o -", "più o meno") StringReplace(GUICtrlGetData($Label1), "ts", "torno subito") StringReplace(GUICtrlGetData($Label1), "tvtbcoa", "Ti voglio tanto bene come amica") EndSwitch WEnd  
    • AnonymousX
      By AnonymousX
      Hello,
      I need to pull data from company intranet website. I created a script that I can give a list of project numbers and it will open up the the related webpage for each project, save the html comments for that project, then move on to the next. 
      However my problem comes in that each time I open up a project it locks it for other users, and if you just exit the page it keeps it locked, you have to actually press the "close" button on the page. So I'm not sure how to deal with this as I end up just locking every project in my name when I run my program as I don't understand the nature of how the website is built. I don't understand how to close with just using my automation code without manually having to press the stupid button. 
      My Function:
      Func Getscript($ProjectAddress) ;getting the page source code and storing it into text file for easy reading it $file = FileOpen(@ScriptDir & "\source.txt", 10) $IE = _IECreate($ProjectAddress & ".html", 0, 0) $source = _IEDocReadHTML($IE) FileWrite($file, $source) $target_source = _StringBetween($source, "<BODY>", "</BODY>") ; only take content between the specified tags _IEQuit($IE) FileClose($file) return $target_source[0] EndFunc ;==>Getscript  
      Source Code I got from website that I think is relevant:
      function closeForm(){ if(topButtonEnabled("closeButton")){ parent.mainFrame.closeForm(); } } <button id="closeButton" class="saveCloseButton" style="LEFT: 77px;" onclick=closeForm()>&nbsp;Close&nbsp;</button>  
      Any Ideas of how to tell website to either activate the closeform function or just give it the command to close?
       
      Thanks
    • num1g8rfan
      By num1g8rfan
      Hi All,
      I have a script whose sole purpose is to walk through about 15,000 - 20,000 files in a single directory structure, making modifications to 5 or 6 lines within each file.  Simple.  I have a small splash screen that is simply counting how many files have been completed out of the overall total.  The script is functioning fine and the splash screams through the initial files, but through time begins to slow to a crawl (It becomes noticeable after about 1,000 to 1,500 files).  
      I have used a combination of methods to write to the file, but none of which appear to make much of any difference:
      Local $FileOpen = FileOpen($FileInList,1) FileWriteLine($FileOpen,@CRLF) FileClose($FileOpen) OR
      FileWriteLine($FileHandle, $NewLineString & @CRLF) OR
      Local $FileHandle = FileOpen($FileInList,2) _FileWriteFromArray($FileHandle,$FileContentArray,1) FileClose($FileHandle) I have checked and the CPU and memory are fine.  My current work-around is to create 5-10 separate directory structures from the original one that contains 20,000 files, run the script and then combine them again in the end.  It works, but isn't very portable.  Any ideas?
      Cleanup_AutoITPost.au3
    • MrSparty1995
      By MrSparty1995
      I have a good handle on how to read a file line by line, and search for a given string.
      Basically using, FileOpen, FileReadLine, and StringInStr.
      I have been trying to figure out why my search keeps coming back with no match.
      For example, if my string line was "Where is Waldy", and I was searching for Waldy that comes back fine.
      But in my file, Let's say it is
      Where is "Waldy"
      So i read that line in and do a search for Waldy, but i comes back with no match since there is quotes around it.
      Is there a way to resolve this?
      Here is my actual example
      The line comes back like this
          <JposEntry logicalName="LineDisp_iSC480">
      I'm searching for LineDisp_iSC480, but its coming back with no match due to the quotes.
      Thanks in advance, James
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.