himago03 Posted November 21, 2018 Share Posted November 21, 2018 Hi, I used _FileWriteToLine to delete lines in text files (not just leaving them empty) but it's not working anymore and i can't undertand why.. _FileWriteToLine($file, $line, "", 1) is this still working ?? it just delete the text line leaving me an empty line, it shoudn't... Link to comment Share on other sites More sharing options...
himago03 Posted November 21, 2018 Author Share Posted November 21, 2018 autoit 3.3.14.3 removed autoit 3.3.14.2 installed _FileWriteToLine($file, $line, "", 1) working again, the line in removed, not just the text erased... don't know if this has been done on purpose... sticking on 3.3.14.2 for now Link to comment Share on other sites More sharing options...
himago03 Posted November 21, 2018 Author Share Posted November 21, 2018 actually i was on 3.3.14.5 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 22, 2018 Moderators Share Posted November 22, 2018 himago03, I see the problem - and I remember why it happens. There was considerable discussion when the function was rewritten some years ago as to whether this "delete a line" functionality should be retained as it removes the possibility of replacing an existing line with a blank one. If it were retained, the only option to "overwrite" with a blank line was to remove the line completely and then rewrite a new blank one, which required 2 calls to the function. The consensus as I recall was that overwriting a line with a blank was more likely than deleting a line - and so the code was amended, but not the Help file. So the question resurfaces - which of the 2 functionalities is more useful? Or should both be available - as in this suggestion for a new version of the function: expandcollapse popup#include <File.au3> #include <MsgBoxConstants.au3> ; These constants added to FileConstants.au3 Const $FWTL_INSERT = 0 Const $FWTL_OVERWRITE = 1 Const $FWTL_DELETE = 2 $sFile = @ScriptDir & "\test.txt" $sContent = "1" & @CRLF & "2" & @CRLF & "3" & @CRLF & "4" FileDelete($sFile) FileWrite($sFile, $sContent) ; Insert a line $iRet = _FileWriteToLine_Mod($sFile, 3, "fred", $FWTL_INSERT) ConsoleWrite($iRet & " - " & @error & @CRLF) FileDelete($sFile) FileWrite($sFile, $sContent) ; Overwrite a line $iRet = _FileWriteToLine_Mod($sFile, 3, "fred", $FWTL_OVERWRITE) ConsoleWrite($iRet & " - " & @error & @CRLF) FileDelete($sFile) FileWrite($sFile, $sContent) ; Delete a line - obviously the $sText parameter is ignored $iRet = _FileWriteToLine_Mod($sFile, 3, "fred", $FWTL_DELETE) ConsoleWrite($iRet & " - " & @error & @CRLF) FileDelete($sFile) ; Use 3 mode options - insert, overwrite, delete Func _FileWriteToLine_Mod($sFilePath, $iLine, $sText, $iMode = $FWTL_INSERT, $bFill = False) If $iMode = Default Then $iMode = $FWTL_INSERT If $bFill = Default Then $bFill = False If Not FileExists($sFilePath) Then Return SetError(2, 0, 0) If $iLine <= 0 Then Return SetError(4, 0, 0) If $iMode < 0 Or $iMode > 2 Then Return SetError(5, 0, 0) If Not IsString($sText) Then $sText = String($sText) If $sText = "" Then Return SetError(6, 0, 0) EndIf If Not IsBool($bFill) Then Return SetError(7, 0, 0) ; Read current file into array Local $aArray = FileReadToArray($sFilePath) ; Create empty array if empty file If @error Then Local $aArray[0] Local $iUBound = UBound($aArray) - 1 ; If Fill option set If $bFill Then ; If required resize array to allow line to be written If $iUBound < $iLine Then ReDim $aArray[$iLine] $iUBound = $iLine - 1 EndIf Else If ($iUBound + 1) < $iLine Then Return SetError(1, 0, 0) EndIf Switch $iMode Case 0 ; Insert $aArray[$iLine - 1] = $sText & @CRLF & $aArray[$iLine - 1] Case 1 ; Overwrite $aArray[$iLine - 1] = $sText Case 2 ; Delete _ArrayDelete($aArray, $iLine - 1) $iUBound -= 1 EndSwitch ; Concatenate array elements Local $sData = "" For $i = 0 To $iUBound $sData &= $aArray[$i] & @CRLF Next $sData = StringTrimRight($sData, StringLen(@CRLF)) ; Required to strip trailing EOL ; Just for illustration purposes MsgBox($MB_SYSTEMMODAL, $iMode, $sData) ; Write data to file Local $hFileOpen = FileOpen($sFilePath, FileGetEncoding($sFilePath) + $FO_OVERWRITE) If $hFileOpen = -1 Then Return SetError(3, 0, 0) FileWrite($hFileOpen, $sData) FileClose($hFileOpen) Return 1 EndFunc ;==>_FileWriteToLine M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
saywell Posted June 3, 2020 Share Posted June 3, 2020 Hi, M23 I've just returned to AutoIt scripting after several years' absence. I need to delete a number of lines from the middle section of a text file, thus keeping the first few lines and the last few lines. Has your function seen the light of day yet, as it seems to have the option to do just what I need? Regards, William Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 3, 2020 Moderators Share Posted June 3, 2020 saywell, We decided that we would not add the functionality to delete lines to a function whose raison-d'etre was to write lines! I suggest you read the file into an array and then delete the required lines before rewriting the remainder back into the file. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
saywell Posted June 3, 2020 Share Posted June 3, 2020 2 minutes ago, Melba23 said: saywell, We decided that we would not add the functionality to delete lines to a function whose raison-d'etre was to write lines! I suggest you read the file into an array and then delete the required lines before rewriting the remainder back into the file. M23 Thank, Melba. I see the logic. Perhaps File Edit Line would have covered both. Or a File Delete Line function separately? I'll look at the array read/write method. Regards, William 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