Jump to content

delete a group of strings?


 Share

Recommended Posts

Hello,

Is it possible to delete a grouping of strings, adjusting the remaining rows of strings in the document upward? For example if I want to delete all strings in document but the last 31 lines:

#include <file.au3>

$CountLines = _FileCountLines("C:my_file.txt")
$Adjust = $CountLines - 31  ;find # of lines except last 31 
For $i 1 to $Adjust
;what should be here to delete each unwanted line and shift the remaining lines upward in the file...?? 
Next

Thank you in advance for suggestions.

Link to comment
Share on other sites

Hi, here is one way it can be done

#include <file.au3>
#include <array.au3>

Func _saveLast($saveLast = 31, $file = -1)
    If $file == -1 Then
        $file = FileOpenDialog("Select file", @ScriptDir, "All (*.*)")
    EndIf
    Dim $lines
    _FileReadToArray($file, $lines)
    If ($lines[0] <= $saveLast) Then
        Return 1
    Else
        Local $fileH = FileOpen($file, 2)
        If $fileH = -1 Then
            MsgBox(0, "Error", "Unable to open file for overwriting.")
            Return 0
        EndIf
        For $i = $lines[0] - $saveLast To $lines[0]
            FileWriteLine($fileH, $lines[$i])
        Next
        FileClose($fileH)
    EndIf
    Return 1
EndFunc   ;==>_saveLast

_saveLast()
Link to comment
Share on other sites

Thanks. Is there no way to simply read each line in the file and then either/or "delete" / "backspace" or something similar to that? I just want to read in a .txt file and keep the last 31 lines of the document, without opening dialog boxes or anything like that. I'm sure it can be done, I just don't know the proper syntax for the delete/backspace (shift up) commands...

Link to comment
Share on other sites

Thanks. Is there no way to simply read each line in the file and then either/or "delete" / "backspace" or something similar to that? I just want to read in a .txt file and keep the last 31 lines of the document, without opening dialog boxes or anything like that. I'm sure it can be done, I just don't know the proper syntax for the delete/backspace (shift up) commands...

Sure you can using FileReadLine(),FileWriteLine()

If you don't want the dialog with the function above use something like:

_saveLast(31,"C:\test.txt")

The above will delete all but the last 31 lines of "C:\test.txt"

Link to comment
Share on other sites

Thank you, I appreciate your help greatly however I'm having a hard time following your code. Perhaps I should have mentioned that I'm trying to delete all but the last 31 lines of a file that is already "open" and has been written to, I simply want to keep the ouput to the last 31 lines at all times. I do not wish to create another file if I can avoid it. Example:

...do stuff to get some data from the Internet and write to a file with a pointer called "$TRACKER" (C:My_File.txt)
...keep the output data to only 31 lines, so call "CleanFile()"  

Func CleanFile()
;DELETE ALL LINES EXCEPT LAST 31 IN FILE... 
$CountLines = _FileCountLines("C:\My_File.txt")  ;count the # lines in the file
$Adjust = $CountLines - 31  ;find # of lines excluding last 31 
For $i = 1 to $Adjust
FileWriteLine($TRACKER, " ")  ;write a "blankspace" in place of the first line so as to "delete" it..
FileWriteLine($TRACKER, ????)  ;what is the syntax to write a "Backspace" so as to (I believe) shift all lines up?   
Next  ;repeat for each line until only last 31 are remaining

Return  ;go back to main script
EndFunc

I think what I have would work but I do not know how to properly "write" a "backspace" character to each line...sorry for my confusion.

Link to comment
Share on other sites

Can you post details of the text file where you want to delete group of strings?

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Yes, sure...thank you. Here is an example of the data. The file is not necessarily always the same length. I'm simply looking to "keep" the last 31 lines of the file at all times and delete any other lines "above" those.

 

Thanks again

Link to comment
Share on other sites

Try this:

#include <file.au3>
Global Const $filename = "my_file.txt" ;file to read
Global Const $new_filename = "my_file_new.txt" ;new file name
Global Const $lines = _FileCountLines($filename) ;get amount of lines
Global Const $offset = 31 ;how many lines from end shouldn't be deleted
Global Const $start =  $lines - $offset + 1 ;calculate start line where to copy lines
Global $cut
If $lines <= $start Then Exit ConsoleWrite("Nothing to do!" & @CRLF) ;less lines than set in $offset
Dim $aFile
_FileReadToArray($filename, $aFile) ;read file to an array
For $i = $start To $aFile[0] ;copy only last $lines lines
    If $i < $aFile[0] Then
        $cut &= $aFile[$i] & @CRLF
    Else
        $cut &= $aFile[$i]
    EndIf
Next
$hFile = FileOpen($new_filename, 2) ;write to a new file
FileWrite($hFile, $cut)
FileClose($hFile)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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