Docfxit Posted March 18, 2019 Share Posted March 18, 2019 (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 March 19, 2019 by Docfxit Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted March 18, 2019 Share Posted March 18, 2019 (edited) @Docfxit ConsoleWrite(StringRegExpReplace('File Content', '(?s)If _OSVersion\(\).*EndIf', '#include "OSVersion.au3"') & @CRLF) ; .*? If you have others If...EndIf statements Edited March 18, 2019 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Bilgus Posted March 18, 2019 Share Posted March 18, 2019 Protip: make a backup first Link to comment Share on other sites More sharing options...
Docfxit Posted March 18, 2019 Author Share Posted March 18, 2019 I'm getting an error: I don't have any functions. This is the entire script. Link to comment Share on other sites More sharing options...
Bilgus Posted March 18, 2019 Share Posted March 18, 2019 you need quotes "" Link to comment Share on other sites More sharing options...
Docfxit Posted March 18, 2019 Author Share Posted March 18, 2019 9 minutes ago, Bilgus said: you need quotes "" That solved that problem. Thanks. Link to comment Share on other sites More sharing options...
Docfxit Posted March 18, 2019 Author Share Posted March 18, 2019 (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 March 19, 2019 by Docfxit Link to comment Share on other sites More sharing options...
Docfxit Posted March 19, 2019 Author Share Posted March 19, 2019 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: This is my code: expandcollapse popup#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 Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 It is supposed to be an array with at least one element to use For In Next perhaps that is not the case? Maybe try.. While $sFinished <> 1 And IsArray($_Array) Link to comment Share on other sites More sharing options...
Docfxit Posted March 19, 2019 Author Share Posted March 19, 2019 (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 March 19, 2019 by Docfxit Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 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 Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 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 Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 MMM also don't use Dim for arrays it is depreciated use Local/Global $aArray[n] instead Redim is OK though Link to comment Share on other sites More sharing options...
Docfxit Posted March 19, 2019 Author Share Posted March 19, 2019 Is there a way for me to Dim $aArray[#ofRecordsInFile] ? Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 Sorry I wasn't paying close attention to your code give me a moment to play with it Link to comment Share on other sites More sharing options...
Docfxit Posted March 19, 2019 Author Share Posted March 19, 2019 (edited) I tried both of these: Dim $_Array[FileGetSize("$sFromFolder & $sFileName")] MsgBox( "", "", 'FileGetSize( ' & $sFromFolder & $sFileName & ') = ' & FileGetSize("$sFromFolder & $sFileName")) Dim $_Array[_FileCountLines("$sFromFolder & $sFileName")] They both return 0 records. Edited March 19, 2019 by Docfxit Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 Try this expandcollapse popup#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 Docfxit 1 Link to comment Share on other sites More sharing options...
Docfxit Posted March 19, 2019 Author Share Posted March 19, 2019 (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 March 19, 2019 by Docfxit Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 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 Link to comment Share on other sites More sharing options...
Bilgus Posted March 19, 2019 Share Posted March 19, 2019 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now