Jump to content

Empty file


Recommended Posts

  • Developers

I have opened a file with fileopen.

What is the easiest autoit command to empty file without needing to delete/create the file?

:idea:

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

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, meaning

doing a rewrite with new value.

:idea:

Link to comment
Share on other sites

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?

:idea:

Edited by jorgeng
Link to comment
Share on other sites

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?

:idea:

almost..

1. FileOpen with mode=0

2. FileRead

3. FileClose

4. FileOpen with mode=2

5. FileWrite

6. FileClose

No 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

almost..

1. FileOpen with mode=0

2. FileRead

3. FileClose

4. FileOpen with mode=2

5. FileWrite

6. FileClose

No 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

I like to always FileOpen, FileClose. Otherwite I'm sure I'll start forgetting to properly close open files

I'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 here

FileWrite($hFile, "")

FileClose($hFile)

And to replace the contents

$hFile = FileOpen("somefile.txt", 2);; handles are best here

FileWrite($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

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

Link to comment
Share on other sites

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

;-))

Stefan

BTW @JohnOne: You are using a filehandle without a FileOpen -> $file isn't declared

Edited by 99ojo
Link to comment
Share on other sites

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

  • 2 weeks later...

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

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