Script not waiting until file is closed to delete file Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Script not waiting until file is closed to delete file

Post by arcylix » 08 Feb 2023, 12:07

I have the shortcut below in my "Work" ahk script. As indicated by the script, it should follow these steps:

1. Activate the Excel document indicated by the stored variable excelPID (it does).
2. Make sure the "Tracker" sheet is selected before performing any actions (it does).
3. Sends Ctrl+Shift+q to execute a macro in Excel (it does).

Here is where it stops behaving the way that I think it should.

4. Loop through a specific directory (correctly identified), looking only for xml files.
5. As long as the file is open, it should remain in the loop.
6. Once the file has been indicated as closed, it should delete that file.

The problem is, it does loop through the directory, but... it deletes all files in the directory despite the xml file identifier. Because of this, I think that it breaks the loop (since other files in that directory are not open) and attempts to delete them all. This is not the intended behavior. The reason I have it looping through the directory rather than looking for an actual file name is because the filename is dynamic, though I am not sure what the naming convention is. The format of the filename itself is 12345_678_OfficeAppointments_MyName_901234.xml.

What am I missing? I appreciate any help, as usual.

Code: Select all

^+n::
{

   WinActivate("ahk_pid " excelPID)
   Xl.Sheets("Tracker").Select

   SendInput "^+q"

   Loop Files medentDir "*.xml"
   {
      fileName := StrSplit(A_LoopFileShortName, ".")[1]
      while (WinExist("ahk_Title " fileName)) {
         Sleep 1000
      }

      FileDelete A_LoopFileFullPath
      fileName := ""
      break
   }
}

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Script not waiting until file is closed to delete file  Topic is solved

Post by mikeyww » 08 Feb 2023, 14:34

Did you find ahk_title in a script somewhere, or is it your own invention?
I think that it breaks the loop
You're uncertain what your script does. Are there some lines that you can add to improve your understanding? MsgBox, confirmation of some kind, logging? AHK also provides ListLines. I suggest that there is no reason to remain uncertain. You can find out what the script is doing.

To wait for a window to close: WinWaitClose. Note that the script will not proceed until the window closes, or the timeout occurs.

Explained: WinTitle

arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Re: Script not waiting until file is closed to delete file

Post by arcylix » 08 Feb 2023, 14:58

@mikeyww

I'm accepting your reply as the answer because in it, you incidentally helped me solve the problem. I had to make a couple changes, but now it works, mainly due to the WinWaitClose suggestion. I also realized I can use more than just the *.xml to narrow down the files. Thanks!

Code: Select all

^+n::
{

   WinActivate("Tracker.xlsb - Excel")
   Xl.Sheets("Tracker").Select

   SendInput "^+q"

   Loop Files medentDir "*OfficeAppointments*.xml"
   {
      tempName := StrSplit(A_LoopFileShortName, ".")[1]
      tempPath := A_LoopFileFullPath
   }

   WinWaitClose(tempName)

   FileDelete tempPath
   tempName := ""
   tempPath := ""
}

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Script not waiting until file is closed to delete file

Post by mikeyww » 08 Feb 2023, 15:17

OK. SplitPath may be more reliable than StrSplit for file paths & file names.

arcylix
Posts: 54
Joined: 27 Sep 2022, 14:43

Re: Script not waiting until file is closed to delete file

Post by arcylix » 08 Feb 2023, 21:53

@mikeyww

Yet another useful function I had no idea existed... That just makes life so much easier at this point. Thanks again!

Post Reply

Return to “Ask for Help (v2)”