Diana (Cda) Posted October 14, 2010 Share Posted October 14, 2010 2 parts of this script; the part that opens the temp file works just great. But the part that should close that temp file if it's open doesn't work. A new file just keeps getting created so that after a while, there are a ton of versions of that file open. What is wrong with the code below pls?;================================================================================================== $File2Read = @DesktopDir & "\TestClose.txt" $TempFILE = @TempDir & "\TmpFile.txt" ;================================================================================================== ;------------------------------------------------------------------------------------------------------ If WinExists($TempFILE) THEN WinClose($TempFILE) If FileExists($TempFILE) THEN FileDelete($TempFILE) ;------------------------------------------------------------------------------------------------------ Sleep(250) $FILE = FileRead($File2Read) FileWrite($TempFILE, $FILE) Sleep(250) If FileExists($TempFILE) Then ShellExecute($Metapad, $TempFILE, "", "Open", @SW_MAXIMIZE) Else ShellExecute($MetapadWork, $TempFILE, "", "Open", @SW_MAXIMIZE) EndIf The temporary file is a template that gets different text dumped into it. So the next time the script is launched, a whole brand new file copy should be opened. This happens, but the used file doesn't get closed and I'm wondering what is wrong in the WinClose part. I'm guessing that there's maybe something missing there that I don't know. Thanks! Link to comment Share on other sites More sharing options...
water Posted October 14, 2010 Share Posted October 14, 2010 2 parts of this script; the part that opens the temp file works just great. But the part that should close that temp file if it's open doesn't work.WinClose closes a window. Shouldn't you use FileClose to close a file? My UDFs and Tutorials: Spoiler UDFs:Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsOutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - DownloadOutlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - WikiPowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - WikiTask Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs:Excel - Example Scripts - WikiWord - Wiki Tutorials:ADO - WikiWebDriver - Wiki Link to comment Share on other sites More sharing options...
Diana (Cda) Posted October 14, 2010 Author Share Posted October 14, 2010 Yes, sorry about that. I didn't have the file that I created yesterday (it's on my flash drive) so I did one up again quickly to try again this morning with only I forgot to put FileClose the second time instead of WinClose. My mistake. But regardless, the same problem happened with this script as yesterday. The file doesn't get closed even though I'm using FileClose the second time. I get a new copy opened each time so my taskbar gets cluttered up pretty quickly. What is missing here, anyone know? Link to comment Share on other sites More sharing options...
trung0407 Posted October 14, 2010 Share Posted October 14, 2010 If you don't have a FileOpen, then you don't need a FileClose Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 14, 2010 Moderators Share Posted October 14, 2010 Diana (Cda),Why even check if the temp file is there? Just use FileDelete($TempFILE) before the FileWrite($TempFILE, $FILE), which you only do if some data was read. That way you either have a temp file with the read data or no file at all. Then your If FileExists($TempFile) structure determines what to do next:;================================================================================================== $File2Read = @DesktopDir & "\TestClose.txt" $TempFILE = @TempDir & "\TmpFile.txt" ;================================================================================================== FileDelete($TempFILE) ; why check if it exists - just delete it if it is there $FILE = FileRead($File2Read) If Not @error Then FileWrite($TempFILE, $FILE) ; fill the temp file with the data if there is any ; We now have a temp file if there was data read and no temp file if data was not read Sleep(250) ; Now check if the temp file exists If FileExists($TempFILE) Then ShellExecute($Metapad, $TempFILE, "", "Open", @SW_MAXIMIZE) Else ShellExecute($MetapadWork, $TempFILE, "", "Open", @SW_MAXIMIZE) EndIfNo FileOpen or FileClose required. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Diana (Cda) Posted October 14, 2010 Author Share Posted October 14, 2010 (edited) No FileOpen or FileClose required. M23Hi, there! Thanks for the more elegant code. I just use snippets of stuff that I know work and re-use them; I'm by no means a coder at all <g>.However, the original problem still remains. I need to open up a new instance of the file, yes, BUT it must always close the previous one. I'm guessing it's because I don't know how to do this.When I use your more elegant code, a new file is opened but this still isn't dealing with closing the old instance down. So if I continue to click, new text files get opened each time. That's what I'm trying to avoid. In essence, it's the reason for the script. I click on the script - the old, used template (the temp file) should be closed and a fresh file opened. This is until the weekend when I can create a GUI to do this much better where I dumpt the text in then after pushing "send to clipboard", everything gets sent to the clipboard (hopefully <g>). In the meantime, for next 2 days, I'm dumping in text manually to a template so that at least I'm not doing this all completely typed up each time.So, once the temp file is open, with my original template untouched on the desktop, how can this temp file be closed _without_ saving since I've entered text into it before copying the whole thing and pasting into a database? Obviously WinClose (used by accident) and FileClose aren't the means to do this.Thanks much! Edited October 14, 2010 by Diana (Cda) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 14, 2010 Moderators Share Posted October 14, 2010 Diana (Cda),So if I continue to click, new text files get opened each timeHang on, Windows does not allow multiple files with the same name - so how can you have multiple @TempDir & "\TmpFile.txt" files at the same time? What exactly do you have multiplying in your taskbar when you continue to click? Is it perhaps multiple instances of your $Metapad app? Because that is a whole different problem. Perhaps if you let us see a bit more of the script rather than just the few lines above. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Diana (Cda) Posted October 14, 2010 Author Share Posted October 14, 2010 (edited) Diana (Cda), Hang on, Windows does not allow multiple files with the same name - so how can you have multiple @TempDir & "\TmpFile.txt" files at the same time? What exactly do you have multiplying in your taskbar when you continue to click? Is it perhaps multiple instances of your $Metapad app? Because that is a whole different problem. Perhaps if you let us see a bit more of the script rather than just the few lines above. M23 <vbg> Toldya's ... I knew it'd be about something I was clueless about ... Okay, here's the entire script, not much different except this is "real" one vs. one I re-created from memory this morning. I imagine it doesn't matter that this temp file has an .AU3 extension vs. .TXT since metapad treats the text identically (??). ; ; AutoIt ; #include <_PartitionLetters.au3> ; this replaces drive letters #include<_WEReplacement.au3> ; references a Windows Explorer replacement program AutoItSetOption("WinTitleMatchMode", 2) ; this allows partial window titles to be valid! ;================================================================================================== $File2Read = @DesktopDir & "\Script\HTML sample, original (do not delete).txt" $TempFILE = @TempDir & "\FileInfodump.au3" ;================================================================================================== ;------------------------------------------------------------------------------------------------------ If WinExists($TempFILE) THEN FileOpen($TempFILE) If FileExists($TempFILE) THEN FileDelete($TempFILE) ;------------------------------------------------------------------------------------------------------ $FILE = FileRead($File2Read) If Not @error Then FileWrite($TempFILE, $FILE) ; fill the temp file with the data if there is any ; We now have a temp file if there was data read and no temp file if data was not read Sleep(250) ; Now check if the temp file exists If FileExists($TempFILE) Then ShellExecute($MetapadWork, $TempFILE, "", "Open", @SW_MAXIMIZE) Else ShellExecute($Metapad, $TempFILE, "", "Open", @SW_MAXIMIZE) EndIfOkay, let me also see exactly the name/title of what gets opened ... Okay-doke, the filepath of the repeatedly opened file is this: C:\Documents and Settings\Administrator\Local Settings\Temp\FileInfodump.au3 But even though my re-creation this morning wasn't named exactly the same, the same situation occurred. You could be very right, it might be a metapad issue. I love metapad because it supports URLs and has a nicer interface for me than notepad. Do you think it's a metapad thing going on here? What approach is needed to close the file, then, before opening a new one? Thx. (p.s., left the check "If file exists then delete ..." in since I tend to know this syntax and re-use it and when I go to search for examples, they'll always be the same. Bear with me; I tend to be repetitive in coding since I'm not an expert and, if it works, even if not quite elegant, I'll stick with what I know. Thx <g>) Edited October 14, 2010 by Diana (Cda) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 14, 2010 Moderators Share Posted October 14, 2010 Diana (Cda),It sounds very much as though you are are opening multiple intances of MetaPad - an editor can usually open many instances of the same file, although saving then becomes interesting! Do you actually need multiple instances of Metapad with the same file open at any one time? If not, then I would suggest using WinExist to check if there is already an instance active and so prevent multiple instances of the editor opening the same file from occuring.Let me know if any of this makes sense - or not! M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Diana (Cda) Posted October 15, 2010 Author Share Posted October 15, 2010 (edited) Let me know if any of this makes sense - or not! M23Yes, that makes supreme sense and I never would have thought of that as being the problem. <g>Now, I can use WinExist, sure, but can it be tied in with this specific file only? I might have other text files open in Metapad that shouldn't be closed so this script couldn't be an indiscriminate closing of Metapad. But I don't know how that would be managed. I already used WinExist but FileClose and WinClose didn't work because of this particular situation. How can we use WinExist to see if that particular file is open, but to close the Metapad instance related to that open file only?Thanks. Edited October 15, 2010 by Diana (Cda) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 15, 2010 Moderators Share Posted October 15, 2010 Diana (Cda),From what I can see in the Metapad screenshots, the title of the open file is included in the Metapad window title as is the case with many editors. So just look for that particular title with WinExists and use WinClose it if it does: If WinExists("TmpFile.txt - metapad") Then WinClose("TmpFile.txt - metapad")M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
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