Know a file extension vector?

matthoffman Posted messages 404 Registration date   Status Member Last intervention   -  
 atchalak -
Hello,
I would like to know if there is a function (if there is one) that returns the extension (or the type) of a file.

If you don't know and you know a function that returns the version of Microsoft Office, that's just as well :).

Thanks in advance.
Configuration: Windows XP Firefox 3.0.9

15 answers

matthoffman Posted messages 404 Registration date   Status Member Last intervention   47
 
Yeah well thanks for your help :)
your method is 'porc' in the sense that you play with fire ;) but it seems the most suitable for my problem.
Provided I create a file .blablabla, it will mess up during processing because it will think the extension is "abla" instead of "blablabla" lol
1
matthoffman Posted messages 404 Registration date   Status Member Last intervention   47
 
up
0
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
How does the user give you the file path? via a dialog box or something else??

In which language?? vb, php, c++?
0
D4rkness
 
nana it’s in HTML :')
0
matthoffman Posted messages 404 Registration date   Status Member Last intervention   47
 
But the post title is VB at the same time, so the language is VB ;)
And I get the file path with the ActiveDocument.Path function .
In fact I’m writing a macro in an Excel (or Word) file and so there you go I retrieve the info from the open document.
0
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
so everything after the '.'..

ext=Right(ActiveWorkbook.FullName, 5)
if left(ext,1)<> "." then ext=Right(ActiveWorkbook.FullName, 4)

Tiny hiccup: if your Excel file is named "monfichier..xls" (2003), it will be treated as 2007, but otherwise it works
0
matthoffman Posted messages 404 Registration date   Status Member Last intervention   47
 
yes it’s also another way of looking at it but you have to be careful because if the extension is longer than 5 characters ....
Thanks for your answer :)
0
matthoffman Posted messages 404 Registration date   Status Member Last intervention   47
 
I just thought of a crude method to retrieve the extension.
We increment a counter until we find a "." in the filename starting from the right.
And the extension corresponds to the string on the right of length equal to the counter.

But as I just said, it's crude and way too long for what it is ^^.
0
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
lol.. well do you find my solution 'pig'??
0
MrSlave Posted messages 2657 Status Member 147
 
Otherwise there is this:
https://vb.developpez.com/faq/?page=Fichiers#ext_rep
--
Bloup! :)
0
cthulhu14
 
Come on... I'm nice
Private Function FileExtension(NomFichier As String) As String
Dim DotPos As Integer
If InStr(1, NomFichier, ".") = 0 Then
    FileExtension = False
Else
    While Left(Right(NomFichier, DotPos), 1) <> "."
        DotPos = DotPos + 1
    Wend
    FileExtension = Right(NomFichier, DotPos - 1)
End If
End Function
0
pijaku Posted messages 13513 Registration date   Status Moderator Last intervention   2 772
 
Hello, I’m taking advantage of the revival of this topic to propose a variant solution:
Dim Extension As String
Extension = IIf(Mid(StrReverse(ThisWorkbook.Name), 4, 1) = ".", Right(ThisWorkbook.Name, 3), Right(ThisWorkbook.Name, 4))
-- ???? Cordially, Franck ????
0
atchalak
 
 Dim myFileName As String, fileExtension As String fileExtension = Right(myFileName, InStrRev(myFileName, ".")) 


Instr returns the first occurrence of the character passed as an argument, InStrRev allows starting from the right; as soon as we reach the first ".", we have only the file extension on the right. This method also handles files that have one or more "." inside.
0
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
ah ben sinon, faut fare le contraire, et la y a pas d exeption :

ext=Right(ActiveWorkbook.FullName, 4)
if left(ext,1)<>"." then ext=Right(ActiveWorkbook.FullName, 5)

:) :p

j suis vraiment nul quand j m y met :)
-1
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
else, you can do something like:

ext=Right(ActiveWorkbook.FullName, 4) '2003
if left(ext,1)<> "." then
ext=Right(ActiveWorkbook.FullName, 5) '2007
if left(ext,1)<> "." then
ext=""
msgbox "extension error"
end if

good enough.. it's junk, but it handles all your exceptions :)
-1
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
oops, désolé :)

Right(ActiveWorkbook.FullName, 3)
-2
matthoffman Posted messages 404 Registration date   Status Member Last intervention   47
 
Hmmm if I’m not mistaken, your function will return the last 3 letters of the full name, which works very well for Office 2003 but not for 2007 because extensions have 4 letters (docx, xlsx, ...).
I would need a function that automatically retrieves the extension so that my macro can adapt to both 2003 and 2007 without any hassle and without having to modify the code if the version changes.
0
Yesman
 
All responses are bad and dirty.
The official method is with the Shell API (Microsoft code)
-3
jjsteing Posted messages 1613 Registration date   Status Contributor Last intervention   181
 
and so, a bit of code so you don’t die, you ignoramus ??
0
Kamil
 
Yes, that's clear. There's a topic, a question, so might as well answer to give a valid response...
I'm also interested in the solution with the shell.
0
Alex
 
System.IO.Path.GetExtension(fileName)
0