Jump to content

Recommended Posts

Posted

Hi guys! I have a problem with my script. It should overwrite a file if it already exists... but it doesn't :whistle: . Can anyone tell me what's wrong with it?

#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 :dance: - Scriptkiddie

[-"Scriptkiddie, nice to meet you!"-]

Posted

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
Posted

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

Posted

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

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

Posted

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

<{POST_SNAPBACK}>

No you don't. You can do a direct filewrite without using fileopen. Try it.
Posted

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

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

Posted

If you do not use fileopen, then fileclose is not required. :whistle:

Filewrite can work without them. If you write often, then use them.

Posted

If you do not use fileopen, then fileclose is not required.  :whistle:

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

Posted

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.

Posted

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?

Remarks

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

Posted (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 incorrect

nice to see you here again... you helped me many times and i have not forgotten

8)

Edited by Valuater

NEWHeader1.png

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...