Scriptkiddi3 Posted August 27, 2005 Posted August 27, 2005 Hi guys! I have a problem with my script. It should overwrite a file if it already exists... but it doesn't . Can anyone tell me what's wrong with it? expandcollapse popup#include <GuiConstants.au3> $main = GUICreate("TxT Writer", 700, 400, (@DesktopWidth - 700) / 2, (@DesktopHeight - 400) / 2, $ws_overlappedwindow + $ws_visible + $ws_clipsiblings) $edit = GUICtrlCreateEdit("", 30, 40, 640, 230) $groupmain = GUICtrlCreateGroup("TxT Writer", 20, 10, 660, 380) $input = GUICtrlCreateInput("TxT File", 40, 310, 160, 20) $groupsave = GUICtrlCreateGroup("Save", 30, 280, 280, 100) $combo = GUICtrlCreateCombo("", 210, 310, 90, 21) $label = GUICtrlCreateLabel("The TxT file is saved to your desktop.", 40, 345) $button = GUICtrlCreateButton("Save", 240, 340) GUICtrlSetData($combo, ".txt|.log", ".txt") GUICtrlSetColor($label, 0x00CC66) GUICtrlSetBkColor($groupmain, 0xFF0000) GUICtrlSetBkColor($groupsave, 0xFF0000) GUISetState(@SW_SHOW) $saved = 1 $textcheck = "" While 1 $msg = GUIGetMsg() Select Case $msg = $gui_event_close If $textcheck <> GUICtrlRead($edit) Then $saved = 0 EndIf If $saved <> 1 Then $answer = MsgBox(3, "TxT Writer", "This TxT is not saved yet. Do you want to save it now?") Select Case $answer = 6 save() Exit Case $answer = 7 Exit Case $answer = 2 ;-- terug EndSelect Else Exit EndIf Case $msg = $button save() Case Else Sleep(50) EndSelect WEnd Func save() $text = GUICtrlRead($edit) $file = GUICtrlRead($input) & GUICtrlRead($combo) FileDelete($file) Sleep(500) FileWrite(@DesktopCommonDir & "\" & $file, $text) $saved = 1 $textcheck = $text EndFunc Thanks - Scriptkiddie [-"Scriptkiddie, nice to meet you!"-]
HardCopy Posted August 27, 2005 Posted August 27, 2005 To Write to any file you have top open it first / even if it does not exist. $file = FileOpen("test.txt", 2) = open for write overwrite previous HardCopy Contributions: UDF _DateYearFirstChildren are like Farts, you can just about stand your own.Why am I not a Vegetarian?...Well...my ancestors didn't fight & evolve to the Top of the food chain for me to survive on Salad
Developers Jos Posted August 27, 2005 Developers Posted August 27, 2005 Filedelete and Filewrite should work too.. ( when they refer to the same file) Func save() $text = GUICtrlRead($edit) $file = GUICtrlRead($input) & GUICtrlRead($combo) FileDelete(@DesktopCommonDir & "\" & $file) FileWrite(@DesktopCommonDir & "\" & $file, $text) $saved = 1 $textcheck = $text EndFunc ;==>save 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.
HardCopy Posted August 27, 2005 Posted August 27, 2005 Filedelete and Filewrite should work too.. ( when they refer to the same file)Func save() $text = GUICtrlRead($edit) $file = GUICtrlRead($input) & GUICtrlRead($combo) FileDelete(@DesktopCommonDir & "\" & $file) FileWrite(@DesktopCommonDir & "\" & $file, $text) $saved = 1 $textcheck = $text EndFunc ;==>save<{POST_SNAPBACK}>why do i always miss the obvious!!!lol... Contributions: UDF _DateYearFirstChildren are like Farts, you can just about stand your own.Why am I not a Vegetarian?...Well...my ancestors didn't fight & evolve to the Top of the food chain for me to survive on Salad
MHz Posted August 27, 2005 Posted August 27, 2005 To Write to any file you have top open it first / even if it does not exist.$file = FileOpen("test.txt", 2) = open for write overwrite previousHardCopy<{POST_SNAPBACK}>No you don't. You can do a direct filewrite without using fileopen. Try it.
Scriptkiddi3 Posted August 27, 2005 Author Posted August 27, 2005 Thanks, I just missed the '@DesktopCommonDir & "\" & $file' [-"Scriptkiddie, nice to meet you!"-]
ReFran Posted August 27, 2005 Posted August 27, 2005 To Write to any file you have top open it first / even if it does not exist.$file = FileOpen("test.txt", 2) = open for write overwrite previousHardCopy<{POST_SNAPBACK}>Yes - and it is also good practice to close the file. Simple look at example in the help file - sometimes it helps.Also I don't understand, why you use different $file statements in FileDelete and FileWrite.HTH, Reinhard
ReFran Posted August 27, 2005 Posted August 27, 2005 Oh, should learn to write faster. Best regards, Reinhard
MHz Posted August 27, 2005 Posted August 27, 2005 If you do not use fileopen, then fileclose is not required. Filewrite can work without them. If you write often, then use them.
ReFran Posted August 27, 2005 Posted August 27, 2005 If you do not use fileopen, then fileclose is not required. Filewrite can work without them. If you write often, then use them.<{POST_SNAPBACK}>Thanks for the hint."If you write often, then use them."Normaly more VBS, but it increases more and ...Have a nice day, Reinhard
MHz Posted August 27, 2005 Posted August 27, 2005 Allow me to express better. A FileWrite can create, open and close a file in one operation. This one line is working code. FileWrite(@ScriptDir & '\test.txt', 'this is a written line of text') But, if you are going to write to the same file, example, in a loop, then use FileOpen, as it creates the file and keeps the file open for writing, then once done with looping and writing, close it with FileClose. This is the quickest and recommended concept for constant writing operations to the same file.
ReFran Posted August 27, 2005 Posted August 27, 2005 Thanks for the detailed explanation. Will (try) to keep it in mind. Reinhard
MHz Posted August 27, 2005 Posted August 27, 2005 This is the reminder, it can look a little conflictive but look at the red sentence in the helpfile for FileWrite. The 1st line is the influence for FileOpen but the 2nd line states otherwise. This is perhaps what misled you and others?RemarksThe text file must be opened in write mode or the FileWrite command will fail.If a filename is given rather than a file handle, the file will be opened and closed during the function call. For parsing large text files this will be much slower than using filehandles. However, filename will be created if it does not already exist.
Valuater Posted August 27, 2005 Posted August 27, 2005 (edited) This is the reminder, it can look a little conflictive but look at the red sentence in the helpfile for FileWrite. The 1st line is the influence for FileOpen but the 2nd line states otherwise. This is perhaps what misled you and others?<{POST_SNAPBACK}>I actually placed file write in the bug section and was told it's ok by powers above, however i still think it is incorrectnice to see you here again... you helped me many times and i have not forgotten8) Edited August 27, 2005 by Valuater
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