Jump to content
Docfxit

How to remove a group of text lines in many files

Recommended Posts

Posted (edited)

You spent a lot of time on it.  I would never know how to do all that.

You must do a lot with AutoIt.

Edited by Docfxit

Share this post


Link to post
Share on other sites

I have plenty of experience with programming Autoit is just another tool in the toolbox

Its my goto solution on Windows usually since its so quick to get something that does what you need without a whole lot of fuss

The great thing about Autoit is the help file there are very few programming languages with such comprehensive help files

Share this post


Link to post
Share on other sites
Posted (edited)

You are really good at it.

A little trivia I didn't mention before.  I was trying to get through this one step at a time.

I need to run this on all *.au3 files in the $sFromFolder.  So I added some code to read all *.au3 files into an array and put it into a For --> Next loop.

Which you made it so easy for me to do.  In trying to figure out why it isn't processing all the files, I found my first error.

While processing the second file, the array $aFile has left over records from the first file.  I have marked it in the script where it needs to be cleared out.

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

$array = _FileListToArray($sFromFolder, "*.au3", 1)
For $x = 1 To $array[0]
    ReplaceLines($array[$x], $sFromFolder, $sFixedFolder)
Next
Exit

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

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

    Local $sSrcFile = $sSrcFolder & "\" & $sFileName
    Local $sDestFile = $sDestFolder & "\" & $sFileName

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

; The last Array $aFile needs to be cleared out here
;    When I read the next file there are left over records in the array
    _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
        ; The error is for the next line
        ;   $i = 132 at the time of the error which is the correct number of lines in the second file
        ;   $iArrayCount = 132 at the time of the error
        ;       Does that mean this line is supposed to be For $i = 0 To $iArrayCount-1   ?
        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

$iArrayCount is correct for the second file but I'm afraid when it comes time to write the file out it will include the lines from the first file.

The second problem I ran into is:

RemoveLines3Error.jpg.617685a583b1f3db336260955ec26ca8.jpg

The line number is not correct.  I marked the spot in the code.

Edited by Docfxit

Share this post


Link to post
Share on other sites

@Docfxit
This is what I was meaning:

#include <MsgBoxConstants.au3>

Global $strFileName = @ScriptDir & "\SourceFile.au3", _
       $strFileContent = ""

If FileExists($strFileName) Then
    $strFileContent =  FileRead($strFileName)
    MsgBox($MB_ICONINFORMATION, "Before the replacement", $strFileContent)
    $strFileContent = StringRegExpReplace($strFileContent, '(?s)If _OSVersion\(\).*?EndIf', '#include "OSVersion.au3"')
    MsgBox($MB_ICONINFORMATION, "After the replacement", $strFileContent)
Else
    MsgBox($MB_ICONERROR, "", "Couldn't find the file '" & $strFileName & "'.")
    Exit
EndIf

SourceFile.au3 content:

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 & '"' )

:)


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

I can't seem to clear out the array $aFile

I have tried each one of these one at a time:

DIm $aFile[0]
;   Local $aFile = 0
;   Local $aFile = _FileCountLines($sSrcFile)
;   _ArrayClear($aFile)     ;Function Not found

The array $aFile is 148 records when it processes the first file.

The array does change to 132 records when it process the second file but the records in the array between 133-148 are still in memory.

How can I clear out the array?

 

Share this post


Link to post
Share on other sites

@Docfxit
Did you run the code above?


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
7 minutes ago, FrancescoDiMuro said:

@Docfxit
Did you run the code above?

Yes.  Thank you.

It does:

1. Display the file before

2. Display the file after

3. Does not display that it couldn't find the file.

Good script to have.

Thanks,

Docfxit

Share this post


Link to post
Share on other sites

:mellow:


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

I haven't been able to clear the file yet.

I ran into another problem.  If the .au3 file doesn't have the code I'm looking to remove the script is giving me an error:

RemoveLinesNotFound.jpg.f87b116cd4b0224def09b161ca95acb7.jpg

It's ok if the script doesn't have the code.  I'd like to write the file to the $sDestFolder without deleting any lines.

 

Share this post


Link to post
Share on other sites
Posted (edited)

@Docfxit

You're still trying to make an intricate script when you already have the solution there.

@mikell

Thank you ^_^

EDIT: @extended is set even if there's an error, so, better check @error code, isn't it? :)

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
4 minutes ago, FrancescoDiMuro said:

@extended is set even if there's an error, so, better check @error code, isn't it?

Check @extended for the number of replacements performed.
If the wanted sequence is not found in $strFileContent, then no replacement occurs, the whole text is returned 'as is' and @extended is set to 0  ^_^

 

 

Share this post


Link to post
Share on other sites
Posted (edited)
19 hours ago, Docfxit said:

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.

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

:)

I guess you didn't see my reply to this script.

Edited by Docfxit

Share this post


Link to post
Share on other sites

This is the what I have for your suggestion:

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
;this is the last line I tried:
FileWrite($sFixedFolder & $sFileName, StringRegExpReplace(FileRead($sFixedFolder & $sFileName), '(?s)If _OSVersion\(\).*EndIf', '#include "OSVersion.au3"') & @CRLF) ; .*? If you have others If...EndIf statements
;I don't understand what is supposed to be in $strFileContent or how to make it write the new file
$strFileContent = StringRegExpReplace($strFileContent, '(?is)If _OSVersion\(\).*?EndIf', "")
    If @extended Then $strFileContent  = '#include "OSVersion.au3"' & @crlf & $strFileContent

 

Share this post


Link to post
Share on other sites
Posted (edited)

@mikell

Sorry for didn't pay attention.

Quite busy and very tired in this period.

Thank you again for the "2 Cents" of today :)

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)
1 hour ago, Docfxit said:

;I don't understand what is supposed to be in $strFileContent or how to make it write the new file

OK. Here is the concept, using Francesco's way. Please note that you will have to put in the correct paths, the error checking, and any other needed stuff

#include <File.au3>

$sFromFolder = @desktopdir
$sFixedFolder = @desktopdir & "\test"

ReplaceLines($sFromFolder, $sFixedFolder, "*.au3")

Func ReplaceLines($sSrcFolder, $sDestFolder, $files)
  Local $au3files = _FileListToArray($sSrcFolder, $files, 1), $n
  For $i = 1 To $au3files[0]
      $strFileContent =  FileRead($sSrcFolder & "\" & $au3files[$i])
      $strFileContent = StringRegExpReplace($strFileContent, '(?s)If _OSVersion\(\).*?EndIf', "")
      If @extended Then 
            $strFileContent  = '#include "OSVersion.au3"' & @crlf & $strFileContent 
            $hfile = FileOpen($sDestFolder & "\" & $au3files[$i], 1 + 8)
            FileWrite($hfile, $strFileContent)
            FileClose($hfile)
            $n += 1
     EndIf
  Next
  Msgbox(0,"", $n & " files fixed from " & $au3files[0]) 
EndFunc

 

1 hour ago, FrancescoDiMuro said:

Sorry ... etc

Please keep cool  :bye:

Edited by mikell
tiny typo

Share this post


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

 

#include <File.au3>

$sFromFolder = @desktopdir
$sFixedFolder = @desktopdir & "\test"

ReplaceLines($sFromFolder, $sFixedFolder, "*.au3")

Func ReplaceLines($sSrcFolder, $sDestFolder, $files)
  Local $au3files = _FileListToArray($sSrcFolder, $files, 1), $n
  For $i = 1 To $au3files[0]
      $strFileContent =  FileRead($sSrcFolder & "\" & $au3files[$i])
      $strFileContent = StringRegExpReplace($strFileContent, '(?s)If _OSVersion\(\).*?EndIf', "")
      If @extended Then 
            $strFileContent  = '#include "OSVersion.au3"' & @crlf & $strFileContent 
            $hfile = FileOpen($sDestFolder & "\" & $au3files[$i], 1 + 8)
            FileWrite($hfile, $strFileContent)
            FileClose($hfile)
            $n += 1
     EndIf
  Next
  Msgbox(0,"", $n & " files fixed from " & $au3files[0]) 
EndFunc

 

 

Thank you very much for putting this together.  It's amazing how few lines it requires.

Now I have two ways to accomplish this project.

The plus to this solution is it requires a lot less lines of code. I always like to learn new ways to work with AutoIt.

The plus to the other solution is it allows me to customize it to resolve other wants I have.

Thank you very much for putting this together for me. 🙂

 

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