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?
Get file path from fileexist with wild card
Re: Get file path from fileexist with wild card
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
}
Re: Get file path from fileexist with wild card
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 _
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
}
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;
Re: Get file path from fileexist with wild card
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
}
Re: Get file path from fileexist with wild card
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.
I figured I would have to use a file loop.
But I never would have thunk to use norm.
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;