Jump to content

Writing BACKSPACE


Recommended Posts

I have a text file that looks like this:

option1
option2
option3
* empty line*

To append new text at the end of the file I use:

FileWriteLine($file, "some text")

Which results in:

option1
option2
option3
* empty line*
some text

Now I want to get rid of that *empty line* that's in the text file by default, by writing a backspace character.

I have tried this:

FileWriteLine($file, Chr(08) & "some text")

But that ends up looking like this:

option1
option2
option3
* empty line*
some text

Notice that character, which should be a backspace.. What's the proper way to do this?

Edited by faire
Link to comment
Share on other sites

you have to use Winapi Functions ans FileSetPointerEx:

#include<Winapi.au3>
$File = _WinAPI_CreateFile("D:\TEMP\Test.txt",2)
DllCall("kernel32.dll","long","SetFilePointerEx","hwnd", $File, "uint64",-1,"long*",0,"dword",2)
MsgBox(0, '', "")
$b = DllStructCreate("Char[10]")
DllStructSetData($b,1,"asdfghjkl")
Dim $w
_WinAPI_WriteFile($File,DllStructGetPtr($B),9,$w)
_WinAPI_CloseHandle($File)

or you first have to Read the File and then write it from new

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

If you know the line number (in this case 4) then you can use

#Include <File.au3>
_FileWriteToLine($File, 4, "some text")

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Another method would be

$test = StringStripWS(FileRead($file), 2) & @CRLF & "some text"
FileOpen($file,2)
FileWrite($File, $Test)
FileClose($File)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

you have to use Winapi Functions ans FileSetPointerEx:

...

or you first have to Read the File and then write it from new

I tried your code. It does get rid of the empty line, but still adds that character:

option1
option2
option3

asdfghj

Edit: hmm, it shows as a blank line here, but in Notepad it shows a block character (like backspace).

If you know the line number (in this case 4) then you can use

Unfortunately the line number isn't always the same.. Edited by faire
Link to comment
Share on other sites

Another method would be

$test = StringStripWS(FileRead($file), 2) & @CRLF & "some text"
FileOpen($file,2)
FileWrite($File, $Test)
FileClose($File)
You, sir, are the winner :) Works!
Link to comment
Share on other sites

I tried your code. It does get rid of the empty line, but still adds that character:

Edit: hmm, it shows as a blank line here, but in Notepad it shows a block character (like backspace).

Unfortunately the line number isn't always the same..

Jeah, you use @CRLF this are two characters :)

You would have to Change the -1 after the uint64 to -2, so you go back 2 chars :(

DllCall("kernel32.dll","long","SetFilePointerEx","hwnd", $File, "uint64",-1,"long*",0,"dword",2)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Maybe this makes more sense:

#include <File.au3>

Global $sFile = "C:\Temp\Test.txt"
Global $fLastLineBlank = False
Global $iLastLine = 0
Global $sNewLine = "This is my new line."

; Get last line
$iLastLine = _FileCountLines($sFile)

; See if last line is blank
If StringStripWS(FileReadLine($sFile, $iLastLine), 8) = "" Then $fLastLineBlank = True

; Write new line
If $fLastLineBlank Then
    _FileWriteToLine($sFile, $iLastLine, $sNewLine, True)
Else
    FileWriteLine($sFile, $sNewLine)
EndIf

:)

P.S. When you view the file in a text editor and the last line appears empty, but contains no characters at all, that "line" doesn't actually exist. It's an artifact of the @CRLF at the end of the "previous" line. The format of the data is like this:

Line One.<@CRLF>
Line Two.<@CRLF>
But the presentation looks like this:
Line One.
Line Two.
<empty line>
Even though there are only two lines in the file, it looks like three in the editor (with line 3 "empty").

Edit: Fix typo (missing "=").

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

In case you are still interested in this, I've added a function to my Filex.au3 UDF that removes all blank lines from a file.

Filex.au3

As usual there is a download button at the bottom of the page or you can just copy the code to the clipboard.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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