Jump to content

Function to Read through and Delete Lines in an Output File


Go to solution Solved by SOLVE-SMART,

Recommended Posts

  • Solution

Okay, to hopefully complete this @SoftWearInGinEar, here is one approach/solution to handle your requirements:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_UseUpx=n
#Au3Stripper_Parameters=/sf /sv /mo /rm /rsln

#include-once
#include <Array.au3>

_Actions()

Func _Actions()
    Local Const $sSourceFile      = @ScriptDir & '\OutputData.dat'
    Local Const $sDestinationFile = @ScriptDir & '\OutputData2.dat'
    Local Const $aListOfLines     = FileReadToArray($sSourceFile)

    Local Const $aNewList        = _RemoveInvalidLines($aListOfLines)
    Local Const $sNewFileContent = _ArrayToString($aNewList, @CRLF)

    _WriteFile($sDestinationFile, $sNewFileContent)
EndFunc

Func _RemoveInvalidLines($aListOfLines)
    Local Const $iLastLine  = UBound($aListOfLines) - 1
    Local Const $iFirstLine = 2

    For $i = $iLastLine To $iFirstLine Step -1
        If Not _IsLineInvalid($aListOfLines[$i]) Then
            ContinueLoop
        EndIf

        _ArrayDelete($aListOfLines, $i) ; remove the invalid line from the list (array)
    Next

    Return $aListOfLines
EndFunc

Func _IsLineInvalid($sLine)
    Local Const $sRegExPattern = '(?m)^0\t|^\t|(?<=\t)0(?=\t)|\t0$|\t$|\t\t'

    Return StringRegExp($sLine, $sRegExPattern) ; if pattern matches, then yes - invalid line
EndFunc

Func _WriteFile($sFile, $sText)
    Local Const $iUtf8WithoutBomAndOverwriteCreationMode = 256 + 2 + 8

    Local $hFile = FileOpen($sFile, $iUtf8WithoutBomAndOverwriteCreationMode)
    FileWrite($hFile, $sText)
    FileClose($hFile)
EndFunc

Hint:
I already prepared this yesterday because it was pretty fun to interact with @pixelsearch on the RegEx topic.
I wanted to fulfill your request and hope that's it is what you're looking for 🤞 .

Best regards
Sven

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

11 minutes ago, SOLVE-SMART said:

Okay, to hopefully complete this @SoftWearInGinEar, here is one approach/solution to handle your requirements:

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y
#AutoIt3Wrapper_Run_Au3Stripper=y
#AutoIt3Wrapper_UseUpx=n
#Au3Stripper_Parameters=/sf /sv /mo /rm /rsln

#include-once
#include <Array.au3>

_Actions()

Func _Actions()
    Local Const $sSourceFile      = @ScriptDir & '\OutputData.dat'
    Local Const $sDestinationFile = @ScriptDir & '\OutputData2.dat'
    Local Const $aListOfLines     = FileReadToArray($sSourceFile)

    Local Const $aNewList        = _RemoveInvalidLines($aListOfLines)
    Local Const $sNewFileContent = _ArrayToString($aNewList, @CRLF)

    _WriteFile($sDestinationFile, $sNewFileContent)
EndFunc

Func _RemoveInvalidLines($aListOfLines)
    Local Const $iLastLine  = UBound($aListOfLines) - 1
    Local Const $iFirstLine = 2

    For $i = $iLastLine To $iFirstLine Step -1
        If Not _IsLineInvalid($aListOfLines[$i]) Then
            ContinueLoop
        EndIf

        _ArrayDelete($aListOfLines, $i) ; remove the invalid line from the list (array)
    Next

    Return $aListOfLines
EndFunc

Func _IsLineInvalid($sLine)
    Local Const $sRegExPattern = '(?m)^0\t|^\t|(?<=\t)0(?=\t)|\t0$|\t$|\t\t'

    Return StringRegExp($sLine, $sRegExPattern) ; if pattern matches, then yes - invalid line
EndFunc

Func _WriteFile($sFile, $sText)
    Local Const $iUtf8WithoutBomAndOverwriteCreationMode = 256 + 2 + 8

    Local $hFile = FileOpen($sFile, $iUtf8WithoutBomAndOverwriteCreationMode)
    FileWrite($hFile, $sText)
    FileClose($hFile)
EndFunc

Hint:
I already prepared this yesterday because it was pretty fun to interact with @pixelsearch on the RegEx topic.
I wanted to fulfill your request and hope that's it is what you're looking for 🤞 .

Best regards
Sven

Awesome! Thank you so much!

Link to comment
Share on other sites

here is my approach with out RegEx. Also from yesterday :)

#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <FileConstants.au3>


_FixIt(@ScriptDir & "\OutputData.dat")
;----------------------------------------------------------------------------------------
Func _FixIt($FilePath)
    ;Read the current script file into an array using the filepath.
    Local $aArray = FileReadToArray($FilePath)
    Local $iLineCount = @extended
    Local $NewTxt, $sTmp, $aSpl

    If @error Then
        ;If An error occurred reading the current script file Then Exit
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. @error: " & @error)
        Exit
    Else
        $NewTxt = ""
        ;
        For $i = 0 To $iLineCount - 1

            If $i < 2 Then  ;ignore 2 first line
                $NewTxt &= $aArray[$i] & @CRLF
                ContinueLoop
            EndIf

            $aSpl = StringSplit($aArray[$i], @TAB, 1)  ;Splits line into tab substrings

            $sTmp = ""
            For $x = 2 To $aSpl[0]  ;skip first tab
                $sTmp = StringStripWS($aSpl[$x], $STR_STRIPALL)

                If $sTmp = 0 Or $sTmp = "" Then  ;If tab = 0 Or "" Then go next line
                    ConsoleWrite("--> Empty line: " & $i & @CRLF)
                    ContinueLoop 2
                EndIf
            Next

            $NewTxt &= $aArray[$i] & @CRLF  ;add line to $NewTxt

        Next

        ;rename the old
        FileMove($FilePath, $FilePath & ".old", $FC_OVERWRITE + $FC_CREATEPATH)

        ;make new
        _SaveFile($FilePath, $NewTxt)

    EndIf

EndFunc   ;==>_FixIt
;----------------------------------------------------------------------------------------
Func _SaveFile($sFile, $sData, $iFormat = 266) ; 256+8+2
    Local $hFileOpen = FileOpen($sFile, $iFormat)
    If $hFileOpen = -1 Then
        Return SetError(1, 0, "")
    EndIf
    Local $msg = FileWrite($hFileOpen, $sData)
    FileClose($hFileOpen)
    Return $msg
EndFunc   ;==>_SaveFile
;----------------------------------------------------------------------------------------

 

Edited by ioa747

I know that I know nothing

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