Jump to content
Borje

Need little help how to locate the file exstension ?

Recommended Posts

Borje

Hello anybody!

I want to ask if anybody user here that can help me little and can give me some tips how to find a file extension

Example if I have a file:  TESTING.EXE how to find out the exe or .exe for that file what code i must have to find that.

 

Share this post


Link to post
Share on other sites
VIP

Use _PathSplit() !
Or my function:
 

Func _SplitPath($sFilePath, $sType = 0)
    Local $sDrive, $sDir, $sFileName, $sExtension, $sReturn
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", 1)
    If @error Then
        ReDim $aArray[5]
        $aArray[0] = $sFilePath
    EndIf
    $sDrive = $aArray[1]
    If StringLeft($aArray[2], 1) == "/" Then
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
    Else
        $sDir = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
    EndIf
    $aArray[2] = $sDir
    $sFileName = $aArray[3]
    $sExtension = $aArray[4]
    If $sType = 1 Then Return $sDrive
    If $sType = 2 Then Return $sDir
    If $sType = 3 Then Return $sFileName
    If $sType = 4 Then Return $sExtension
    If $sType = 5 Then Return $sFileName & $sExtension
    If $sType = 6 Then Return $sDrive & $sDir
    If $sType = 7 Then Return $sDrive & $sDir & $sFileName
    Return $aArray
EndFunc   ;==>_SplitPath

 


Regards,
 

Share this post


Link to post
Share on other sites
Borje

Thanks Trong for this example but I can not have that to work can you explain how you insert a file to this, example: file  TEST.EXE and then

pick upp $sExtension

 

 

Share this post


Link to post
Share on other sites
Danyfirex

Hello. Just for fun

MsgBox(0, "", _GetExtension("test.test.exe"))
MsgBox(0, "", _GetExtension("test.xml"))
MsgBox(0, "", _GetExtension("test"))
MsgBox(0, "", _GetExtension("test.name.test.temp"))

Func _GetExtension($sName)
    Return StringMid($sName, StringInStr($sName, ".", 0, -1))
EndFunc   ;==>_GetExtension

Saludos

  • Like 1

Share this post


Link to post
Share on other sites
VIP
1 hour ago, Borje said:

Thanks Trong for this example but I can not have that to work can you explain how you insert a file to this, example: file  TEST.EXE and then

pick upp $sExtension

 

 

@Danyfirex Trong consider add something like this Global Enum $eDrive=1, $eDir, $eFileName, $eExtension, $eFullFileName, $ePath, $ePathNoEXT

Global Enum $eDrive = 1, $eDir, $eFileName, $eExtension, $eFullFileName, $ePath, $ePathNoEXT

Global $sPathSplit = _SplitPath(@ScriptFullPath)

ConsoleWrite("+ Path IN: " & $sPathSplit[0] & @CRLF)
ConsoleWrite("- Drive: " & $sPathSplit[1] & @CRLF)
ConsoleWrite("- Dir: " & $sPathSplit[2] & @CRLF)
ConsoleWrite("- FileName: " & $sPathSplit[3] & @CRLF)
ConsoleWrite("- Extension: " & $sPathSplit[4] & @CRLF)
ConsoleWrite("- FullFileName: " & $sPathSplit[5] & @CRLF)
ConsoleWrite("- Path: " & $sPathSplit[6] & @CRLF)
ConsoleWrite("- PathNoEXT: " & $sPathSplit[7] & @CRLF)

Global $sPathSplit = @SystemDir & "\cmd.exe"
ConsoleWrite("! Path IN: " & $sPathSplit & @CRLF)
ConsoleWrite("- Drive: " & _SplitPath($sPathSplit, $eDrive) & @CRLF)
ConsoleWrite("- Dir: " & _SplitPath($sPathSplit, $eDir) & @CRLF)
ConsoleWrite("- FileName: " & _SplitPath($sPathSplit, $eFileName) & @CRLF)
ConsoleWrite("- Extension: " & _SplitPath($sPathSplit, $eExtension) & @CRLF)
ConsoleWrite("- FullFileName: " & _SplitPath($sPathSplit, $eFullFileName) & @CRLF)
ConsoleWrite("- Path: " & _SplitPath($sPathSplit, $ePath) & @CRLF)
ConsoleWrite("- PathNoEXT: " & _SplitPath($sPathSplit, $ePathNoEXT) & @CRLF)


Func _SplitPath($sFilePath, $rType = 0)
    Local $aArray = StringRegExp($sFilePath, "^\h*((?:\\\\\?\\)*(\\\\[^\?\/\\]+|[A-Za-z]:)?(.*[\/\\]\h*)?((?:[^\.\/\\]|(?(?=\.[^\/\\]*\.)\.))*)?([^\/\\]*))$", 1)
    Local $rError = @error
    ReDim $aArray[8]
    If $rError Then $aArray[0] = $sFilePath ;  Original path
    If StringLeft($aArray[2], 1) == "/" Then
        $aArray[2] = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\/")
    Else
        $aArray[2] = StringRegExpReplace($aArray[2], "\h*[\/\\]+\h*", "\\")
    EndIf
    $aArray[5] = $aArray[3] & $aArray[4]
    $aArray[6] = $aArray[1] & $aArray[2]
    $aArray[7] = $aArray[6] & $aArray[3]
    If $rType = 1 Then Return $aArray[1] ; Drive
    If $rType = 2 Then Return $aArray[2] ; Dir
    If $rType = 3 Then Return $aArray[3] ; FileName
    If $rType = 4 Then Return $aArray[4] ; Extension
    If $rType = 5 Then Return $aArray[5] ; FullFileName : FileName & $eExtension
    If $rType = 6 Then Return $aArray[6] ; Path : Drive & $eDir
    If $rType = 7 Then Return $aArray[7] ; PathNoEXT : Drive & Dir & FileName
    Return $aArray ; Array with 8 elements
EndFunc   ;==>_SplitPath

 

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites
Borje

Thanks very very much Trong now I have this to work I have much to learn.

 

Share this post


Link to post
Share on other sites
Danyfirex

@Trong consider add something like this Global Enum  $eDrive=1,$eDir,$eFileName,$eExtension,$eFullFileName,$ePath,$ePathNoEXT

 

 

Saludos

  • Like 1

Share this post


Link to post
Share on other sites
Borje

Hi Danyfirex

Thank you very much for your example that works perfect your little example is was I need thanks again.

Have a nice day !!

 

 

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

×