Jump to content

File Management


anixon
 Share

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
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?

Link to comment
Share on other sites

Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

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
Link to comment
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]
Link to comment
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

Link to comment
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.
Link to comment
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.
Link to comment
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.

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