Sign in to follow this  
Followers 0
autoitxp

How to backspace last word in text file

8 posts in this topic

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)

Share this post


Link to post
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


 

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

#3 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Edited by autoitxp

Share this post


Link to post
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. :)

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  
Followers 0