Sign in to follow this  
Followers 0
darkmaster071

How to delete contents of a text file? :S

9 posts in this topic

#1 ·  Posted (edited)

Hi, im wondering if there is a function or any way to delete text content of a text file (.log file) without deleting the actual file. I have been searching through the help file and the forums but ive come across nothing usefull. Help much appreciated. Thanks.

Edited by darkmaster071

Share this post


Link to post
Share on other sites



Just open the file in overwrite mode and close it without writing anything to it :blink:

$hFile = FileOpen($sFile, 2)

FileClose($hFile)


*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

Share this post


Link to post
Share on other sites

Thanks, didnt think of that.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Just tinkering around a bit.

test()
If @error Then Exit @error

Func test()
    Local $sFilespec = @ScriptDir & '\' & 'TestFile.tmp'
    If FileExists($sFilespec) And Not FileDelete($sFilespec) Then Return SetError(1)
    If Not FileWrite($sFilespec, 'abcdef') Then Return SetError(2)
    If Not _ZeroWipe_FileContent($sFilespec) Then Return SetError(@error)
    If Not _Zero_FileSize($sFilespec) Then Return SetError(@error)
EndFunc

;; http://www.autoitscript.com/forum/index.php?showtopic=117880&view=findpost&p=820821
Func _ZeroWipe_FileContent($sFilespec)
    Local $iFileSize = FileGetSize($sFilespec)
    If @error Or Not $iFileSize Then Return SetError(11, 0, 0)
    Local $hFile = FileOpen($sFilespec, 2 + 16)
    If $hFile = -1 Or $hFile < 1 Then Return SetError(12, 0, 0)
    If Not FileSetPos($hFile, $iFileSize - 1, 0) Then Return SetError(13, 0, 0)
    If Not FileWrite($hFile, Binary(Chr(0))) Then Return SetError(14, 0, 0)
    If Not FileClose($hFile) Then Return SetError(15, 0, 0)
    Return 1
;~  If Not FileSetPos($hFile, (Ceiling(($iFileSize) / 512) * 512)-1, 0) Then Return SetError(3,0,0) ;; MFT note: This is only good for files that are not stored in MFT system file.
EndFunc
Func _Zero_FileSize($sFilespec)
    Local $hFile = FileOpen($sFilespec, 2)
    If $hFile = -1 Or $hFile < 1 Then Return SetError(22, 0, 0)
    If Not FileClose($hFile) Then Return SetError(25, 0, 0)
    Return 1
EndFunc

+ MFT note.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

_ZeroWipe_FileContent wont work i think.

Edit: OK. Did not understand it :blink:

Edited by ProgAndy

*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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Why do you think that ?

Last time I checked (before posting) it was working fine.

... yep, still working correct on this side. :blink:

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

OK it works, but why would you need that?


*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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Don't know.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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