Sign in to follow this  
Followers 0
anixon

File Management

27 posts in this topic

What is the simple solution for clearing out the contents of a *.txt file rather than deleting and then recreating it? The solution has just not jumped out of Help

CODE
;//Empty the Batch File [Delete and then Recreate]

FileClose($sBatchFile)

ProcessWaitClose($sBatchFile, 30)

FileSetAttrib($BatchFile, "-RS")

FileDelete($BatchFile)

;//New Batch File

If Not FileExists($BatchFile) Then

$sBatchFile = FileOpen($BatchFile, 9)

FileClose($sBatchFile)

ProcessWaitClose($sBatchFile, 30)

FileSetAttrib($BatchFile, "+RS")

EndIf

Assistance is always appreciated. Ant..

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

Do an empty FileWrite()??

This does not work

CODE

FileSetAttrib($BatchFile, "-RS")

$SmsFile = FileOpen($BatchFile, 9)

FileWrite()

FileClose($SmsFile)

ProcessWaitClose($SmsFile, 30)

FileSetAttrib($BatchFile, "+RS")

and neither does this

CODE
FileSetAttrib($BatchFile, "-RS")

$SmsFile = FileOpen($BatchFile, 9)

FileWrite($batchfile,"")

FileClose($SmsFile)

ProcessWaitClose($SmsFile, 30)

FileSetAttrib($BatchFile, "+RS")

Why is it so??? Ant..

Edited by anixon

Share this post


Link to post
Share on other sites

This does not work

CODE

FileSetAttrib($BatchFile, "-RS")

$SmsFile = FileOpen($BatchFile, 9)

FileWrite()

FileClose($SmsFile)

ProcessWaitClose($SmsFile, 30)

FileSetAttrib($BatchFile, "+RS")

Try with opening the file in mode 2

...

$SmsFile = FileOpen($BatchFile, 2)

...

Are you sure you need ProcessWaitClose to reset the attributes?

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Func _FileClear($pFile)
    $hFile = FileOpen($pFile, 2)
    If @error Then Return SetError(@error,0,0)
    FileClose($hFile)
    Return 1
EndFunc
Manadar! You should know better: FileOpen doesn't set an error on failure, just returns -1.

Func _FileClear($pFile)
    $hFile = FileOpen($pFile, 2)
    If $hfile = -1  Then Return SetError($hfile,0,0)
    FileClose($hFile)
    Return 1
EndFunc

Share this post


Link to post
Share on other sites

Manadar! You should know better: FileOpen doesn't set an error on failure, just returns -1.

Func _FileClear($pFile)
    $hFile = FileOpen($pFile, 2)
    If $hfile = -1  Then Return SetError($hfile,0,0)
    FileClose($hFile)
    Return 1
EndFunc
same sort of question, but, how would you go about erasing 1 letter (backspace) in a text document

[u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

same sort of question, but, how would you go about erasing 1 letter (backspace) in a text document

Read in the file, use the string* funcs to edit your text and then write it back.

Edit: Backspace is a letter? :)

Edit2: Apparently it is, did't know that

Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites

Read in the file, use the string* funcs to edit your text and then write it back.

Edit: Backspace is a letter? :)

Edit2: Apparently it is, did't know that

sorry, by using backspace


[u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]

Share this post


Link to post
Share on other sites

Manadar! You should know better: FileOpen doesn't set an error on failure, just returns -1.

A minor trivial problem. :)

Ofcourse, my logic does not apply here since the OP had bad logic to begin with.

Share this post


Link to post
Share on other sites

Edit: Backspace is a letter? :)

Only in old implementations of programs. It has become redundant today, and many applications will not handle it.

Share this post


Link to post
Share on other sites

Only in old implementations of programs. It has become redundant today, and many applications will not handle it.

StringTrimRight() would work, but it would take too long to process...anything alittle faster?


[u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]

Share this post


Link to post
Share on other sites

StringTrimRight() would work, but it would take too long to process...anything alittle faster?

I think any solution that works goes beyond the scope of AutoIt. Consider using another language to do this task for you.

Share this post


Link to post
Share on other sites

This does work, but if I use a larger text file (6.5MB) it still does it with reasonable speed... :)

Global $diff, $diff2
$file = FileOpenDialog ("File", @MyDocumentsDir, "Text Documents (*.txt)")

$ret = _FileClear($file)
MsgBox (0, "", $ret & @CRLF & "Time 1 = " & $diff & "ms" & @CRLF & "Time 2 = " & $diff2 & "ms")

Func _FileClear($pFile)
    $timer = TimerInit ()
    $sRead = FileRead ($pFile)
    $diff = TimerDiff ($timer)
    $timer = TimerInit ()
    $sTemp = StringTrimRight ($sRead, 1)
    $diff2 = TimerDiff ($timer)
    $hFile = FileOpen($pFile, 2)
    If $hfile = -1  Then Return SetError($hfile,0,0)
    FileWrite ($hfile, $sTemp)
    FileClose($hFile)
    Return 1
EndFunc

Cheers,

Brett

Share this post


Link to post
Share on other sites

I think any solution that works goes beyond the scope of AutoIt. Consider using another language to do this task for you.

There are actually a few ways to remove from the end of a file on the forum, use the search function to find them. As far as the view that something is beyond the scope of AutoIt, I think that the scope of a language is only as broad as the coder is creative.

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

There are actually a few ways to remove from the end of a file on the forum, use the search function to find them. As far as the view that something is beyond the scope of AutoIt, I think that the scope of a language is only as broad as the coder is creative.

You are entitled to your own opinion. Enjoy writing kernel drivers in AutoIt.

Share this post


Link to post
Share on other sites

You are entitled to your own opinion. Enjoy writing kernel drivers in AutoIt.

Touche, but the two scenarios are hardly comparable.

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

Touche, but the two scenarios are hardly comparable.

You're probably right. File operations can not be sped up much by choosing another language. This isn't my question though, so I'm going to crawl back under my programmer rock.

Share this post


Link to post
Share on other sites

You're probably right. File operations can not be sped up much by choosing another language. This isn't my question though, so I'm going to crawl back under my programmer rock.

For the record i didn't mean to come off as condescending as i did, it's just a bit of a peeve of mine when people blame the language for their inability to solve a problem.


1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

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