Jump to content

Script to write file na,me into txt document


jjump
 Share

Recommended Posts

Hi,

I am trying to make a script that will write the name of a file that is dropped into the input box.. This is what I have so far..

#include <file.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>


$Form1_1 = GUICreate("Write file name", 436, 230, 191, 117, Default, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x0A3B76)
Global $Input1 = GUICtrlCreateInput("", 20, 100, 360, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)



_FileCreate("temp.txt")
$txt = FileOpen("temp.txt", 1)


; Check if file opened for writing OK
If $txt = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_DROPPED
            $File = @GUI_DragFile
            Switch @GUI_DropId
                Case $Input1
                    FileWrite($txt, "$File" & @CRLF)
              EndSwitch
    EndSwitch
WEnd

This does not work, can anyone tell me why?

Thanks!

Ian

Link to comment
Share on other sites

@GUI_DropId only works if you're in OnEvent mode:

(Note: Because you're using FileOpen, you won't see anything added to the txt file until you close the window which will call FileClose)

#include <file.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt ("GUIOnEventMode", 1)

$Form1_1 = GUICreate("Write file name", 436, 230, 191, 117, Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "_GUI_EVENT_CLOSE")
GUISetOnEvent($GUI_EVENT_DROPPED, "_GUI_EVENT_DROPPED")
GUISetBkColor(0x0A3B76)
Global $Input1 = GUICtrlCreateInput("", 20, 100, 360, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

_FileCreate("test.txt")
$txt = FileOpen("test.txt", 1)


; Check if file opened for writing OK
If $txt = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    Sleep (100)
WEnd

Func _GUI_EVENT_CLOSE()
    FileClose ($txt)
    Exit
EndFunc

Func _GUI_EVENT_DROPPED()
    $File = @GUI_DragFile
    Switch @GUI_DropId
        Case $Input1
            FileWrite($txt, $File & @CRLF)
    EndSwitch
EndFunc
Link to comment
Share on other sites

Hi,

Thank you so much for your reply! I tried that but it does not seem to work.. I also want it to write to the file without needing to close the gui.. I thought it would be like this:

#include <file.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt ("GUIOnEventMode", 1)

$Form1_1 = GUICreate("Write file name", 436, 230, 191, 117, Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "_GUI_EVENT_CLOSE")
GUISetOnEvent($GUI_EVENT_DROPPED, "_GUI_EVENT_DROPPED")
GUISetBkColor(0x0A3B76)
Global $Input1 = GUICtrlCreateInput("", 20, 100, 360, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

_FileCreate("test.txt")
$txt = FileOpen("test.txt", 1)


; Check if file opened for writing OK
If $txt = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf


While 1
    Sleep (100)
WEnd

Func _GUI_EVENT_CLOSE()
    Exit
EndFunc

Func _GUI_EVENT_DROPPED()
    $File = @GUI_DragFile
    Switch @GUI_DropId
        Case $Input1
            FileWrite($txt, $File & @CRLF)
                FileClose ($txt)
    EndSwitch
EndFunc

But that does not seem to work.. Any ideas?

Ian

Link to comment
Share on other sites

How about this?

#include <file.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt ("GUIOnEventMode", 1)

$Form1_1 = GUICreate("Write file name", 436, 230, 191, 117, Default, $WS_EX_ACCEPTFILES)
GUISetOnEvent($GUI_EVENT_CLOSE, "_GUI_EVENT_CLOSE")
GUISetOnEvent($GUI_EVENT_DROPPED, "_GUI_EVENT_DROPPED")
GUISetBkColor(0x0A3B76)
Global $Input1 = GUICtrlCreateInput("", 20, 100, 360, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

While 1
    Sleep (100)
WEnd

Func _GUI_EVENT_CLOSE()
    Exit
EndFunc

Func _GUI_EVENT_DROPPED()
    $File = @GUI_DragFile
    Switch @GUI_DropId
        Case $Input1
            FileWriteLine("test.txt", $File)
    EndSwitch
EndFunc
Edited by exodius
Link to comment
Share on other sites

  • Moderators

Hi,

Just to say that exodius' script in Post #4 is working fine for me.

M23

Edit:

@GUI_DropId only works if you're in OnEvent mode

I hate to disagree but the code works in MessageLoop mode too without problem: :)

#include <file.au3>
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1_1 = GUICreate("Write file name", 436, 230, 191, 117, Default, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x0A3B76)
Global $Input1 = GUICtrlCreateInput("", 20, 100, 360, 20)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)
GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_DROPPED
            _GUI_EVENT_DROPPED()
    EndSwitch
WEnd

Func _GUI_EVENT_DROPPED()
    $File = @GUI_DragFile
    Switch @GUI_DropId
        Case $Input1
            FileWriteLine("test.txt", $File)
    EndSwitch
EndFunc
Edited by Melba23

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

I hate to disagree but the code works in MessageLoop mode too without problem: ;)

Fair enough, I misread the line "OnEvent functions are only called when the option GUIOnEventMode is set to 1 - when in this mode GUIGetMsg is NOT used at all." from GUISetOnEvent :)

Link to comment
Share on other sites

Thanks for all your replies! Moved the script and ran it again, now working fine!! Ok, so, it now writes the whole file name including the directory etc.. Is there anyway of just writing the files name?

Thanks!

Ian

Link to comment
Share on other sites

  • Moderators

jjump,

Just do this: :)

Func _GUI_EVENT_DROPPED()
    $File = @GUI_DragFile
    Switch @GUI_DropId
        Case $Input1
            FileWriteLine("test.txt", StringRegExpReplace($File, "^.*\\", ""))  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    EndSwitch
EndFunc

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

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