Jump to content

Question - How to delete a single line


caramen
 Share

Recommended Posts

@pixelsearch

1) I already seen M23 comenting somone that was changing a UDF file. There is no point to do this. Exept for someone like you that got the skill to do it. I will never pretend that I can do better than the AIT team. I dont say you do :) But that's why i wont open an include.

3)I noticed that :

If you run these macro on a file if it is the last line. It wont react same as if you do it in middle line of the file.

On the last line it will definitly delet it.

On a line in the middle nop, just blank exept if you do it 3 times.

 

At the end just this macro have to be reworked that's sure :)You revealed certain inconsistencies. Or who know... just the AIT team know :P 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

On 11/22/2018 at 2:22 PM, Melba23 said:

So the question resurfaces - which of the 2 functionalities is more useful?

IMHO, you wouldn't expect a function called _FileWriteToLine to delete a line from a file would you? What I would expect is that it is used to write lines to a file, not delete them. There's a lot better ways to delete a single line from a file than using the function with the wrong name to do it. 

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Haha forgot about that one.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

1 hour ago, BrewManNH said:

IMHO, you wouldn't expect a function called _FileWriteToLine to delete a line from a file would you?

This is common sense, I like that approach.

Though Melba23's rewrited script does the job nicely (see page before), I'd prefer a separate function to delete line(s) in a file, maybe a single line to delete, or a range of lines, why not lines numbers populating an array : the parameter "line number(s) to delete" could be multitask.

Actually _FileWriteToLine() and its last parameter already made me frown when I read this in the help file :
"By default, the function will return an error if the line does not exist in the file, but setting the $bFill parameter to True will force it will add blank lines to pad the file to a size allowing the line to be written."
So here is an error which finally is not an error, depending of the value of another parameter, great... ^_^

imho, that last $bFill parameter could have been avoided : if the user starts to play with that file as a canvas (or a puzzle) to leave blank lines in the end, followed by a non-blank line, that's up to him. And well, if he *really* wants to play that game, let him add by himself some leading @CRLF's before his string.

Now look at this commented line in Melba23's rewrited script :
; Delete a line - obviously the $sText parameter is ignored

Which means, if the same function is also used for deleting lines, we will have a mandatory parameter ($sText) that is useless because the function doesn't need it when called for deletion, great... ^_^

Please let's keep all this simple : _FileDeleteLine() where are you hiding ?

 

On 24/11/2018 at 5:32 AM, caramen said:

...But that's why i wont open an include.

imho you really should, because it's very interesting to see how AutoIt staff programmed them nicely and they're a great teaching tool. Of course I don't understand them fully because of my limited knowledge, but it's very rewarding when you find there what you were looking for. How do you think I discovered why you constantly had the "error 7" ? :P

The only precaution you'll have to take (and I took it months ago) : please apply the read-only attribute to all include files, so nothing bad will ever happen to them. Only then it won't be risky to edit and study them : back to school :)

 

Link to comment
Share on other sites

I wouldn't use _FileCreate to delete a file, or FileRead(Line) to delete it either. FileMove is a unique case, and doesn't apply here as there's no FileRename in AutoIt. Only FileWriteToLine seems to have this concept being applied to it, and it doesn't apply.

If you want to delete a line in a file, do it with something else, and remove any functionality that would do that from this function. It doesn't belong in it, and should never have been implemented, and shouldn't be implemented going forward. This is my opinion -> We already have too much bloat in the UDF functions as it is, we don't need to have functions doing things they're not designed to do, and shouldn't be designed to do.

 

To show how easy it actually is to delete a single line from a file, here's an example script.

#include <MsgBoxConstants.au3>
; Delete this line <<<<<<<<<<<<<<<<<<<


_FileDeleteLine(@ScriptFullPath, 2)
ShellExecute(@ScriptFullPath)
Func _FileDeleteLine($InFile, $DeleteLine)
    Local $aArray = FileReadToArray($InFile)
    Local $iLineCount = @extended
    Local $aNewArray[$iLineCount]
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "There was an error reading the file. " & $InFile & @CRLF & "@error: " & @error) ; An error occurred reading the current script file.
    Else
        For $Loop = 0 To $DeleteLine - 1
            $aNewArray[$Loop] = $aArray[$Loop]
        Next
        For $Loop = $DeleteLine To $iLineCount - 1
            $aNewArray[$Loop - 1] = $aArray[$Loop]
        Next
        Local $hOutfile = FileOpen($InFile, 2)
        For $i = 0 To $iLineCount - 1
            FileWriteLine($hOutfile, $aNewArray[$i] & @CRLF)
        Next
    EndIf
EndFunc   ;==>_FileDeleteLine

This uses only internal AutoIt functions, no includes, and is very fast. Of course, there's no error or bounds checking in this script, and should only be used as a base from which to grow a real function, but you can't get much simpler.

Edited by BrewManNH
Added code example

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

2 hours ago, BrewManNH said:

we don't need to have functions doing things they're not designed to do

It's a real question with UDFs
Concerning _FileWriteToLine, logically the "$fOverwrite = 0" parameter should not exist, because if the line is not overwritten then it is added, and the func to do this should be named _FileAddLine or _FileInsertLine
But both features are present in _FileWriteToLine, while for arrays there are  :  assignment (overwrite), _ArrayInsert (dont overwrite), and for deletion _ArrayDelete

OTOH _FileWriteToLine used with "" effectively erases the line (which is absolutely correct because "" is a string ) ... and this looks a lot like a line deletion
 

BTW about FileMove, of course I was joking :idiot:

Link to comment
Share on other sites

 

1 minute ago, mikell said:

OTOH _FileWriteToLine used with "" effectively erases the line (which is absolutely correct because "" is a string )

But removing text from a line and replacing it with a blank string isn't the same as deleting line 3 from a 10 line file, creating a 9 line file. A blank line still keeps it a 10 line file.

3 minutes ago, mikell said:

Concerning _FileWriteToLine, logically the "$fOverwrite = 0" parameter should not exist,

Agreed, if you want to write to line 3, then it should overwrite what's in line 3, not move line 3 to 4 and inserting a new line 3.

Too many changes being made without people asking themselves whether it makes sense that it should be in there at all.

Reminds me of what happened to _ArrayDisplay before it was renamed to _Debug_ArrayDisplay, none of the changes should have been in there because it was a debugging function, not a lazy man's listview.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

6 minutes ago, BrewManNH said:

Too many changes being made without people asking themselves whether it makes sense

Basically, you're right. The whole reasoning is flawless...
I just meant that if the implementation of the "overwrite or not" feature was accepted , then there is no reason to reject the "line deleted or blank" one

I didn't read the long discussion about _FileWriteToLine mentioned by Melba, and I definitely will take an hour to do it carefully  :)

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