31290

Extract "never the same" string from filename

10 posts in this topic

#1 ·  Posted (edited)

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

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

1 person likes this

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

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

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

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

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

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

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

    • 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

    • Kiran_L
      By Kiran_L
      Hi guys,
       
      I am trying to read a pdf file with unstructured data. I dontot know how to handle pdf activities in AutoIt,
      Can you help me with any UDF to open the PDF and read the doc.
       
      Thanks for your time.
       
    • cheeroke
      By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc