Jump to content



Photo

_GetFilename() & _GetFilenameExt() using WMI


  • Please log in to reply
8 replies to this topic

#1 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,153 posts

Posted 30 January 2011 - 07:04 PM

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 >> http://www.autoitscript.com/forum/topic/124861-how-i-detect-binary-file-of-text-file/page__pid__866935#entry866935

AutoIt         
#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:
AutoIt         
#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, 17 May 2011 - 07:49 PM.

UDF List:
Spoiler
Updated: 31/03/2015







#2 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,266 posts

Posted 30 January 2011 - 08:32 PM

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):

AutoIt         
#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

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1



AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler


Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *


==================================================
My_Userbar.gif
==================================================

 

AutoIt is simple, subtle, elegant. © AutoIt Team


#3 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,153 posts

Posted 30 January 2011 - 08:38 PM

No of course, but I was just showcasing the possibilities :) Nice example and normally I use StringRegExp()
UDF List:
Spoiler
Updated: 31/03/2015

#4 MrCreatoR

MrCreatoR

    Must AutoIt!

  • MVPs
  • 3,266 posts

Posted 30 January 2011 - 08:51 PM

normally I use StringRegExp()

Yep :).

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1



AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler


Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *


==================================================
My_Userbar.gif
==================================================

 

AutoIt is simple, subtle, elegant. © AutoIt Team


#5 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,153 posts

Posted 30 January 2011 - 08:54 PM

OK, you got me I do use your Version :)
UDF List:
Spoiler
Updated: 31/03/2015

#6 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,153 posts

Posted 21 January 2013 - 01:34 AM

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, 21 January 2013 - 01:34 AM.

UDF List:
Spoiler
Updated: 31/03/2015

#7 TheSaint

TheSaint

    *** Death is a Jester ***

  • MVPs
  • 7,038 posts

Posted 22 January 2013 - 07:34 AM

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 Files\Chat Tools\Skype\Skypegui.exe
It would then be complete.

Make sure brain is in gear before opening mouth!

If I put effort into communication, I expect you to read properly & fully, or just not comment.

Spoiler

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox


#8 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,153 posts

Posted 22 January 2013 - 09:56 AM

Your idea would be suited to using string functions, not WMI.
UDF List:
Spoiler
Updated: 31/03/2015

#9 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,153 posts

Posted 22 January 2013 - 03:19 PM

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 Files\Chat Tools\Skype\Skypegui.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:
Spoiler
Updated: 31/03/2015




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users