Jump to content
Sign in to follow this  
wisem2540

Opposite of FileWritetoLine??

Recommended Posts

wisem2540

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?

Share this post


Link to post
Share on other sites
wisem2540

I was able to get it done using sending the the original flle to an array, using arraydelete, and then writing it back.

While this does work, I would still like to know an answer to my original question

thanks

Share this post


Link to post
Share on other sites
FireFox

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


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
BrewManNH

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

Share this post


Link to post
Share on other sites
kylomas

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

Share this post


Link to post
Share on other sites
PhoenixXL

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.

Share this post


Link to post
Share on other sites
czardas

@BrewmanNH there's errors in your last post.

Edited by czardas

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.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

 

Share this post


Link to post
Share on other sites
czardas

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

Share this post


Link to post
Share on other sites
kylomas

@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

Share this post


Link to post
Share on other sites
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.

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.

Share this post


Link to post
Share on other sites
czardas

@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

Share this post


Link to post
Share on other sites
kylomas

@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

Share this post


Link to post
Share on other sites
kylomas

@czardas,

Of course. I have some things to do right now, so why don't you give it a try? ;)

I wish that I had the regexp (and patience) to do that!

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

Share this post


Link to post
Share on other sites
PhoenixXL

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 )

  • Like 1

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.

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.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

 

Share this post


Link to post
Share on other sites
czardas

PhoenixXL is better at this than me. :) You should only really use StringRegExpReplace if StringReplace is not suitable, which is true in this instance.

Edited by czardas

Share this post


Link to post
Share on other sites
kylomas

@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

Share this post


Link to post
Share on other sites
kylomas

@water - thanks, I've read this and many other tutorials...I'm beginning to think that I just can't get it!


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

Share this post


Link to post
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
Sign in to follow this  

×