How to extract file extensions?

W

What-A-Tool

DK said:
I have a directory listing for all the files in a particular drive.
With excel macros, I have extracted filenames with extension in column
E. like filename.xxx

i need to extract the extensions in Column F. Now, column E does not
have all files with extensions.

Extension should be extracted with a condition of having a dot and 3
letter extension.

Can someone please help me in writing a small code?

Put this together for use in an Access database I worked on a while back -
not claiming its the best way - some of the more knowledgeable people around
here may be able to point out a better method - but its always worked for
me.
Good luck - Sean

Private Function GetExtension(ByVal strPath As String) As String
'Get file extension from file name
Dim intPtLoc As Integer
intPtLoc = InStrRev(strPath, ".")
GetExtension = UCase(Right(strPath, (Len(strPath) - (intPtLoc))))
End Function
 
W

What-A-Tool

What-A-Tool said:
Put this together for use in an Access database I worked on a while back -
not claiming its the best way - some of the more knowledgeable people
around here may be able to point out a better method - but its always
worked for me.
Good luck - Sean

Private Function GetExtension(ByVal strPath As String) As String
'Get file extension from file name
Dim intPtLoc As Integer
intPtLoc = InStrRev(strPath, ".")
GetExtension = UCase(Right(strPath, (Len(strPath) - (intPtLoc))))
End Function

Sorry - saw Dave Petersons post after I sent mine, and realized I answered a
different question than u asked.
 
D

DK

I have a directory listing for all the files in a particular drive.
With excel macros, I have extracted filenames with extension in column
E. like filename.xxx

i need to extract the extensions in Column F. Now, column E does not
have all files with extensions.

Extension should be extracted with a condition of having a dot and 3
letter extension.

Can someone please help me in writing a small code?
 
D

Dave Peterson

You could use a formula in column G:
=if(mid(f1,len(f1)-3,1)=".",right(f1,3),"")
to show xxx.

=if(mid(f1,len(f1)-3,1)=".",right(f1,4),"")
to show .xxx

In code:

Option Explicit
Sub testme()

Dim myCell As Range
Dim myRng As Range

With Worksheets("sheet1")
Set myRng = .Range("F1", .Cells(.Rows.Count, "F").End(xlUp))
End With

For Each myCell In myRng.Cells
If Mid(myCell.Value, Len(myCell.Value) - 3, 1) = "." Then
myCell.Offset(0, 1).Value = Right(myCell.Value, 3)
'should column F have the extension dropped?
'myCell.Value = Left(myCell.Value, Len(myCell.Value) - 4)
Else
myCell.Offset(0, 1).Value = ""
End If
Next myCell
End Sub
 
D

DK

Thank you so much Dave!
I will test this out!

Dave said:
You could use a formula in column G:
=if(mid(f1,len(f1)-3,1)=".",right(f1,3),"")
to show xxx.

=if(mid(f1,len(f1)-3,1)=".",right(f1,4),"")
to show .xxx

In code:

Option Explicit
Sub testme()

Dim myCell As Range
Dim myRng As Range

With Worksheets("sheet1")
Set myRng = .Range("F1", .Cells(.Rows.Count, "F").End(xlUp))
End With

For Each myCell In myRng.Cells
If Mid(myCell.Value, Len(myCell.Value) - 3, 1) = "." Then
myCell.Offset(0, 1).Value = Right(myCell.Value, 3)
'should column F have the extension dropped?
'myCell.Value = Left(myCell.Value, Len(myCell.Value) - 4)
Else
myCell.Offset(0, 1).Value = ""
End If
Next myCell
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top