Jump to content

Opposite of FileWritetoLine??


Recommended Posts

So I made a script to read a file for two specific strings, and return the line numbers. I want to delete everything in between.

I thought there would be an opposite to FileWritetoLine, like FileDeleteLine....

So for example sake... If I have START at line 50, and END at line 100, and I want to delete everything in between, can it be done?

Link to comment
Share on other sites

@wisem2540

Fast made code :

#include <File.au3>
#include <Array.au3>

Local Const $sFileName = "toto.txt"

Local $aFile
_FileReadToArray($sFileName, $aFile)

Local Const $iStartDelLine = 3, $iFinishDelLine = 6

For $iLine = $iStartDelLine To $iFinishDelLine
    _ArrayDelete($aFile, $iStartDelLine)
Next

Local Const $hFile = FileOpen($sFileName, $FO_OVERWRITE)
_FileWriteFromArray($hFile, $aFile, 1)
FileClose($hFile)

Br, FireFox.

Link to comment
Share on other sites

This is my contribution, that would be much faster on large files, only one ReDim needed.

#include <File.au3>

Local Const $sFileName = "toto.txt"
Global $sSearchStringStart = "Start"
Global $sSearchStringEnd = "End"
Local $aFile
_FileReadToArray($sFileName, $aFile)
Global $aTempArray[$aFile[0]]
;~ Local Const $iStartDelLine = 3, $iFinishDelLine = 6
Global $iCount = 0
For $iLine = 1 to $aFile[0]
    If $aFile[$iLine] <> $sSearchStringStart And $aFile[$iLine] <> $sSearchStringEnd Then
        $aTempArray[$iCount] = $aFile[$iLine]
        $iCount += 1
    EndIf
Next
ReDim $aTempArray[$iCount]

Local $hFile = FileOpen($sFileName, $FO_OVERWRITE)
_FileWriteFromArray($hFile, $aTempArray, 1)
FileClose($hFile)

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

Actually after rethinking this whole thing, this would probably be even faster, I haven't done speed comparisons though.

#include <File.au3>


Local Const $sFileName = "toto.txt"
Global $sSearchStringStart = "Start"
Global $sSearchStringEnd = "End"
Local $aFile
_FileReadToArray($sFileName, $aFile)
Global $iCount = 0
For $iLine = 1 to $aFile[0]
    If $aFile[$iLine] <> $sSearchStringStart Then
        $iStart = $iLine
    ElseIf $aFile[$iLine] <> $sSearchStringEnd Then
        $iEnd = $iLine
        ExitLoop
    EndIf
Next

Local $hFile = FileOpen($sFileName, $FO_OVERWRITE)
_FileWriteFromArray($hFile, $aTempArray, 0, $iStart)
_FileWriteFromArray($hFile, $aTempArray, $iEnd)
FileClose($hFile)

This of course is going on the assumption that you don't know where in the original file the start and end lines are, if you already know where these lines are in the file, then just do it this way.

#include <File.au3>
Local Const $sFileName = "toto.txt"
Global $sSearchStringStart = 9
Global $sSearchStringEnd = 16
Local $aFile
_FileReadToArray($sFileName, $aFile)

Local $hFile = FileOpen($sFileName, $FO_OVERWRITE)
_FileWriteFromArray($hFile, $aFile, 0, $sSearchStringStart)
_FileWriteFromArray($hFile, $aFile, $sSearchStringEnd)
FileClose($hFile)
Edited by BrewManNH

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

wisem2540,

To answer your original question

I thought there would be an opposite to FileWritetoLine, like FileDeleteLine....

No, however you might try

_FileWriteToLine("c:\test.txt", 3, "", 1)

The solutions presented above are much better than line by line file manipulation.

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Maybe Slower

Deletes everything bw Start .. End(inclusive).

Local $sRead ;Through FileRead
$sRead = "Something Here Start" & @CRLF & _
"Another Line..." & @CRLF & _
"Some Other Data End" ; or $sRead = FileRead( "FileName.ext" )

Local $sText = StringRegExpReplace($sRead, "(?s:Start.*?End)", '')

MsgBox(64, "Return:", $sText) ; or FileWrite( "FileName.ext", $sText )
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

The solution provide by PhoenixXL is what came to my mind too.

Read the whole file into a variable and use StringRegExpReplace to delete everything between the start and end string. Then overwrite the file with the new string.

I think this will be much faster then any solution using _ArrayDelete.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

This is not a regexp solution, but it works when lines use the full delimiter @CRLF (you can alter that of course).

Local $sRead ;Through FileRead
$sRead = "Something Here" & @CRLF & _
"Another Line..." & @CRLF & _
"Another Line..." & @CRLF & _
"Another Line..." & @CRLF & _
"Some Other Data" ; or $sRead = FileRead( "FileName.ext" )

Local $iFrom = 2, $iTo = 4 ; Delete lines 2 through 4
Local $sText = StringLeft($sRead, StringInStr($sRead , @CRLF, 0, $iFrom -1)) & StringRight($sRead, StringLen($sRead) - StringInStr($sRead , @CRLF, 0, $iTo))

MsgBox( 64, "Return:", $sText ) ; or FileWrite( "FileName.ext", $sText )

I'm not sure about speed differences. The method breaks if there are insufficient lines in the text. That would need to be checked first. It works when deleting lines inbetween the first and last line only.

Edited by czardas
Link to comment
Share on other sites

@czardas, phoenixxl,

The OP want to detect the lines that two string occur in and delete these lines and all lines between inclusively...your solutions do not do that.

Is there a way to detect a string and make regexp backup to the start of the line and delete it?

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

The OP want to detect the lines that two string occur in and delete these lines and all lines between inclusively...your solutions do not do that.

Oh! My Mistake in that case, I have updated the Script

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

@czardas, phoenixxl,

The OP want to detect the lines that two string occur in and delete these lines and all lines between inclusively...your solutions do not do that.

The OP does say he has the line numbers already so what you say is not entirely correct, but indeed a regexp might be better to do everything in one hit.

Is there a way to detect a string and make regexp backup to the start of the line and delete it?

Of course. I have some things to do right now, so why don't you give it a try? ;) Edited by czardas
Link to comment
Share on other sites

@PhoenixXL,

Using this test string can regexp delete lines 1 through 3

Local $sRead ;Through FileRead
$sRead = "line 0 Something Here" & @crlf & _
"line 1 this is the start" & @CRLF & _
"line 2  Another Line..." & @CRLF & _
"line 3 Some Other Data End after terminator token " & @crlf & _ ; or $sRead = FileRead( "FileName.ext" )
"line 4 Some Other Data after deleted line" ; or $sRead = FileRead( "FileName.ext" )
Local $sText = StringRegExpReplace($sRead, "(?s:Start.*?End)", '')
MsgBox(64, "Return:", $sText) ; or FileWrite( "FileName.ext", $sText )

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

This would do

Local $sRead ;Through FileRead
$sRead = "line 0 Something Here" & @CRLF & _
"line 1 this is the start" & @CRLF & _
"line 2  Another Line..." & @CRLF & _
"line 3 Some Other Data End after terminator token " & @CRLF & _ ; or $sRead = FileRead( "FileName.ext" )
"line 4 Some Other Data after deleted line" ; or $sRead = FileRead( "FileName.ext" )
Local $sText = StringRegExpReplace($sRead, "(?i)(.*?Start)(?s:.*?End)(.*\n)", '')
MsgBox(64, "Return:", $sText) ; or FileWrite( "FileName.ext", $sText )

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Is there a way to detect a string and make regexp backup to the start of the line and delete it?

It's called "lookbehind assertion" and can be found here.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@PhoenixXL,

I don't get how the regexp deletes the entire lines containing the start and end delimiters. Can you explain? (and again...thank you for your patience...)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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