Jump to content

Script leads to text file corruption


 Share

Recommended Posts

Hi Iam very new to scripting, Can some one help me to know why script causes the text file corruption.

The objective of the script is to change a line in the script and process the edited text file with an external application. I am not sure what mistake iam doing in the script, my script is below


#include <GUIConstantsEx.au3>
#include<GUIConstants.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#Include <WinAPIEx.au3>
#include <GuiMenu.au3>
#include <MsgBoxConstants.au3>
#include<ComboConstants.au3>
#include <WinAPIFiles.au3>


VersionReplace()
Func VersionReplace()
    $targetDir ="G:\DataView Databases\P023-C1\23.10.2019\Data\Converted Files"

        Local $FileList = _FileListToArray($targetDir)
            _ArrayDisplay($FileList, "$FileList")

        For $i = 0 to UBound($FileList)-1

            $find = "Software Version['975-d529cde45']"
            $replace = "Software Version['2.6.4.0']"
            $filepath = $targetDir ; currently static path
            $filename = $targetDir & "\" & $FileList[$i]
                MsgBox(0, "", $filename,1)
            Dim $avLines
                If _FileReadToArray($filename, $avLines) And $avLines[0] > 0 Then
                    $Count = 0
            For $n = 1 To $avLines[0]
                If StringLeft($avLines[$n], StringLen($find)) = $find Then
                    $Count += 1
                    $avLines[$n] = $replace & StringTrimLeft($avLines[$n], StringLen($find))
                EndIf
            Next
        If Not $Count Then
            ; MsgBox(16, "Error", "No instances of '" & $find & "' found!", 10)
            ; Exit
        EndIf

        If _FileWriteFromArray($filename, $avLines, 1) Then; Start from index=1 to not write count in [0]
             MsgBox(64, "Finished", "Successfully replaced " & $Count & " instances of '" & $find & "' with '" & $replace & "'",1)
        Else
             MsgBox(16, "Error", "Error occured writing array to file: " & $filename, 1)
        EndIf
        Else
         MsgBox(16, "Error", "Error reading file into array.")
        EndIf
        
        If 
        
        
    Next
EndFunc


 

Link to comment
Share on other sites

It is not the most effective way if you want to edit thousands of files, but have a look at the help for the function : _ReplaceStringInFile

EDIT : Here an example (for the test, create a subdir "\Dirtest" and unzip the attached file Dirtest.zip there) :

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

_VersionReplace(@ScriptDir & "\Dirtest")

; ---- Function ----
Func _VersionReplace($SearchDir)
    ConsoleWrite("> @@DEBUG SearchDir = " & $SearchDir & @CRLF) ; *** just for display

    Local $sSearch  = "Software Version['975-d529cde45']"
    Local $sReplace = "Software Version['2.6.4.0']"
    Local $iReplaceResult

    ; list of all textfiles (*.txt) in $SearchDir :
    Local $aFileList = _FileListToArray($SearchDir, "*.txt", $FLTA_FILES, True)
    If Not @error Then
        _ArrayDisplay($aFileList, "Array FileList") ; *** just for display
        For $i = 1 To $aFileList[0] Step 1
            ConsoleWrite("> >>>>> ------------------------------------- <<<<<< " & @CRLF) ; *** just for display
            ConsoleWrite("< >>>>> @@DEBUG Check File = " & $aFileList[$i] & @CRLF) ; *** just for display

            $iReplaceResult = _ReplaceStringInFile($aFileList[$i], $sSearch, $sReplace, $STR_NOCASESENSE)
            If Not @error Then
                ConsoleWrite("+ @@DEBUG : " & $iReplaceResult & " String(s) replaced in File = " & $aFileList[$i] & @CRLF) ; *** just for display
            EndIf
        Next
    Else
        ConsoleWrite("! @@DEBUG Error : Array FileList = " & @error & @CRLF)
    EndIf
EndFunc

 

Dirtest.zip

Edited by Musashi

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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