Jump to content

How to write into file without append?


Recommended Posts

Referring to following link, I would like to know on how to write string into file without append option, so the file should only contain "Line 4".

Local Const $sFilePath = "D:\test.txt"
   Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
  
    ; Write data to the file using the handle returned by FileOpen.
   FileWrite($hFileOpen, "Line 2")
   FileWrite($hFileOpen, "This is still line 2 as a new line wasn't appended to the last FileWrite call." & @CRLF)
   FileWrite($hFileOpen, "Line 3" & @CRLF)
   FileWrite($hFileOpen, "Line 4")
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

Does anyone have any suggestions?
Thanks in advance for any suggestions

 

 

https://www.autoitscript.com/autoit3/docs/functions/FileWrite.htm

Edited by oemript
Link to comment
Share on other sites

  • Developers

I do not understand the question looking at the example. Do you want to insert a record in an existing file in between existing other records?
If so: Open the Helpfile and search for the existing solution, else define again what your question is.

Jos 

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

Referring to FileWrite, there is no other option for selection, if I only want to write data into file without appending existing contents, by default it is using $FO_APPEND.

$hFileOpen = FileOpen($sFilePath, $FO_APPEND)

If File D:\test.txt is already existed with contents, I would like to know on how to write string into file and overwrite any existing contents, so for this coding, FileWrite($hFileOpen, "Line 4") file should only contain "Line 4".

Do you have any suggestions?
Thank you very much for any suggestions (^v^)

 

Link to comment
Share on other sites

  • Developers
2 hours ago, oemript said:

I would like to know on how to write string into file and overwrite any existing contents

Seriously? So you simply want to override the existing file and you couldn't find the solution yourself when looking at 

2 hours ago, oemript said:

$hFileOpen = FileOpen($sFilePath, $FO_APPEND)

Please learn to open the helpfile  before posting questions as you have been around long enough to be able to solve stuff yourself.

remember? : 

Jos

Edited by Jos

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

  • Moderators

oemript,

And if you look in the Help file for FileOpen what flags can you see which you might use other than $FO_APPEND and which might do what you want?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Developers
2 hours ago, oemript said:

Thanks, to everyone very much for suggestions (^v^)

I am being serious so do not see any reason why to tag my last post with a HAHA smiley! 

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

  • Moderators

You are on notice @oemript, the entire community is becoming tired of your general laziness and wanting everyone to spoon feed common sense to you. The next one will result in a good long holiday from the forum.

Edited by JLogan3o13

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

$VAR = "hello world"
Run(@comspec & ' /k "echo '& $VAR &' > "D:\test.txt"', "", @SW_HIDE)

 

Edited by Jos
If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

  • Developers
3 hours ago, alienclone said:
$VAR = "hello world"
Run(@comspec & ' /k "echo '& $VAR &' > "D:\test.txt"', "", @SW_HIDE)

 

Yea..  "All roads lead to Rome", but isn't the proper answer here. ;)

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

16 minutes ago, Jos said:

Yea..  "All roads lead to Rome", but isn't the proper answer here. ;)

he asked for any suggestions, my code writes string to file without append which looked to me to be the OP's end goal, how is it not proper?

If @error Then
    MsgBox(262192, "", @ComputerName & " slaps " & @UserName & " around a bit with a large trout!")
EndIf

"Yeah yeah yeah patience, how long will that take?"  -Ed Gruberman

REAL search results  |  SciTE4AutoIt3 Editor Full Version

Link to comment
Share on other sites

  • Developers
2 hours ago, alienclone said:

he asked for any suggestions, my code writes string to file without append which looked to me to be the OP's end goal, how is it not proper?

Well, I would think doing a FIleOpen() with the $FO_OVERWRITE property is a more appropriate answer, don't you think?
... but as said :  "roads.... Rome"....  ;) 

Jos

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

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