autoitxp Posted November 21, 2008 Share Posted November 21, 2008 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 More sharing options...
FireFox Posted November 21, 2008 Share Posted November 21, 2008 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 More sharing options...
Aceguy Posted November 21, 2008 Share Posted November 21, 2008 (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 November 21, 2008 by Aceguy [u]My Projects.[/u]Launcher - not just for games & Apps (Mp3's & Network Files)Mp3 File RenamerMy File Backup UtilityFFXI - Realtime to Vana time Clock Link to comment Share on other sites More sharing options...
autoitxp Posted November 22, 2008 Author Share Posted November 22, 2008 (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 November 22, 2008 by autoitxp Link to comment Share on other sites More sharing options...
rasim Posted November 22, 2008 Share Posted November 22, 2008 autoitxp how to remove last word in text fileExample: $sFile = @ScriptDir & "\test.txt" $sRead = FileRead($sFile) $sRead = StringRegExpReplace($sRead, "(^[\w-\W]*) (.*)", "\1") $hFile = FileOpen($sFile, 2) FileWrite($hFile, $sRead) FileClose($hFile) Link to comment Share on other sites More sharing options...
rasim Posted November 22, 2008 Share Posted November 22, 2008 autoitxp How to write on that last word ?This? $sFile = @ScriptDir & "\test.txt" $sRead = FileRead($sFile) $sRead = StringRegExpReplace($sRead, "(^[\w-\W]*) (.*)", "\1 new_word") $hFile = FileOpen($sFile, 2) FileWrite($hFile, $sRead) FileClose($hFile) Link to comment Share on other sites More sharing options...
autoitxp Posted November 22, 2008 Author Share Posted November 22, 2008 (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 November 22, 2008 by autoitxp Link to comment Share on other sites More sharing options...
rasim Posted November 23, 2008 Share Posted November 23, 2008 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 wordYou tried to run above code? Backup your file and run code. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now