Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WSH - VBscript - Length function

Status
Not open for further replies.

alfie002

Technical User
Mar 3, 2004
121
GB
Dear all,

I want to return the file extension of a give file. I am having a problem with it

Sample code as follows;

extension = lcase(substr(file_name,(len(file_name-3)),(len(file_name)) ))

This syntax should work, but I cannot remember if I should be using the "len" or "length" function !!!

Any reminder much appreciated.

Thanks

Alf
 
extension = LCase(Right(file_name,3))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Len() not Length().

Keep in mind that only a subset of file extensions are 3 characters though.
 
Dear PHV and dilettante,

thank you both for your contributions

Much appreciated.

Alf
 
Another way that avoids the problem dilettante brings up (and allows for extensions with less than, or more than 3 characters) is to use the GetExtensionName method of the FileSystemObject, as long as you know the full path to the file.

Code:
Dim sMyFile
sMyFile = "C:\Pathtofile\Filename.ext"
wscript.echo GetAnExtension(sMyFile)

Function GetAnExtension(DriveSpec)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   GetAnExtension = fso.GetExtensionName(Drivespec)
End Function
 
OK, without FSO:
extension = LCase(Mid(file_name,InStrRev(file_name,".")+1))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top