jorgeng Posted May 20, 2010 Share Posted May 20, 2010 I have opened a file with fileopen. What is the easiest autoit command to empty file without needing to delete/create the file? Link to comment Share on other sites More sharing options...
Developers Jos Posted May 20, 2010 Developers Share Posted May 20, 2010 I have opened a file with fileopen.What is the easiest autoit command to empty file without needing to delete/create the file? FileOpen() with the right parameter? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
jorgeng Posted May 20, 2010 Author Share Posted May 20, 2010 FileOpen() with the right parameter?Yes, i have done fileopen, fileread and get the value.The value is Trade in one row. I want to replace it with NoTrade on one row, meaningdoing a rewrite with new value. Link to comment Share on other sites More sharing options...
Tvern Posted May 20, 2010 Share Posted May 20, 2010 I think what Jos meant was this: help on FileOpen(): Parameters: ... [mode]: [optional] Mode to open the file in ... 2 = Write mode (erase previous contents) Link to comment Share on other sites More sharing options...
jorgeng Posted May 20, 2010 Author Share Posted May 20, 2010 (edited) I think what Jos meant was this: help on FileOpen(): Parameters: ... [mode]: [optional] Mode to open the file in ... 2 = Write mode (erase previous contents) So if i first do a fileopen 0 and fileread, can i depending on the result doing a fileopen 2 which emptying the file, and then fileopen 1, filewrite with new data? Edited May 20, 2010 by jorgeng Link to comment Share on other sites More sharing options...
Tvern Posted May 20, 2010 Share Posted May 20, 2010 So if i first do a fileopen 0 and fileread, can i depending on the result doinga fileopen 2 which emptying the file, and then fileopen 1, filewrite with new data? almost..1. FileOpen with mode=02. FileRead3. FileClose4. FileOpen with mode=25. FileWrite6. FileCloseNo need to fileopen with mode= 1 to write the data as mode=2 is alreay write mode. And always remember to close your file handles. Link to comment Share on other sites More sharing options...
GEOSoft Posted May 20, 2010 Share Posted May 20, 2010 almost..1. FileOpen with mode=02. FileRead3. FileClose4. FileOpen with mode=25. FileWrite6. FileCloseNo need to fileopen with mode= 1 to write the data as mode=2 is alreay write mode. And always remember to close your file handles.Better yet, You don't need to open a file for reading$sStr = FileRead("somefile") 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 More sharing options...
Tvern Posted May 20, 2010 Share Posted May 20, 2010 I like to always FileOpen, FileClose. Otherwite I'm sure I'll start forgetting to properly close open files Link to comment Share on other sites More sharing options...
GEOSoft Posted May 20, 2010 Share Posted May 20, 2010 I like to always FileOpen, FileClose. Otherwite I'm sure I'll start forgetting to properly close open filesI'm just pointing out that it doesn't have to be opened in any mode to read it. If he wants to read from the file the FileRead() does that without opening it at all. If he wants to empty the file contents then it should be$hFile = FileOpen("somefile.txt", 2);; handles are best hereFileWrite($hFile, "")FileClose($hFile)And to replace the contents$hFile = FileOpen("somefile.txt", 2);; handles are best hereFileWrite($hFile, "Some new string")FileClose($hFile)Remember, a file can be open for reading and writing at the same time. 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 More sharing options...
Tvern Posted May 20, 2010 Share Posted May 20, 2010 Remember, a file can be open for reading and writing at the same time.How would that be done? Nested fileopens don't work for me and mode 0+1 just results in write mode (obviously). Link to comment Share on other sites More sharing options...
99ojo Posted May 20, 2010 Share Posted May 20, 2010 (edited) Hi, you can read in a file, which is open in write mode, can't you? But you can't write to file, which is open in read mode.... ;-)) Stefan Edited May 20, 2010 by 99ojo Link to comment Share on other sites More sharing options...
Tvern Posted May 20, 2010 Share Posted May 20, 2010 I've tried allot of combinations, but I can't seem to do it. There is no need for me to open a file in read/write mode anyways. I think the OP's question is answered and I'm just streching this topic now though. Link to comment Share on other sites More sharing options...
JohnOne Posted May 20, 2010 Share Posted May 20, 2010 I would use #Include <File.au3> $var = Fileread($file) _FileWriteToLine($file,$line,$text,1) ;write line overwriting the existing line AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
99ojo Posted May 20, 2010 Share Posted May 20, 2010 (edited) Hi,Sorry correction:You have to use FileOpen (..., 0) to use FileRead or FileReadLine with an file handle.See helpfile:Failure: Sets @error to 1 if file not opened in read mode or other error.Feeling like..... ;-))StefanBTW @JohnOne: You are using a filehandle without a FileOpen -> $file isn't declared Edited May 20, 2010 by 99ojo Link to comment Share on other sites More sharing options...
JohnOne Posted May 20, 2010 Share Posted May 20, 2010 Not if $file contains a string path to the file, an open and close is auto performed. Also its nice if your file contains more lines than one and you only need to work with one of them, just use FileReadLine($file,$line) AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jorgeng Posted June 4, 2010 Author Share Posted June 4, 2010 I would use #Include <File.au3> $var = Fileread($file) _FileWriteToLine($file,$line,$text,1) ;write line overwriting the existing line I tried this and it works, and does what i want. I have contracted a company building a php script on server and i had problem whith the delete file which crashed the script since php wanted access to file too. I think this is a great way to update a single line in a file without needing to open file, check status and so on since in my case this can happens on the same second from many scripts. Thanks. Link to comment Share on other sites More sharing options...
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