How to get the path of a file object? Topic is solved
How to get the path of a file object?
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?
Does the file object have this method?
Re: How to get the path of a file object?
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.
-
- Posts: 4331
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: How to get the path of a file object? Topic is solved
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, "^\\\\\?\\")
}
Re: How to get the path of a file object?
Perfect solution to my problem, very good, thank youteadrinker wrote: ↑25 May 2020, 11:04This 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, "^\\\\\?\\") }
Re: How to get the path of a file object?
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.
Re: How to get the path of a file object?
I have considered that this operation is obvious. Next time I ask a question, I will write the known methods.
Re: How to get the path of a file object?
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.
Re: How to get the path of a file object?
Code: Select all
file := "C:\TestFolder\test.txt"
Loop, %file%
FilePath := A_LoopFileDir
MsgBox, % FilePath
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.