FileSelectFile - Ignore file in use

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
2_05
Posts: 31
Joined: 09 May 2018, 10:28

FileSelectFile - Ignore file in use

31 Jul 2023, 04:23

I have a script where the user needs to provide a path name of a file.
To simplify inserting the path name, I added a FileSelectFile

Code: Select all

FileSelectFile, FileName, 1
This works as long as the file is not in use. When the file is in use, the dialog cannot be closed and the message "This file is in use." appears.
I want to ignore File in use as I only need the path of the file, I'm not actually editing the file.
Is there an option for FileSelectFile to ignore file in use? So the user can provide the path of the file without actually having to type it out or copy the path from somewhere.
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: FileSelectFile - Ignore file in use

31 Jul 2023, 04:58

Hello,

I did not experience this. I ran the following script and selected an open Microsoft Word document. The script completed without errors.

Code: Select all

#Requires AutoHotkey v1.1.33 
FileSelectFile filePath, % REQUIREFILE := 1
MsgBox 64, Selected, % filePath
Do you have a way to help the forum reader replicate the problem that you are experiencing?
2_05
Posts: 31
Joined: 09 May 2018, 10:28

Re: FileSelectFile - Ignore file in use

03 Aug 2023, 17:18

This script demonstrates the issue. It will not allow you to obtain the path of Test.txt via the file dialog.

Code: Select all

f := FileOpen("Test.txt", "a-") ; Simulates locking of the file. In my case an external program locks the file so this line cannot be altered and the variable f is not accessible.
FileSelectFile, filepath, 1
MsgBox, %filepath%
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: FileSelectFile - Ignore file in use

03 Aug 2023, 18:21

To the best of my knowledge, if you lock the file for read access, then it's doing what you said-- prevent reading by others.

Most other programs do not lock for reading, just for writing-- which also works with FileOpen in AHK.
2_05
Posts: 31
Joined: 09 May 2018, 10:28

Re: FileSelectFile - Ignore file in use

04 Aug 2023, 06:17

In my situation another program does lock the file for reading.
I have no interest in reading the file via ahk though, I only want to obtain the file path.
I use FileSelectFile because it is a more user friendly way to provide the path, but this method fails when another program has locked the file.

Besides FileSelectFile, is there another way to let the user provide the path, other than him having to write or copy-paste the path from somewhere else?
toralf
Posts: 868
Joined: 27 Apr 2014, 21:08
Location: Germany

Re: FileSelectFile - Ignore file in use

04 Aug 2023, 06:22

You could try it with darg & drop on a Gui or a script icon
Or via explorer context menu "send to" to a script.
ciao
toralf
User avatar
mikeyww
Posts: 27150
Joined: 09 Sep 2014, 18:38

Re: FileSelectFile - Ignore file in use

04 Aug 2023, 07:42

Code: Select all

#Requires AutoHotkey v1.1.33
f := FileOpen("Test.txt", "a-")

F2::
FileSelectFile filepath, 1
MsgBox % filepath
Return

F3::
on := True
Run % A_ScriptDir
Return

#If on && WinActive("ahk_class CabinetWClass")
Enter::
sel := getSelected()
WinClose
on := False
For each, item in sel
 MsgBox % item
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww, RussF and 116 guests