Jump to content

How to backspace last word in text file


autoitxp
 Share

Recommended Posts

Hi

how to remove last word in text file i didnt find any help in docs which can remove last written word in text or log file ?

#include <Misc.au3>


$dll = DllOpen("user32.dll")

$file = FileOpen("test.txt", 1)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    Sleep ( 250 )
    If _IsPressed("08", $dll) Then;BACKSPACE PRESSED
       FileWriteLine($file, "BACKSPACE"); HERE I WANTED TO REMOVE LAST WORD IN TEXT FILE 
        ExitLoop
    EndIf
WEnd
DllClose($dll)







FileClose($file)
Link to comment
Share on other sites

Hi,

Use String Split function with space for last word like this

$Line=FileReadLine("yourfile",1)
$string=StringSplit($Line," ")
Msgbox(0,"",$String[$string[0]])

Then you count the number of letters of your word and you do stringtrimright with count of your word

Link to comment
Share on other sites

UNTESTED

#include <Misc.au3>
#include<array.au3>

$dll = DllOpen("user32.dll")

$file = FileOpen("test.txt", 2)

_FileReadToArray("test.txt", $array)
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    Sleep(250)
    If _IsPressed("08", $dll) Then;BACKSPACE PRESSED
            $ss = StringSplit($array[0], " ")
            If Not @error Then
                $tmp = ""
                For $a = 1 To $ss[0] - 1
                    $tmp &= $ss[$a] & " "
                    
                Next
            EndIf
            FileWriteLine($file, $tmp); HERE I WANTED TO REMOVE LAST WORD IN TEXT FILE
                ExitLoop
    EndIf
WEnd
DllClose($dll)

FileClose($file)
Edited by Aceguy
Link to comment
Share on other sites

how to write on last word its easy to read last word of text file like ;

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("test.html",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf

$String = StringSplit($aRecords[0], " ")
$sstring   = $aRecords[$String[$string[0]]]
$xstring = StringSplit($sstring , "")
Msgbox(0, "" ,          $xstring[$xstring[0]])

How to write on that last word ?

Edited by autoitxp
Link to comment
Share on other sites

man but its FileOpen($sFile, 2) mode 2 will erase previous content ? wanted to append ..

i dont think autoit filewrite function will support to erase singal word

You tried to run above code? Backup your file and run code. :)
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...