Jump to content

Strip Character from String Not Working


 Share

Recommended Posts

Hi All-

I am new to all of this and I am sure that I am calling my function incorrectly but maybe one of you can tell me why, when I run the below code I see an error that _StringStripChr() called by a previous line with 0 arg(s). Min=2. first previous line calling this func is 1. 

I am trying to get the file version of MSO.dll but strip the decimals so I can work with just the raw version number as a whole number instead of decimal. Thank you!

$test1=_StringStripChr()
MsgBox(0, "Message", $test1)

;========================================================================================================
;
; Function Name:     _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
; Description:
; Parameters:
;    $sString_In   - The string to be stripped
;          $sChr   - The characters to be stripped (case sensitive)
;        $iFlags   - Flag to indicate the type of stripping that should be performed (add the flags together for multiple operations):
;                    1 = strip leading instances of characters in $sChr
;                    2 (Default) = strip trailing instances of characters in $sChr
;                    4 = Replace multiple instances of characters in $sChr with a single instance
;                    8 = strip all instances of $sChr (over-rides all other flags)
;        $iCount   - The max number of leading or trailing instances of $sChr to strip
;                    0 (Default) = Strip all
;
; Requirement:   None.
; Return Value:  Stripped string
; Author:       Bowmore
;
; Notes:
;
; Examples:
;         _StringStripChr("AAAAbdcaefgA", "A", 3, 0)
;         would return
;         "bdcaefg"
;
;         _StringStripChr("AAAAbdcaefgA", "A", 2, 0)
;         would return
;         "AAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "AB", 7, 0)
;         would return
;         "CAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "ABe", 8, 0)
;         would return
;         "Cbdcafg"
;
;========================================================================================================
$sString_In = FileGetVersion("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\mso.dll")

Func _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
    Local $sNewString = $sString_In
    Local $sChr1 = "."

    If (BitAND($iFlags, 8) = 8) Then
        For $i = 1 To StringLen($sChr)
            $sChr1 = StringMid($sChr, $i, 1)
            $sNewString = StringReplace($sNewString, $sChr1, "", 0, 1)
        Next
    Else
        If (BitAND($iFlags, 4) = 4) Then
            For $i = 1 To StringLen($sChr)
                $sChr1 = StringMid($sChr, $i, 1)
                $sNewString = StringRegExpReplace($sNewString, $sChr1 & "{2,}", $sChr1)
            Next
        EndIf

        If (BitAND($iFlags, 2) = 2) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringRight($sNewString, 1), 1))
                    $sNewString = StringTrimRight($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringRight($sNewString, 1), 1)) Then
                        $sNewString = StringTrimRight($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf

        If (BitAND($iFlags, 1) = 1) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringLeft($sNewString, 1), 1))
                    $sNewString = StringTrimLeft($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringLeft($sNewString, 1), 1)) Then
                        $sNewString = StringTrimLeft($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf
    EndIf

    Return ($sNewString)

EndFunc   ;==>_StringStripChr
Link to comment
Share on other sites

neuronyx,

Just link to the code, don't duplicate the content please.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

These are obviously the wrong arguments I don't have Office 14 to test but this is what it should look like:

Your code:

#include "_StringStripChr.au3"
$sString_In = FileGetVersion("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\mso.dll")
$test1=_StringStripChr($sString_In, 0, 2, 0)
MsgBox(0, "Message", $test1)

And the UDF that you have now included: (save as "_StringStripChr.au3" in the script dir)

;========================================================================================================
;
; Function Name:     _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
; Description:
; Parameters:
;    $sString_In   - The string to be stripped
;          $sChr   - The characters to be stripped (case sensitive)
;        $iFlags   - Flag to indicate the type of stripping that should be performed (add the flags together for multiple operations):
;                    1 = strip leading instances of characters in $sChr
;                    2 (Default) = strip trailing instances of characters in $sChr
;                    4 = Replace multiple instances of characters in $sChr with a single instance
;                    8 = strip all instances of $sChr (over-rides all other flags)
;        $iCount   - The max number of leading or trailing instances of $sChr to strip
;                    0 (Default) = Strip all
;
; Requirement:   None.
; Return Value:  Stripped string
; Author:       Bowmore
;
; Notes:
;
; Examples:
;         _StringStripChr("AAAAbdcaefgA", "A", 3, 0)
;         would return
;         "bdcaefg"
;
;         _StringStripChr("AAAAbdcaefgA", "A", 2, 0)
;         would return
;         "AAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "AB", 7, 0)
;         would return
;         "CAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "ABe", 8, 0)
;         would return
;         "Cbdcafg"
;
;========================================================================================================
Func _StringStripChr($sString_In, $sChr, $iFlags, $iCount)
    Local $sNewString = $sString_In
    Local $sChr1 = "."

    If (BitAND($iFlags, 8) = 8) Then
        For $i = 1 To StringLen($sChr)
            $sChr1 = StringMid($sChr, $i, 1)
            $sNewString = StringReplace($sNewString, $sChr1, "", 0, 1)
        Next
    Else
        If (BitAND($iFlags, 4) = 4) Then
            For $i = 1 To StringLen($sChr)
                $sChr1 = StringMid($sChr, $i, 1)
                $sNewString = StringRegExpReplace($sNewString, $sChr1 & "{2,}", $sChr1)
            Next
        EndIf

        If (BitAND($iFlags, 2) = 2) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringRight($sNewString, 1), 1))
                    $sNewString = StringTrimRight($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringRight($sNewString, 1), 1)) Then
                        $sNewString = StringTrimRight($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf

        If (BitAND($iFlags, 1) = 1) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringLeft($sNewString, 1), 1))
                    $sNewString = StringTrimLeft($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringLeft($sNewString, 1), 1)) Then
                        $sNewString = StringTrimLeft($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf
    EndIf

    Return ($sNewString)

EndFunc   ;==>_StringStripChr

Make sense?

Link to comment
Share on other sites

Once you get that set up, this was the mistake you were making:

$test1=_StringStripChr()

You were not calling any arguments. Look at the beginning part of the green writing and each of these are explained.

you will have to play around with them until you get the ones you need.

For instance:

_StringStripChr($sString_In, $sChr, $iFlags, $iCount)

$sString_In - was correct

$sChr - The characters to be stripped (case sensitive)

and so on...

good luck !

Bill

Link to comment
Share on other sites

neuronix,

Try it this way (path changed to my version of Office)...

local $file_ver = stringreplace(FileGetVersion('C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll'),'.','')
ConsoleWrite($file_ver & @LF)

kylomas

edit: you can do it using the funciton you posted like this...

ConsoleWrite(_StringStripChr(FileGetVersion('C:\Program Files (x86)\Common Files\microsoft shared\OFFICE12\mso.dll'),'.',8) & @LF)


;========================================================================================================
;
; Function Name:     _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
; Description:
; Parameters:
;    $sString_In   - The string to be stripped
;          $sChr   - The characters to be stripped (case sensitive)
;        $iFlags   - Flag to indicate the type of stripping that should be performed (add the flags together for multiple operations):
;                    1 = strip leading instances of characters in $sChr
;                    2 (Default) = strip trailing instances of characters in $sChr
;                    4 = Replace multiple instances of characters in $sChr with a single instance
;                    8 = strip all instances of $sChr (over-rides all other flags)
;        $iCount   - The max number of leading or trailing instances of $sChr to strip
;                    0 (Default) = Strip all
;
; Requirement:   None.
; Return Value:  Stripped string
; Author:       Bowmore
;
; Notes:
;
; Examples:
;         _StringStripChr("AAAAbdcaefgA", "A", 3, 0)
;         would return
;         "bdcaefg"
;
;         _StringStripChr("AAAAbdcaefgA", "A", 2, 0)
;         would return
;         "AAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "AB", 7, 0)
;         would return
;         "CAbdcaefg"
;
;         _StringStripChr("ABCAAbdcaefgA", "ABe", 8, 0)
;         would return
;         "Cbdcafg"
;
;========================================================================================================

Func _StringStripChr($sString_In, $sChr, $iFlags = 2, $iCount = 0)
    Local $sNewString = $sString_In
    Local $sChr1 = "."

    If (BitAND($iFlags, 8) = 8) Then
        For $i = 1 To StringLen($sChr)
            $sChr1 = StringMid($sChr, $i, 1)
            $sNewString = StringReplace($sNewString, $sChr1, "", 0, 1)
        Next
    Else
        If (BitAND($iFlags, 4) = 4) Then
            For $i = 1 To StringLen($sChr)
                $sChr1 = StringMid($sChr, $i, 1)
                $sNewString = StringRegExpReplace($sNewString, $sChr1 & "{2,}", $sChr1)
            Next
        EndIf

        If (BitAND($iFlags, 2) = 2) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringRight($sNewString, 1), 1))
                    $sNewString = StringTrimRight($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringRight($sNewString, 1), 1)) Then
                        $sNewString = StringTrimRight($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf

        If (BitAND($iFlags, 1) = 1) Then
            If $iCount = 0 Then
                While (StringInStr($sChr, StringLeft($sNewString, 1), 1))
                    $sNewString = StringTrimLeft($sNewString, 1)
                WEnd
            Else
                For $i = 1 To $iCount
                    If (StringInStr($sChr, StringLeft($sNewString, 1), 1)) Then
                        $sNewString = StringTrimLeft($sNewString, 1)
                    Else
                        ExitLoop
                    EndIf
                Next
            EndIf
        EndIf
    EndIf

    Return ($sNewString)

EndFunc   ;==>_StringStripChr

but why over complicate it?

Note - the _stringstripchar function is useful if you want to strip multiple chars from a string or control where the chars are stripped from 

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Wow! This forum is awesome! Thanks to everyone for the replies. I have been slowly learning this over time to make my job easier but you guys rock! I have had good luck in writing these things so far but this one honestly had me stumped! I am also brand spanking new to functions....yeah I don't know how I survived this long without them but I am a quick learner!

Apologies for duplicating the code, I will make sure to omit that although a HUGE thanks to Bowmore for the code....it appears to do exactly what I was looking for. 

Bohica- That code is perfect and does what I needed.

I didn't realize that I need to call my getfileversion inside of the function name! Is it typical to save functions in a separate AU3 file? When it compiles to a EXE are the functions automatically included with the EXE as well?

Thanks again all! Everyone here is amazing!

Edited by neuronyx
Link to comment
Share on other sites

I made great progress with my script but alas I am stuck again. Can someone tell me why if something.dll does NOT exist, why this statement returns "Say this"? If I check the output of $ver it is 0.0.0.0 since the file does not exist. 

$ver = FileGetVersion("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\something.dll")

If $ver = "0.0.0.0" Then
    MsgBox(0,"",$ver)
Else
    MsgBox(0,"","Say this.")
EndIf
Link to comment
Share on other sites

You have the statements mixed up.  I would think you want to display if NOT 0.0.0.0, right?

$ver = FileGetVersion("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\something.dll")
 
If $ver <> "0.0.0.0" Then
    MsgBox(0,"",$ver)
Else
    MsgBox(0,"","Say this.")
EndIf
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Ugh, thanks Universal. I was stripping my periods out with my original function so it was returning 0000 not 0.0.0.0.

In either case can someone tell me why <> would be any different than the If Not Statement. Am I not saying something identical in this case?

Thanks! :)

Link to comment
Share on other sites

Or you could try this instead of the 0.0.0.0

$ver = FileGetVersion("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\something.dll")
ConsoleWrite("$ver =  " & $ver & @CRLF)

If Not @error = 1 Then
    MsgBox(0, "", $ver)
Else
    MsgBox(0, "", "Say this.")
EndIf
Link to comment
Share on other sites

Maybe this is clearer:

If Not @error = 1 Then  ; if the @error does NOT return 1 then....
    MsgBox(0, "", $ver)
Else
    MsgBox(0, "", "Say this.")
EndIf
;==================================================
If $ver <> "0.0.0.0" Then ; anything that is NOT exactly this Then.....
    MsgBox(0,"",$ver)
Else
    MsgBox(0,"","Say this.")
EndIf
Link to comment
Share on other sites

You can make that a bit more simple:

If Not @error Then

Using Not for my case:

If Not ($ver = "0.0.0.0") Then

lots of ways to do the same thing.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

 

Or you could try this instead of the 0.0.0.0

$ver = FileGetVersion("C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14\something.dll")
ConsoleWrite("$ver =  " & $ver & @CRLF)

If Not @error = 1 Then
    MsgBox(0, "", $ver)
Else
    MsgBox(0, "", "Say this.")
EndIf

Your error check is in the wrong place, it will never equal 1.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

 

Your error check is in the wrong place, it will never equal 1.

Interesting...

The help file says:

 

Failure:    "0.0.0.0" if no version information (or other error) or "" when retrieving a stringname, and sets the @error flag to 1.

Could you explain please?

Link to comment
Share on other sites

Good luck getting ConsoleWrite to return an error :)

$ver = FileGetVersion("test.dll")
$error = @error
ConsoleWrite("$ver =  " & $ver & @CRLF)

If Not $error Then
    MsgBox(0, "", $ver)
Else
    MsgBox(0, "", "Say this.")
EndIf
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Why?  Am I missing something ?

#include <MsgBoxConstants.au3>

Example()

Func Example()
    ; Retrieve the file version of the AutoIt executable.
    Local $sFileVersion = FileGetVersion("@AutoItExen")   ; intentionally wrong to test @error
ConsoleWrite("@error =  " & @error & @CRLF)
If Not @error And $sFileVersion <> "0.0.0.0" Then
    ; Display the file version. This should be equal to @AutoItVersion.
    MsgBox($MB_SYSTEMMODAL, "", $sFileVersion)
Else
    MsgBox($MB_SYSTEMMODAL, "", "Error = 1")
    EndIf
EndFunc   ;==>Example
Link to comment
Share on other sites

Run this, @error returns the value from last applicable function (the later is the return from the ConsoleWrite):

$ver = FileGetVersion("not a real file.dll")
$error = @error
ConsoleWrite(@error & "=?" & $error & ":" & (@error=$error) & @CRLF)
ConsoleWrite("$ver =  " & $ver & @CRLF)
ConsoleWrite(@error & "=?" & $error & ":" & (@error=$error) & @CRLF)
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I cant find a smiley that shows a head exploding... :

Thanks for the attempt to clarify but that went right over my head...

but you are saying that this ConsoleWrite Error check is not the error flag 1 from FileGetVersion?

From post 18

Local $sFileVersion = FileGetVersion("@AutoItExen")   ; intentionally wrong to test @error
ConsoleWrite("@error =  " & @error & @CRLF)
Link to comment
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
 Share

  • Recently Browsing   0 members

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