Jump to content

how to remove the last blank line from a file?


kor
 Share

Recommended Posts

I have an automated output from a database program that spits out a text file with a series of values tab separated.

However it spits out a blank line at the very end of the file which messes up my script when I create the array from all the data.

Is there a command that I can run to delete the last line of a file if it is blank? I'm looking in the "File" commands but I can't find anything that would allow me to set the position to the last line in a file.. and then delete the last line.

Bonus points if someone can show me what I would need to do to check if the line is blank or not.

Link to comment
Share on other sites

  • Moderators

kor,

It would probably be easier to amend the array once you have created it. What does the added line do to your array?

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

kor,

It would probably be easier to amend the array once you have created it. What does the added line do to your array?

M23

If that is the case, how do I code a loop to figure out what the last N line is in my array and delete it? I can figure out how to check if the line is blank in the array.
Link to comment
Share on other sites

  • Moderators

kor,

UBound will tell you how many elements you have in your array - the last element is 1 less than the returned value (because the first element is [0]) and you can feed that value into _ArrayDelete. :mellow:

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

kor,

UBound will tell you how many elements you have in your array - the last element is 1 less than the returned value (because the first element is [0]) and you can feed that value into _ArrayDelete. :mellow:

M23

For $i = 0 To UBound($aRaw) - 1
        If $aRaw[$i][0] = "" Then _ArrayDelete($aRaw[$i])
    Next

?

Link to comment
Share on other sites

  • Moderators

kor,

If you are going to delete lines from an array you should always start from the bottom and work up or else you could end up with an array smaller than the loop size and get the dreaded "index error": :)

For $i = UBound($aRaw) - 1 To 0 Step -1 ; <<<<<<<
    If $aRaw[$i][0] = "" Then _ArrayDelete($aArray, $i) ; <<<<<< Look in the Help file for the syntax for _ArrayDelete!!!!!!
Next

But I thought you only wanted to delete the last line? :mellow:

_ArrayDelete($aRaw, UBound($aRaw) - 1)

All clear? :)

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

kor,

If you are going to delete lines from an array you should always start from the bottom and work up or else you could end up with an array smaller than the loop size and get the dreaded "index error": :)

For $i = UBound($aRaw) - 1 To 0 Step -1 ; <<<<<<<
    If $aRaw[$i][0] = "" Then _ArrayDelete($aArray, $i) ; <<<<<< Look in the Help file for the syntax for _ArrayDelete!!!!!!
Next

But I thought you only wanted to delete the last line? :mellow:

_ArrayDelete($aRaw, UBound($aRaw) - 1)

All clear? :)

M23

got it! thanks.
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...