Jump to content
Docfxit

How to remove a group of text lines in many files

Recommended Posts

Posted (edited)

I have no idea how to go about this.  I've been looking for examples for hours and can't find anything.  I know this is totally wrong but I need someplace to start.

I have many AutoIt scripts with a common set of lines of code that I need to remove.  The first line of code starts with "If _OSVersion" The last line of code starts with "EndIf"

This is an example:

Opt("WinWaitDelay",100)
Opt("WinTitleMatchMode",4)
Opt("WinDetectHiddenText",1)
Opt("MouseCoordMode",0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
; Set so that tray displays current line number

; Unzip the following file so it can be installed
$Var1 = "C:\Dnload\9xAddons\actic25.zip"
If _OSVersion() = "Win7" Or "Win7X64" Then
    $ProgramFiles = "C:\Programs\"
Else
    $ProgramFiles = "C:\Program Files\"
EndIf

While 1 <> 1

RunWait(@Comspec & ' /c C:\Dnload\9xAddons\UnZip.bat "' & $Var1 & '"' )

If it finds the first line, I need it to remove all lines until it finds the first "Endif"  Then I need it to add this line:

#include "OSVersion.au3"

Then it's done with the file.

This is the code I tried to take a stab at it with.  I know it's no where close to what I need.  I just don't know how to do it.

Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
TrayTip("clears any tray tip", "", 0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
Local Const $sFileName = "ACTIC25.AU3"
Local Const $sFromFolder = "C:\Dnload\9xAddons\"
Local Const $sFixedFolder = "C:\Dnload\9xAddons\Fixed\"
Local Const $iStart = "If _OSVersion"
Local Const $iTo = "EndIf"

If FileExists($sFixedFolder & $sFileName) Then
    FileDelete($sFixedFolder & $sFileName)
EndIf
;FileWrite($sFixedFolder & $sFileName, StringRegExpReplace(FileRead($sFromFolder & $sFileName), "(?s)((\V*\v*){" & $iStart & "})(\V+\v*){" & ($iTo) & "}(.*$)", "\1\4"))
FileWrite($sFixedFolder & $sFileName, StringRegExpReplace(FileRead($sFixedFolder & $sFileName), '(?s)If _OSVersion\(\).*EndIf', '#include "OSVersion.au3"') & @CRLF) ; .*? If you have others If...EndIf statements

Could someone please help me accomplish what I need?

Thanks,

Docfxit

Edited by Docfxit

Share this post


Link to post
Share on other sites
Posted (edited)

@Docfxit

ConsoleWrite(StringRegExpReplace('File Content', '(?s)If _OSVersion\(\).*EndIf', '#include "OSVersion.au3"') & @CRLF) ; .*? If you have others If...EndIf statements

:)

Edited by FrancescoDiMuro

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
Posted (edited)
2 hours ago, FrancescoDiMuro said:

@Docfxit

FileWrite($sFixedFolder & $sFileName, StringRegExpReplace(FileRead($sFixedFolder & $sFileName), '(?s)If _OSVersion\(\).*EndIf', '#include "OSVersion.au3"') & @CRLF) ; .*? If you have others If...EndIf statements

:)

The script is:

1. Writing out the file ACTIC25.AU3 with only 2 spaces

2. It's not writing out '#include "OSVersion.au3"'

3. It's not writing out the remainder of the lines in the file.

Edited by Docfxit

Share this post


Link to post
Share on other sites

Since I don't understand how to fix the error in the last script, I decided to try to accomplish the same thing a little old school.

I got into trouble with this error:

RemoveLinesComError.jpg.910dd504c0b3f7200bdacaa4e5fe74ad.jpg

This is my code:

#include <File.au3>
#include <Array.au3>
Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
TrayTip("clears any tray tip", "", 0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
Local Const $sFileName = "ACTIC25.AU3"
Local Const $sFromFolder = "C:\Dnload\9xAddons\"
Local Const $sFixedFolder = "C:\Dnload\9xAddons\Fixed\"
Local Const $iStart = "If _OSVersion"
Local Const $iTo = "EndIf"
Local $sFinished
$objErr = ObjEvent("AutoIt.Error", "MyErrFunc")


$sFinished = 0
If FileExists($sFixedFolder & $sFileName) Then
    FileDelete($sFixedFolder & $sFileName)
EndIf

Dim $_Array
_FileReadToArray('$sFromFolder & $sFileName', $_Array)
$_Array = _DeleteArrayElement($_Array)
_FileWriteFromArray('$sFixedFolder & $sFileName', $_Array, 1)
Exit

Func _DeleteArrayElement($_Array)
    While $sFinished <> 1
        Local $_Item
        For $_Element In $_Array    ;This is where I'm getting the error  ~*~*~*~*~*~*~*~*~*~*~*~*~*
            If StringInStr($_Element, $iStart) <> 0 Then
                _ArrayDelete($_Array, $_Item)
                For $_Element In $_Array
                    If StringInStr($_Element, $iTo) <> 0 Then
                        _ArrayDelete($_Array, $_Item)
                        $_Array[$_Item] = '#include "OSVersion.au3"'
                        $sFinished = 1
                        ExitLoop
                    EndIf
                Next
            Else
                $_Item += 1
            EndIf
        Next
    WEnd
    Return ($_Array)
EndFunc   ;==>_DeleteArrayElement


Func MyErrFunc()
    $hexnum = Hex($objErr.number, 8)

    MsgBox(0, "", "We intercepted a COM Error!!" & @CRLF & @CRLF & _
            "err.description is: " & $objErr.description & @CRLF & _
            "err.windescription is: " & $objErr.windescription & @CRLF & _
            "err.lastdllerror is: " & $objErr.lastdllerror & @CRLF & _
            "err.scriptline is: " & $objErr.scriptline & @CRLF & _
            "err.number is: " & $hexnum & @CRLF & _
            "err.source is: " & $objErr.source & @CRLF & _
            "err.helpfile is: " & $objErr.helpfile & @CRLF & _
            "err.helpcontext is: " & $objErr.helpcontext _
            )

    Exit
    ;   SetError(1)
EndFunc   ;==>MyErrFunc

I

Share this post


Link to post
Share on other sites
Posted (edited)

That took care of the error.

Even though $sFinished = 0 it doesn't execute the code between the While --> Wend

Evidently it doesn't feel $_Array is an array.  How can I define  a variable size array since I have no idea how many records will be read into the array.

Thank you,

Docfxit

Edited by Docfxit

Share this post


Link to post
Share on other sites

Generally you make it as large as you think it should normally be plus some fudge size and then check with UBOUND() and redim if necessary

 

;Pseudocode
Local $aArray[100]

;
;
If Ubound($aArray) < $iThreshold Then
    Redim $aArray[$iThreshold + 10]
EndIf

 

Share this post


Link to post
Share on other sites

If you are on a hot path then redimming should be kept to a minimum but I wouldn't worry about it till you need to speed it up

Share this post


Link to post
Share on other sites

MMM also don't use Dim for arrays it is depreciated use Local/Global $aArray[n] instead

Redim is OK though

Share this post


Link to post
Share on other sites

Try this

#include <File.au3>
#include <Array.au3>
Opt("WinWaitDelay", 100)
Opt("WinTitleMatchMode", 4)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 0)
TrayTip("clears any tray tip", "", 0)
AutoItSetOption("TrayIconDebug", 1) ;0-off
Global Const $sFileName = "ACTIC25.AU3"
Global Const $sFromFolder = "C:\Dnload\9xAddons\"
Global Const $sFixedFolder = "C:\Dnload\9xAddons\Fixed\"
Global Const $sStart = "If _OSVersion"
Global Const $sTo = "EndIf"

ReplaceLines($sFileName, $sFromFolder, $sFixedFolder)
Exit

Func ReplaceLines($sFileName, $sSrcFolder, $sDestFolder)

    ;$objErr = ObjEvent("AutoIt.Error", "MyErrFunc")

    Local $sFinished = 0, $iErr = 0, $aFile
    Local $sSrcFile = $sSrcFolder & "\" & $sFileName
    Local $sDestFile = $sDestFolder & "\" & $sFileName

    If FileExists($sDestFile) Then
        FileDelete($sDestFile)
    EndIf


    _FileReadToArray($sSrcFile, $aFile, $FRTA_NOCOUNT) ;array is 0-based use UBound() to get size
    $iErr = @error
    If $iErr Or Not IsArray($aFile) Then
        MsgBox(0, "Error Occurred", $iErr)
        Return
    EndIf

    _DeleteArrayElement($aFile)
    $iErr = @error

    If $iErr Or Not IsArray($aFile) Then
        MsgBox(0, "Error Occurred", Hex($iErr) & ":" & $iErr)
        Return
    EndIf

    _FileWriteFromArray($sDestFile, $aFile, 0)

EndFunc   ;==>ReplaceLines


Func _DeleteArrayElement(ByRef $_Array)
    If Not IsArray($_Array) Then Return SetError(0xBAD, 0, 0)

    Local $iArrayCount = UBound($_Array)
    Local $iStartPos = -1, $iEndPos = -1
    Local $iErr = 0xDEAF ;Returned if no match found

    For $i = 0 To $iArrayCount
        If StringInStr($_Array[$i], $sStart) <> 0 Then
            For $j = ($i + 1) To $iArrayCount ;We Start After $sStartPos
                If StringInStr($_Array[$j], $sTo) <> 0 Then
                    $iStartPos = $i
                    $iEndPos = $j
                    ExitLoop
                EndIf

            Next
            If $iStartPos >= 0 And $iEndPos > $iStartPos Then ExitLoop
        EndIf
    Next

    If $iStartPos >= 0 And $iEndPos > $iStartPos Then
        Local $sRange = $iStartPos & "-" & $iEndPos
        _ArrayDelete($_Array, $sRange)
        $iErr = @error
        If $iErr Then Return SetError($iErr, 0, 0)
        _ArrayInsert($_Array,0,'#include "OSVersion.au3"')
    EndIf
    Return SetError($iErr, 0, 0)
    ;Note $_Array is Byref so it doesn't need returned
EndFunc   ;==>_DeleteArrayElement

 

Share this post


Link to post
Share on other sites
Posted (edited)

You are a genius.   It works great.

Thank you so much..........

I can't believe it takes that much to get this done.

It's really nice of you to create that for me.

Thanks,

Docfxit

Edited by Docfxit

Share this post


Link to post
Share on other sites

Notice a Few things I've changed..

I made those Local at the top Global

I moved all the code into functions in order to compartmentalize it to make it easier to follow and to ensure I get the scope right

I made the file paths variables and added an extra "\" to ensure it gets added ("\\" is ok in a path)

By making the file paths variables I only need to type it once and i'm ensured no typos happen 

I added error handling to each place I expect an error to occur

I got rid of the Array count in _FileReadToArray with $FRTA_NOCOUNT

In _DeleteArrayElement I used Byref so I can just pass the array into the function without copying it

I checked again if it was an array and raise error if not

I grabbed my own UBound($_Array) and stored it

I walk the array looking for $sStart and walk again looking for $sEnd

 

 

Share this post


Link to post
Share on other sites

You are welcome, 

I really didn't add much, most of it was copying and pasting your existing code the biggest part is error handling

it is always important to try and catch errors or at least give notice of them so you can find where it went wrong

 

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

×
×
  • Create New...