Finding Extension Of A Particular File

I know this has been covered elsewhere in the forums on a couple of occasions but there has to be a simpler way, doesn’t there, or am I just having a mental block.

I am writing an addon for a legacy database system, files are stored on a server, and a database record is created in a MSACCESS database.

Going forward my system actually stores the file extension of the file ‘copied’ to the server so its easy, the legacy system does not.

Each time a file is copied, it is renamed to the document record ID (autoincrement record number) in the MSACCESS database, also saved at the same time is a .DAT file, basically a text file with some information in it related to the file. So for every document there are two documents, example

155552.dat
155552.?

I need to find the ? to launch the file, basically if this was a shell command, dir 155552.*, ignore the .DAT file and what is left is the file needed, grab the extension, add it to the rest of the file and launch the file.

Each of these folders broken down by year and month contains hundreds of files so the below iteration across all files is slow, is there a way to only search the two files and ignore the .DAT file, thus making it much quicker.

It is not complete code as i will search the array of files to launch the right one.

Any suggestions or pointers as always appreciated.

[code]
//launch double clicked file
dim f1 as FolderItem=getfolderitem(lbDocument.rowtag(lbdocument.listindex))

if f1<>nil and f1.exists then
f1.Launch
else

//old documents may not have extension recorded so need aditional check
//ignore .dat file - what is left

Dim myFolder As FolderItem

myfolder = getfolderitem(storagepath)
dim filelist() as string

If myFolder Is Nil Then
Return
End If

Dim filecount As Integer = myFolder.Count
For i As Integer = 1 To filecount
Dim f As FolderItem = myFolder.Item(i)
If f <> Nil and uppercase(f.type)<>".DAT" Then
filelist.Append(f.Name)
End If
Next

dim res as integer
res=msgbox(“The document associated with this”+EndOfLine+“record cannot be found to launch.”,16,“Document Image Error”)

end if[/code]

dim v() as string
v=split(f.nativepath,".")
return v(v.ubound)

this ASSUMES that the last string following the last period is what you want

Thanks Dave, I can find the extension of a known file (although your way is quicker), what I want to do is reduce the number of files I have to search, i.e. make the native file path X:\Filestore\2010\10\15552.*, if you see what I mean, not read the whole directory of say 556 files and then within those find the two that begin with 155552 which is where the delay is.

Something like this? http://forums.realsoftware.com/viewtopic.php?p=253433&sid=2c2958ea9f32180a463a6828fbb8b6a9#p253433

so you want something like “DIR *.xyz” then
not sure that can be done inside of Xojo without parsing thru each filename
but you could use SHELL and actually use “DIR” or “LS” depending on OS

that might work, but Windows only, and may NOT work in 64bit

Thanks both, I had considered shell, and then parse the result, but trying to keep neat and tidy in XOJO, the API calls look interesting so will test that on 32/64 bit as well and see how I get on.

Get the file extension you want (Target Extension),
In a loop, check all files and keep (in an array) all files with the Target Extension
at the end of the loop, you have all files with the Target Extension.

It is not clear to me if you are searching for one file with the Target Extension or many.

Also, how can you ignore the name of the file extension of a file you generate.

Nota: I may not read correctly read your question.

BTW: the desing below can store all non .dat files:

In a loop, check all files and keep (in an array) all files without the “.dat” Extension
at the end of the loop, you have all files with the Target Extension (if there are only two kind of extensions in your target folder).

I am writing an addon for a legacy database system
Going forward my system actually stores the file extension of the file ‘copied’ to the server…
the legacy system does not.
You are in charge of your system _AND to the Legacy System as you wrote above.

So I tend to think at a blockqge in the thinking.

But, if my suspicions are correct, you will get two files with different file Extensions in a target folder. So, exclude “.dat” file extensions files and you will get all files with the other and “unknow” file Extension.
Worst: unless your unknow file extension(s) are created randomly, once you run the program and it save a couple (or more) files, you only have to read them to know the “name” of the unknow file Extension.

Is this helping you ?

[quote=353987:@Emile Schwarz]Get the file extension you want (Target Extension),
In a loop, check all files and keep (in an array) all files with the Target Extension
at the end of the loop, you have all files with the Target Extension.[/quote]
That is exactly what he said he wishes to avoid doing

The problem is I don’t know the extension, i know one file will be a dat file, I don’t know the extension of the other, the one I am interested in. I know the filename up to the ‘.’, but not beyond it. Iterating all the files is slow.

In windows shell dir [path]\15552.* will give me two files, I know I don’t want 15552.dat, but I do want the extension of the other to add to the 15552 I know, give it its extension in effect.

Iterating all the files is slow, but in the array of files I can find 15552 easily, and then as above ingore the .dat one, and get the full name including extension of the other.

So far working with the API calls the trailing ‘\’ is stripped so when I then search the path is incorrect, i may end up doing a shell/parse at this rate.

I am not sure if you’ve looked into it or not, but MBS plugins can access file lists faster than Xojo does (at least on Mac I recall this being true). Someone correct me if I’m misinformed!

I have MBS, will take a look - half the problem is there are so many functions its difficult to know where to start - thats not a complaint by the way :slight_smile:

Take a look at the FileListMBS and the examples at the bottom of the page (or if you installed the examples on your machines, look in the MSB Util/FileList folder).

Note that if using Windows, there is a Constructor variation where you can pass a filter string so you could filter to “15552.*” and instead of iterating over the entire folder contents you’d only have those files returned in the list. But even if you get the entire list, you’ll find this is significantly faster than FolderItems.

Since you mention MS Access databases, I’m guessing you only need to support Windows.

Sorry. (but it can be done ONCE; read below)

But the question about how he does not know the Extension name of a file he creates is still pertinent (IMHO). How can that be ?

Isn’t it possible to make a simple check in what is in the target folder ONCE, then the Extension name is know (excepted as I already wrote, the Extension name is created randomly…).

[quote=354023:@Emile Schwarz]But the question about how he does not know the Extension name of a file he creates is still pertinent (IMHO). How can that be ?

Isn’t it possible to make a simple check in what is in the target folder ONCE, then the Extension name is know (excepted as I already wrote, the Extension name is created randomly…).[/quote]

As I understand the OP, he is not creating the file. He is getting the root name (without extension) from a database, then has to find the file with that root name but unknown extension that is not .dat – but he does not know what it will be. So he is looking for how to find that file in the most efficient manner given the path and root name without extension.

To my knowledge, using FileListMBS( fPath, rootname + “.*” ) will be by far the fastest way of doing that for records in the “legacy database system”.

Douglas you are correct I know the extension name of the new files I create as I store the full filename in the database, but I have no control over the legacy ones. I settled on shell for the time being, and will optimise and tidy it up when I am less tired and also explore MBS as described to keep it ‘in XOJO’.

I settled with the below for the time being. Thanks everyone.

[code]//launch double clicked file
dim f1 as FolderItem=getfolderitem(lbDocument.rowtag(lbdocument.listindex))
if f1<>nil and f1.exists then
f1.Launch
else
//old documents may not have extension recorded so need aditional check
//ignore .dat file - what is left is the file
dim nme as String
nme=f1.name

Dim s As Shell
dim cmd as string
s = New Shell
s.Mode=2
//shell creates a directory listing using documentname.*
cmd=“dir “+f1.NativePath+”.* >”+SpecialFolder.Temporary.NativePath+“list.txt”
s.Execute(cmd)

//read the temporary file
dim f as FolderItem=GetFolderItem(SpecialFolder.Temporary.NativePath+“list.txt”)
dim textinput as TextInputStream
dim textrow, rw, ext, v() as string

if f<>nil then
textinput=TextInputStream.open(f)
do
rw=textinput.ReadLine
if instr(0,rw,nme)>0 and instr(0,uppercase(rw),".DAT")=0 then //must be a filename in this row and not a .dat file
textrow=rw
v=split(rw,".")
ext=v(v.ubound)
end if
loop until textinput.eof
textinput.close
end if
//rebuild the filename with the extension
f1=GetFolderItem(f1.NativePath+"."+ext)
if f1<>nil and f1.exists then
f1.Launch
else
dim res as integer
res=msgbox(“The document associated with this”+EndOfLine+“record cannot be found to launch.”,16,“Document Image Error”)
return
end if
//give up and do nothing
end if
[/code]