Jump to content

Recommended Posts

Posted (edited)

This uses WMI to retrieve the Filename e.g. Test.exe = Test and FileExtension e.g. Test.exe = exe

Inspiration was taken from here & MSDN >>

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
Global $sFile = FileOpenDialog("Select A File To Find The Filename & Extension", @ScriptDir, "(*.*)")
If @error Then
    Exit
EndIf

;~ Example File - C:\Program Files\AutoIt\AutoIt.exe
ConsoleWrite(_GetFilename($sFile) & @CRLF) ; Gets Filename - AutoIt
ConsoleWrite(_GetFilenameExt($sFile) & @CRLF) ; Gets Filename Extension - exe
ConsoleWrite(_GetFilenameInt($sFile) & @CRLF) ; Gets Initial Filename - C:\Program Files\AutoIt\AutoIt.exe
ConsoleWrite(_GetFilenamePath($sFile) & @CRLF) ; Gets Path - \Program Files\AutoIt\
ConsoleWrite(_GetFilenameDrive($sFile) & @CRLF) ; Gets Drive - C:

Func _GetFilename($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.FileName
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilename

Func _GetFilenameExt($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Extension
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameExt

Func _GetFilenameInt($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Name
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameInt

Func _GetFilenameDrive($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return StringUpper($oObjectFile.Drive)
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenameDrive

Func _GetFilenamePath($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            Return $oObjectFile.Path
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_GetFilenamePath

_PathSplitWMI() based on the idea and code by MrCreatoR:

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <Array.au3>

Global $aReturn, $sFile = FileOpenDialog("Select A File To Find The Details About It.", @ScriptDir, "(*.*)")
If @error Then
    Exit
EndIf
$aReturn = _PathSplitWMI($sFile)
_ArrayDisplay($aReturn)

Func _PathSplitWMI($sFilePath)
    Local $oWMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\" & "." & "\root\cimv2")
    Local $oColFiles = $oWMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($sFilePath, "\", "\\") & "'")
    Local $aReturn[5]
    If IsObj($oColFiles) Then
        For $oObjectFile In $oColFiles
            $aReturn[0] = $oObjectFile.Name
            $aReturn[1] = StringUpper($oObjectFile.Drive)
            $aReturn[2] = $oObjectFile.Path
            $aReturn[3] = $oObjectFile.FileName
            $aReturn[4] = $oObjectFile.Extension
            Return $aReturn
        Next
    EndIf
    Return SetError(1, 1, 0)
EndFunc   ;==>_PathSplitWMI
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Why using WMI when you can use strings operations?

And if i would use it, i would probably do it like this (analogy of _PathSplit):

#include <Array.au3>

Global $File = FileOpenDialog("Select A File To Find The FileName & Extension", @ScriptDir, "(*.*)")
If @error Then Exit

$aPath = _FilePathSplit($File)
_ArrayDisplay($aPath)

Func _FilePathSplit($gf_Path)
    Local $gf_WMIService = ObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
    Local $gf_ColFiles = $gf_WMIService.ExecQuery("Select * From CIM_Datafile Where Name = '" & StringReplace($gf_Path, "\", "\\") & "'")
    Local $aRet[5]
    
    If IsObj($gf_ColFiles) Then
        For $gf_ObjectFile In $gf_ColFiles
            $aRet[0] = $gf_ObjectFile.Name
            $aRet[1] = $gf_ObjectFile.Drive
            $aRet[2] = $gf_ObjectFile.Path
            $aRet[3] = $gf_ObjectFile.FileName
            $aRet[4] = $gf_ObjectFile.Extension
            
            Return $aRet
        Next
    EndIf
    
    Return SetError(1, 1, 0)
EndFunc

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

No of course, but I was just showcasing the possibilities :) Nice example and normally I use StringRegExp()

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted
  Quote

normally I use StringRegExp()

Yep :).

 

  Reveal hidden contents

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Posted

OK, you got me I do use your Version :)

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 1 year later...
Posted (edited)

  On 1/30/2011 at 8:32 PM, 'MrCreatoR said:

Why using WMI when you can use strings operations?

2 years on and I'm asking the same as you, why? Just goes to show we all learn from our mistakes.

This code example is deprecated, so any further discussion about it is pointless. Thanks.

Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/30/2011 at 8:51 PM, 'MrCreatoR said:

Yep :).

Very nice, I must have overlooked that.

You should update it to include a 10th element - the parent folder/dir of the file (as it's often the program name).

i.e. C:Program FilesChat ToolsSkypeSkypegui.exe

It would then be complete.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

  Reveal hidden contents

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Posted

Your idea would be suited to using string functions, not WMI.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 1/22/2013 at 7:34 AM, 'TheSaint said:

You should update it to include a 10th element - the parent folder/dir of the file (as it's often the program name).

i.e. C:Program FilesChat ToolsSkypeSkypegui.exe

It would then be complete.

After much debate on why I don't feel the need to create a new _PathSplit and enquiring whether you would be up for the challenge, I've decided to re-tweak _PathSplit and will post an update shortly with the proposed changes you've requested.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...