Sign in to follow this  
Followers 0
Gene

File Extension Udf Or Function

3 posts in this topic

So I wrote this one. I hope you'll gently point me to the existing one, or tell me how I can improve this one and what other error checking should be addressed.

;===============================================================================
;
; Description:    Returns the extension of the specified file.
; Syntax:          _GetFileExt($sFilePath)
; Parameter(s):  $sFilePath - Path and filename of the extension to be returned
; Requirement(s):   The Attrib() function
; Return Value(s):  On Success - Returns the file extension and sets @error to 1
;                  On Failure - Returns 0 and sets @error to one of 4 error codes
;                  -1 = "No file extension"
;                  -2 = "Invalid character in filename"
;                  -3 = "File is a directory"
;                  -4 = "File not found"
; Author(s):        Gene <[email="gtsears@yahoo.com"]gtsears@yahoo.com[/email]>
; Note(s):
;
;===============================================================================
Func _GetFileExt($sFilePath)
 Local $sValidFileChars, $sTestVar, $wrkVar, $sAttrib, $sExt
 $sValidFileChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_~\.:"
 $sTestVar = $sFilePath
 $wrkVar = " "
 $sAttrib = ""
 While $wrkVar <> "." And $sTestVar <> ""
  $wrkVar = StringRight($sTestVar, 1)
  If StringInStr($sValidFileChars, $wrkVar) Then
   If $wrkVar = "." Then
    If $sExt = "" Then
     $sAttrib = FileGetAttrib($sFilePath)
     If @error Then
      SetError(-4);"File not found"
      Return 0
     Else
      If StringInStr($sAttrib, "D") Then
       SetError(-3);"File is a directory"
       Return 0
      EndIf
     EndIf
     SetError(-1);"No file extension"
     Return 0
    Else
     SetError(1)
     Return $sExt;"Yes file extension"
    EndIf
   Else
    $sExt = $wrkVar & $sExt
    $sTestVar = StringTrimRight($sTestVar, 1)
   EndIf
  Else
   SetError(-2);"Invalid character in filename"
   Return 0
  EndIf
 WEnd
 SetError(-1);"No file extension"
 Return 0
EndFunc  ;==>_GetFileExt


;Example
;======================
#include <File.au3>
$FileExt = _GetFileExt("C:\autoexec.bat")
$iError = @error
Select
 Case $iError = 1
 MsgBox(0, "File Extension found", "File extension = " & $FileExt & "  " & $iError, 5)
 Case $iError = -1
 MsgBox(0, "No File Extension found", "Error code = " & $iError, 5)
 Case $iError = -2
 MsgBox(0, "Invalid character in filename", "Error code = " & $iError, 5)
 Case $iError = -3
 MsgBox(0, "File is a directory", "Error code = " & $iError, 5)
 Case $iError = -4
 MsgBox(0, "File not found", "Error code = " & $iError, 5)
EndSelect
Exit

Gene


[font="Verdana"]Thanks for the response.Gene[/font]Yes, I know the punctuation is not right...

Share this post


Link to post
Share on other sites



Hi,

In beta?,

_PathSplit

--------------------------------------------------------------------------------

Splits a path into the drive, directory, file name and file extension parts. An empty string is set if a part is missing.

#include <File.au3>

_PathSplit($szPath, ByRef $szDrive, ByRef $szDir, ByRef $szFName, ByRef $szExt)

Best, randall

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi,

In beta?,

Best, randall

No, I didn't remember that one, but I don't think I would have used it anyway, I was trying to remain independent. I could use it though.

Thanks.

Gene

Edit: Here it is with _PathSplit and error checking.

;===============================================================================
;
; Description:    Returns the extension of the specified file.
; Syntax:          _GetFileExt($sFilePath)
; Parameter(s):  $sFilePath - Path and filename of the extension to be returned
; Requirement(s):   _PathSplit() and the Attrib() function
; Return Value(s):  On Success - Returns the file extension and sets @error to 1
;                  On Failure - Returns 0 and sets @error to one of 4 error codes
;                  -1 = "No file extension"
;                  -2 = "Invalid character in filename"
;                  -3 = "File is a directory"
;                  -4 = "File not found"
;                  -5 = "Could not get Attribute
; Author(s):        Gene <[email="gtsears@yahoo.com"]gtsears@yahoo.com[/email]>
; Note(s):
;
;===============================================================================

Func _GetFileExt($sFilePath)
 Local $sValidFileChars, $sTestVar, $wrkVar, $sAttrib, $sExt, $szDrive, $szDir, $szFName, $szExt
 $sValidFileChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_~\.:"
 $sTestVar = $sFilePath
 $wrkVar = " "
 $sAttrib = ""
 While $sTestVar <> ""
  $wrkVar = StringRight($sTestVar, 1)
  If StringInStr($sValidFileChars, $wrkVar) Then
   $sTestVar = StringTrimRight($sTestVar, 1)
  Else
   SetError(-2);"Invalid character in filename"
   Return 0
  EndIf
 WEnd
 If FileExists($sFilePath) Then
  $sAttrib = FileGetAttrib($sFilePath)
  If @error Then
   SetError(-5);"Could not get Attribute"
   Return 0
  Else
   If StringInStr($sAttrib, "D") Then
    SetError(-3);"File is a directory"
    Return 0
   EndIf
  EndIf 
 Else
  SetError(-4);"File not found"
  Return 0
 EndIf
 $aPath = _PathSplit($sFilePath, $szDrive, $szDir, $szFName, $szExt)
 If $aPath[4] = "" Then
  SetError(-1)
  Return 0
 Else 
  SetError(1)
  Return $aPath[4]
 EndIf 
EndFunc  ;==>_GetFileExt


;Example
;======================
#include <File.au3>
$FileExt = _GetFileExt("C:\autoexec.bat")
$iError = @error
Select
 Case $iError = 1
 MsgBox(0, "File Extension found", "File extension = " & $FileExt & "  " & $iError, 5)
 Case $iError = -1
 MsgBox(0, "No File Extension found", "Error code = " & $iError, 5)
 Case $iError = -2
 MsgBox(0, "Invalid character in filename", "Error code = " & $iError, 5)
 Case $iError = -3
 MsgBox(0, "File is a directory", "Error code = " & $iError, 5)
 Case $iError = -4
 MsgBox(0, "File not found", "Error code = " & $iError, 5)
 Case $iError = -5
 MsgBox(0, "Could not get attribute", "Error code = " & $iError, 5)
EndSelect
Exit
Edited by Gene

[font="Verdana"]Thanks for the response.Gene[/font]Yes, I know the punctuation is not right...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0