Sign in to follow this  
Followers 0

_GetFilename() & _GetFilenameExt() using WMI

9 posts in this topic

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

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

normally I use StringRegExp()

Yep :).

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted (edited)

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

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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

Share this post


Link to post
Share on other sites

Posted

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.

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