Get file path from fileexist with wild card

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Get file path from fileexist with wild card

09 Jun 2021, 15:44

My script needs to find a particular file.

I can do this:

thisfile := fileexist("c:\somefolder\myfile*.txt")
and this will return the "A" for the Archive attribute.

How can I get the actual file path of that file that I know exists?

What my dilemma is, is that I have two files in a particular directory, and I need to fetch the one with the highest version number in the file name.
file1.2.3.txt
file4.5.txt

The only thing I can think of to do is to loop through file*.txt and look for the first number, then trim, then look for the second number, etc. But I can't know how many numbers there will be in that name, and perhaps it's just file.txt with no numbers at all. If it has numbers, that will be what I want, but I'll take the one without numbers if that's all there is.

Maybe if I do the loop, perhaps the loop will automatically end on the file with the highest numbers in the filename since it would end up being the last file found?

Is there no other way?
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Get file path from fileexist with wild card

09 Jun 2021, 16:21

Code: Select all

Loop, Files, %A_ScriptDir%\t\file*.txt
{ RegExMatch(A_LoopFileName, "[\d.]+(?=\.)", thisVer)
  If norm(thisVer) >= norm(ver)
   ver := thisVer, latest := A_LoopFilePath
}
MsgBox, 64, Latest, %latest%

norm(ver) {
 For k, v in StrSplit(ver, ".")
  new .= "." Format("{:05}", v)
 Return new
}
This may need adjustment depending on how you view whether version "x.10" is less than "x.9".
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Get file path from fileexist with wild card

09 Jun 2021, 16:52

Well, I wasn't looking for someone to write the code for me, but thank you very much! It looks like it might work.

What if the file is named with . OR _ ?? - such as somefile5.7.2.txt AND somefile5_7_1.txt
I indeed, seem to have both cases.

Here I've tried to add that to the regex, but it still only finds the file with the . even though the file with _ has a higher number. The files with the _ in them get skipped as if 5_7 or 5_7_2 was just a 5 (guess the regex doesn't look past the _

Code: Select all

Loop, Files, %thisdir%\%thisfile%
{ RegExMatch(A_LoopFileName, "[\d.]+(?=[_|\.])", thisVer)
    thismatchVer := strreplace(thisVer,"_",".")
  If norm(thismatchVer) >= norm(ver)
   ver := thismatchVer, latest := A_LoopFilePath
}
MsgBox, 64, Latest, %latest%

norm(ver) {
 For k, v in StrSplit(ver, ".")
  new .= "." Format("{:05}", v)
 Return new
}
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Get file path from fileexist with wild card

09 Jun 2021, 17:10

The following seems to work.

Code: Select all

Loop, Files, %A_ScriptDir%\t\file*.txt
{ RegExMatch(A_LoopFileName, "[\d._]+(?=\.)", thisVer)
  If norm(thisVer) >= norm(ver)
   ver := thisVer, latest := A_LoopFilePath
}
MsgBox, 64, Latest, %latest%

norm(ver) {
 For k, v in StrSplit(StrReplace(ver, "_", "."), ".")
  new .= "." Format("{:05}", v)
 Return new
}
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Get file path from fileexist with wild card

09 Jun 2021, 17:19

Thank you again for your help. That works like a charm.

I figured I would have to use a file loop.
But I never would have thunk to use norm.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, Hansielein, Lpanatt and 324 guests