Jump to content
31290

Extract "never the same" string from filename

Recommended Posts

31290

Hi everyone :)

Hope you're doing great today. ;)

Well, I have a little question about extracting a certain string from a file name to make comparison after treatment.

Let's get into the details:

First of all, I gather and store a machine BIOS Version by running this:

RunWait(@ComSpec & " /c " & "wmic bios get SMBIOSBIOSVERSION >> C:\Drivers\Tag.txt" & @CRLF, "", @SW_HIDE, "$STDOUT_CHILD")
_FileWriteToLine("c:\Drivers\BIOS_Version.txt", 1, "", 1)
$sContent = FileRead ("C:\Drivers\BIOS_Version.txt")
$sContent = StringRegExpReplace($sContent, " ", "")

Result is, for example, A10

Then, I download the latest available BIOS version from the Dell related model website and the filename of the latest BIOS is stored in a $sBIOSName variable.

Result is for example, E5440A14.exe

What i can't figure out is how to extract the Bios name stored in the $sBIOSName knowing that each model this app would run won't have the same number of chars.
Indeed, I have some OPL790AXX.exe / OPL7010AXX.exe / E5470AXX.exe etc...

I imagine something like getting the whole string, left trimming it to the first A found count 2 chars on the right and store them to have the possibility to make the check between A10 and A14 (as the number of char of filenames is never the same)

Hope this is clear :) If not, don't hesitate to ask for more.
Thanks in advance for the help :)

 

Edited by 31290

~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites
water

Use StringInStr to get the position of the first "A" and then use this as starting position for your calculation.

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
31290

Hi Water :)

Thanks for the reply.

Indeed, I just found this way to do so:

RunWait(@ComSpec & " /c " & "wmic bios get SMBIOSBIOSVERSION >> C:\Drivers\Tag.txt" & @CRLF, "", @SW_HIDE, "$STDOUT_CHILD")
_FileWriteToLine("c:\Drivers\BIOS_Version.txt", 1, "", 1)
$sContent = FileRead ("C:\Drivers\BIOS_Version.txt")
$sContent = StringRegExpReplace($sContent, " ", "")
$sMidBIOSNAME = StringInStr ($sBIOSName, "A")
$sFinalBIOSNAME = StringMid($sBIOSName, $sMidBIOSNAME, 3)
msgbox (0, "", $sFinalBIOSNAME)

I got the expected result. 

Do you think this can be optimized / shortened or not? (just curious ^^)


~~~ Doom Shall Never Die, Only The Players ~~~

Share this post


Link to post
Share on other sites
water

RegExp's are most of the time shorter than regular AutoIt functions. But don't forget to add comments so you still know what the RegExp does in a few months.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jguinch

I know it's not the question, but you should get the BIOS version directly from the registry :

Func _BiosVersion()
    Return RegRead("HKLM\HARDWARE\DESCRIPTION\System\BIOS", "BIOSVersion")
EndFunc

 

Share this post


Link to post
Share on other sites
Danyfirex

Hello. Just for fun.

 

$str = "   OPL790A12.exe   "
Msgbox(0,"",StringMid($str,StringInStr($str,".exe")-3,3))

Saludos

Share this post


Link to post
Share on other sites
water

Or use "Scriptomatic" to generate the WMI query for you. It assigns the result to an variable or array.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
VIP

Script get the version wrong syntax:

RunWait(@ComSpec & " /c " & "wmic bios get SMBIOSBIOSVERSION >> C:\Drivers\Tag.txt" & @CRLF, "", @SW_HIDE, "$STDOUT_CHILD")

TO:

RunWait(@ComSpec & " /c " & "wmic bios get SMBIOSBIOSVERSION" & @CRLF, "", @SW_HIDE, $STDOUT_CHILD)
;::OR
RunWait(@ComSpec & " /c " & "wmic bios get SMBIOSBIOSVERSION >> C:\Drivers\Tag.txt" & @CRLF, "", @SW_HIDE, $STDOUT_CHILD)

and something useful:

Local $iPID = Run('"' & @ComSpec & '" /c ' & 'wmic bios get SMBIOSBIOSVERSION', '', @SW_HIDE, 0x2) ;$STDOUT_CHILD (0x2)
ProcessWaitClose($iPID)
Local $sSMBIOSBIOSVersion = StdoutRead($iPID)
$sSMBIOSBIOSVersion = StringStripWS(StringReplace(StringReplace($sSMBIOSBIOSVersion, "SMBIOSBIOSVersion", ""), " ", ""), 8)

Local $hFileOpen = FileOpen("C:\Drivers\BIOS_Version.txt", 2 + 8)
FileWrite($hFileOpen, $sSMBIOSBIOSVersion)
FileClose($hFileOpen)

ConsoleWrite($sSMBIOSBIOSVersion & @CRLF)
;~ ConsoleWrite(@CRLF & _WMI_SingleQuery("Win32_BIOS", "Manufacturer") & @CRLF)
;~ ConsoleWrite(@CRLF & _WMI_SingleQuery("Win32_BIOS", "SMBIOSBIOSVERSION") & @CRLF)
;~ ConsoleWrite(@CRLF & _BIOSinfo() & @CRLF)

Func _BIOSinfo($sComputer = ".")
    Local $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & $sComputer & "\root\CIMV2")
    Local $Output, $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_BIOS", "WQL", 0x10 + 0x20)
    If IsObj($colItems) Then
        For $objItem In $colItems
            $Output &= $objItem.BIOSVersion(0) & @CR
            $Output &= $objItem.Manufacturer & @CR
            $Output &= $objItem.SMBIOSBIOSVersion
            Return $Output
        Next
    EndIf
EndFunc   ;==>_BIOSinfo

Func _WMI_SingleQuery($sClass, $sProperty, $sComputer = ".")
    Local $objWMI = ObjGet("winmgmts:\\" & $sComputer & "\root\CIMV2")
    Local $objInstances = $objWMI.InstancesOf($sClass)
    Local $count = $objInstances.Count
    If @error Or Not $count Then Return SetError(1, 0, "") ;"Object Error"
    For $objItem In $objInstances
        For $objProperties In $objItem.Properties_()
            If ($objProperties.Name = $sProperty) Then Return $objProperties.Value
        Next
    Next
    Return SetError(2, 0, "") ;"Nothing Found"
EndFunc   ;==>_WMI_SingleQuery

 


Regards,
 

Share this post


Link to post
Share on other sites
31290

Many thanks all for your inputs, much appreciated!

Have a great day!


~~~ Doom Shall Never Die, Only The Players ~~~

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

  • Similar Content

    • WoodGrain
      By WoodGrain
      Hi All,
      I'd like to replace 'COMMA' with ',' for example:
      $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', ',') Now I've tried escaping the ',' in various ways unsuccessfully, such as:
      '[,]'
      "[,]"
      '\,'
      [,] seems to work in the pattern, I just can't figure out how to use it in the replace, and it seems everyone online is only interested in removing/replacing commas lol.
      I also tried creating and using a variable as the replacement but also didn't work:
      $myComma = "," $myString = "COMMA" StringRegExpReplace($myString, 'COMMA', $myComma) I'm sure it's super simple if someone could point me in the right direction - thanks.
    • kartune
      By kartune
      Hello, I am getting this error message when running my script:
      Case $aButiD[0] To $aButiD[$iTotButtons - 1]
      Case ^ ERROR
      Error:  Array variable has incorrect number of subscripts or subscript dimension range exceeded.
       
      The script's purpose is to read my ini file and create buttons for the sections and section contents.
      The first part of the script creates buttons for the sections of the ini, once you click on the section button it should open another set of buttons of all the contents under the section
      In this case, the section in the .ini file is called "TPA"
      on the Func flavorco()
      It opens the contents of "TPA" into buttons.
      Right now, the section it is reading is determined by $sSection.
      It is only when I try to change $sSection = "TPA"
      to
      $sSection = $flavorco
      that the error starts to appear
       
      Here is the script
      #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> #include <Array.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <ButtonConstants.au3> $optbar = IniReadSectionNames("test.ini") If Not IsArray($optbar) Then Exit Local $Button[UBound($optbar) - 1] $mGUI = GUICreate('Restock Flavor Ingredients', 10 + (130 * UBound($optbar)), 100) For $x = 1 To UBound($optbar) - 1 $var = IniReadSection("test.ini", $optbar[$x]) If @error Then ContinueLoop $X_Coord = 10 + (($x - 1) * 100) $Button[$x - 1] = GUICtrlCreateButton($optbar[$x], $X_Coord, 30, 70, 30) Next GUISetState() While 1 $mMsg = GUIGetMsg() If $mMsg = $GUI_EVENT_CLOSE Then GUIDelete($mGUI) ExitLoop EndIf For $i = 0 To UBound($Button) - 1 If $mMsg = $Button[$i] Then MsgBox(0, '$Msg = ' & $mMsg, GuiCtrlRead($Button[$i]) & ' Pressed', 2) Global $flavorco = $Button ; sets the flavor company's variable Call('flavorco') EndIf Next WEnd Func flavorco() ;opened flavor company's flavors MsgBox(0, '$Msg = ' & $mMsg, GuiCtrlRead($Button[$i]) & $flavorco, 0) Local $sFilePath = @ScriptDir & "\Test.ini" Local $sSection = $Button Local $aArray = IniReadSection($sFilePath, $sSection) _ArrayDelete($aArray, 0) ;_ArrayDisplay($aArray) Local $iIndex Local $iTotButtons = UBound($aArray) Local $iNumPerRow = 5 Local $iButWidth = 100 Local $iButDepth = 30 Local $aButiD[$iTotButtons] Local $hGUI = GUICreate('Ingredients Stock "' & $sFilePath & '"', 10 + ($iButWidth + 10) * $iNumPerRow, _ 10 + Ceiling($iTotButtons / $iNumPerRow) * ($iButDepth + 10), -1, $WS_EX_TOPMOST) For $i = 0 To $iTotButtons - 1 ; $x = $iXBorder + (($iRectWidth + $iSpacing) * Mod($i, $iNumCols)) $x = 10 + (($iButWidth + 10) * Mod($i, $iNumPerRow)) ; $y = $iYBorder + (($iRectDepth + $iSpacing) * Int($i / $iNumCols)) $y = 10 + (($iButDepth + 10) * Int($i / $iNumPerRow)) $aButiD[$i] = GUICtrlCreateButton($aArray[$i][0], $x, $y, $iButWidth, $iButDepth) ;, $BS_NOTIFY) Next GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Switch $msg Case $GUI_EVENT_CLOSE GUIDelete($hGUI) ExitLoop Case $aButiD[0] To $aButiD[$iTotButtons - 1] ;clicking on a flavor name $iIndex = ($msg - $aButiD[0]) $RSINGRflavor = $aArray[$iIndex][0] ;flavor name $RSINGRml = $aArray[$iIndex][1] ; ml $RSINGRgal = $aArray[$iIndex][1]*0.000264172 ;converts ml to gal Call('openflavor') EndSwitch WEnd EndFunc Func openflavor() #Region ### START Koda GUI section ### Form=F:\Karl\AutoIt\FJ-Stock JP\GUI Forms\RSINGRSUBFORM.kxf ;opens flavor to view stock and restock submit $RSINGRSUBFORM = GUICreate($sSection & " " & $RSINGRflavor, 242, 213, 530, 269) $RSINGRFlavorTitle = GUICtrlCreateLabel($sSection & " " & $RSINGRflavor, 32, 8, 200, 28) GUICtrlSetFont(-1, 16, 800, 0, "MS Sans Serif") $RSINGRstockcaption = GUICtrlCreateLabel("Current Stock in ml:", 16, 48, 96, 17) $RSINGRstockml = GUICtrlCreateLabel($RSINGRml, 120, 48, 114, 25) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") $RSINGRInput = GUICtrlCreateInput("ml", 40, 112, 153, 21) $RSINGRSUBMIT = GUICtrlCreateButton("SUBMIT", 16, 152, 81, 33) $RSINGRCLOSE = GUICtrlCreateButton("CLOSE", 145, 151, 81, 33) $RSINGRgalcaption = GUICtrlCreateLabel("Current Stock in gal:", 16, 80, 97, 17) $RSINGRstockgal = GUICtrlCreateLabel($RSINGRGAL, 120, 80, 114, 25) GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif") GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### ; $msg = "" While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($RSINGRSUBFORM) ExitLoop Case $RSINGRCLOSE GUIDelete($RSINGRSUBFORM) ExitLoop EndSwitch WEnd EndFunc  
      Any help is much appreciated!  Thanks.
      My guess is that $Button isn't an actual string??  Whenever I use msgbox it shows up the string exactly, no numbers attached.  So i'm not sure whats going on here.
    • XinYoung
      By XinYoung
      Hello again,
      I'm trying to use the FileMove function to rename a bunch of text files.
      Some of the files have "_1" at the end of their name, for example, "File123_1.txt". I want to remove the "_1" so it would become "File123.txt".
      What I currently have...
              FileMove($sSource & "\*_1.txt", $sDestination & "\*.txt")
      But since I'm using a wildcard, it doesn't appear to be working. I think it's just replacing .txt with .txt. Am I going about this the wrong way? How can I use wildcards and still accomplish this?
      Any help is greatly appreciated ^__^
    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
    • rkr
      By rkr
      Hi, i want to read a particular string from a text file using autoit. i wish to read it without explicitly opening the text file. the copied string should be then transferred to an excelbook (again, no need to explicitly open the excel book)... 

      with reference to my screenshot attached, my input to the  'script'' is going to be 0017-0008, and the script should copy the highlighted two lines from the input file to excel
      thanks

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.