Jump to content

Delete an empty line in a file with _FileWriteToLine


himago03
 Share

Recommended Posts

  • Moderators

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:

#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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • 1 year later...

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

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