anixon Posted February 20, 2009 Posted February 20, 2009 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..
AdmiralAlkex Posted February 20, 2009 Posted February 20, 2009 Do an empty FileWrite()?? .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface
anixon Posted February 20, 2009 Author Posted February 20, 2009 (edited) Do an empty FileWrite()??This does not workCODEFileSetAttrib($BatchFile, "-RS")$SmsFile = FileOpen($BatchFile, 9)FileWrite()FileClose($SmsFile)ProcessWaitClose($SmsFile, 30)FileSetAttrib($BatchFile, "+RS")and neither does thisCODEFileSetAttrib($BatchFile, "-RS")$SmsFile = FileOpen($BatchFile, 9)FileWrite($batchfile,"")FileClose($SmsFile)ProcessWaitClose($SmsFile, 30)FileSetAttrib($BatchFile, "+RS")Why is it so??? Ant.. Edited February 20, 2009 by anixon
leos Posted February 20, 2009 Posted February 20, 2009 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?
BrettF Posted February 20, 2009 Posted February 20, 2009 Step 1. RTFM. FileWrite does not accept no parameters. Set the file to the file/opened handle and the text to be "". Cheers, Brett Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
jvanegmond Posted February 20, 2009 Posted February 20, 2009 Func _FileClear($pFile) $hFile = FileOpen($pFile, 2) If @error Then Return SetError(@error,0,0) FileClose($hFile) Return 1 EndFunc github.com/jvanegmond
ResNullius Posted February 20, 2009 Posted February 20, 2009 Func _FileClear($pFile) $hFile = FileOpen($pFile, 2) If @error Then Return SetError(@error,0,0) FileClose($hFile) Return 1 EndFuncManadar! 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
ReaImDown Posted February 20, 2009 Posted February 20, 2009 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]
AdmiralAlkex Posted February 20, 2009 Posted February 20, 2009 (edited) same sort of question, but, how would you go about erasing 1 letter (backspace) in a text documentRead 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 February 20, 2009 by AdmiralAlkex .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface
ReaImDown Posted February 20, 2009 Posted February 20, 2009 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 thatsorry, by using backspace [u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]
jvanegmond Posted February 20, 2009 Posted February 20, 2009 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. github.com/jvanegmond
jvanegmond Posted February 20, 2009 Posted February 20, 2009 Edit: Backspace is a letter? Only in old implementations of programs. It has become redundant today, and many applications will not handle it. github.com/jvanegmond
ReaImDown Posted February 20, 2009 Posted February 20, 2009 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]
jvanegmond Posted February 20, 2009 Posted February 20, 2009 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. github.com/jvanegmond
BrettF Posted February 21, 2009 Posted February 21, 2009 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 Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
seandisanti Posted February 21, 2009 Posted February 21, 2009 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.
jvanegmond Posted February 21, 2009 Posted February 21, 2009 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. github.com/jvanegmond
seandisanti Posted February 21, 2009 Posted February 21, 2009 You are entitled to your own opinion. Enjoy writing kernel drivers in AutoIt.Touche, but the two scenarios are hardly comparable.
jvanegmond Posted February 21, 2009 Posted February 21, 2009 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. github.com/jvanegmond
seandisanti Posted February 21, 2009 Posted February 21, 2009 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.
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