How to get the path of a file object? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JackMa
Posts: 8
Joined: 28 Nov 2019, 01:02

How to get the path of a file object?

Post by JackMa » 25 May 2020, 07:08

I want to get the path of the file object. I have tried to search for "path" in the help file of the file object and search for relevant keywords on Google, but I did not find a satisfactory answer.

Does the file object have this method?

User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: How to get the path of a file object?

Post by boiler » 25 May 2020, 07:43

You would have needed the path to create the file object, correct? That would be the time where your script would know it and assign it to a variable if desired.

teadrinker
Posts: 4331
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get the path of a file object?  Topic is solved

Post by teadrinker » 25 May 2020, 11:04

This is possible like this:

Code: Select all

filePath := "D:\MyFile.txt"
file := FileOpen(filePath, "r")
myPath := GetFilePathFromFileObject(file)
file.Close()
MsgBox, % myPath

GetFilePathFromFileObject(fileObj) {
   chars := DllCall("GetFinalPathNameByHandle", "Ptr", fileObj.__Handle, "Ptr", 0, "UInt", 0, "UInt", 0)
   VarSetCapacity(filePath, chars << !!A_IsUnicode, 0)
   DllCall("GetFinalPathNameByHandle", "Ptr", fileObj.__Handle, "Str", filePath, "UInt", chars, "UInt", 0)
   Return RegExReplace(filePath, "^\\\\\?\\")
}

JackMa
Posts: 8
Joined: 28 Nov 2019, 01:02

Re: How to get the path of a file object?

Post by JackMa » 25 May 2020, 20:05

boiler wrote:
25 May 2020, 07:43
You would have needed the path to create the file object, correct? That would be the time where your script would know it and assign it to a variable if desired.
Do you mean to store variables in advance?

JackMa
Posts: 8
Joined: 28 Nov 2019, 01:02

Re: How to get the path of a file object?

Post by JackMa » 25 May 2020, 20:07

teadrinker wrote:
25 May 2020, 11:04
This is possible like this:

Code: Select all

filePath := "D:\MyFile.txt"
file := FileOpen(filePath, "r")
myPath := GetFilePathFromFileObject(file)
file.Close()
MsgBox, % myPath

GetFilePathFromFileObject(fileObj) {
   chars := DllCall("GetFinalPathNameByHandle", "Ptr", fileObj.__Handle, "Ptr", 0, "UInt", 0, "UInt", 0)
   VarSetCapacity(filePath, chars << !!A_IsUnicode, 0)
   DllCall("GetFinalPathNameByHandle", "Ptr", fileObj.__Handle, "Str", filePath, "UInt", chars, "UInt", 0)
   Return RegExReplace(filePath, "^\\\\\?\\")
}
Perfect solution to my problem, very good, thank you

User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: How to get the path of a file object?

Post by boiler » 25 May 2020, 20:27

JackMa wrote:
25 May 2020, 20:05
boiler wrote:
25 May 2020, 07:43
You would have needed the path to create the file object, correct? That would be the time where your script would know it and assign it to a variable if desired.
Do you mean to store variables in advance?
I mean when you create the file object, it’s with a known path, right? So you could just capture the path at that point by saving it to a variable. Not sure whether you consider that in advance or not.

JackMa
Posts: 8
Joined: 28 Nov 2019, 01:02

Re: How to get the path of a file object?

Post by JackMa » 25 May 2020, 20:50

boiler wrote:
25 May 2020, 20:27
JackMa wrote:
25 May 2020, 20:05
boiler wrote:
25 May 2020, 07:43
You would have needed the path to create the file object, correct? That would be the time where your script would know it and assign it to a variable if desired.
Do you mean to store variables in advance?
I mean when you create the file object, it’s with a known path, right? So you could just capture the path at that point by saving it to a variable. Not sure whether you consider that in advance or not.
I have considered that this operation is obvious. Next time I ask a question, I will write the known methods. :)

User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: How to get the path of a file object?

Post by boiler » 25 May 2020, 22:52

JackMa wrote:
25 May 2020, 20:48
I have considered that this operation is obvious. Next time I ask a question, I will write the known methods.
A lot of people post questions and have overlooked the most straightforward solutions, so it's worth mentioning. Since you're new here, I don't know what you may have considered.

mora145
Posts: 57
Joined: 25 Jun 2022, 15:31

Re: How to get the path of a file object?

Post by mora145 » 08 Feb 2023, 19:12

Code: Select all

file := "C:\TestFolder\test.txt"
Loop, %file%
    FilePath := A_LoopFileDir
    MsgBox, % FilePath
The text box should show "C:\testFolder".

I don't know if that's what you needed, but I'll leave it here, I was looking for a way to get the path without using regular expressions.

Post Reply

Return to “Ask for Help (v1)”