Sign in to follow this  
Followers 0
Clay

String Mid

9 posts in this topic

#1 ·  Posted (edited)

Issue:

So I have a script that reads an xml file that is part of an install.... I search the xml file line by line for a storage path and I want to make sure that there are no leading slashes / or \ before or after the storage path. I am using the stringmid function to get the character before and after the storage path string but I dont like how this function works with Autoit .....(my 1 and only issue with Autoit it since I have started using it for everything haha) is there a better way to do what I would like to accomplish or another funtion that I can use.

#include <File.au3>
#include <String.au3>

Local $tmp
Local $line
Local $file
Local $CONFIGFILEDIR

Local $getId

$CONFIGFILEDIR = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Trading Technologies\Installation", "CONFIGFILEDIR") &"\aconfig_local.txt"; or C:tt\config\aconfig_local.txt
$file = fileopen($CONFIGFILEDIR, 0)
    If $file = -1 Then
        MsgBox(0,"", "Could Not Do It")
    Else
        While 1
            $line = FileReadLine($file)
            If @error = -1 Then MsgBox(0, "Line read:", "It Error")
            $tmp = StringInStr($line, "c:\tt\config", 0)
            If StringInStr($line, "c:\tt\config", 0) Then 
                    MsgBox(0, "Line read:", $line)
                    $Test = StringMid($line,2, 2)
                    
                ;$getId = _StringBetween($line, '<value>', '</value>')
                    MsgBox(0,"", $Test)
                    
                ;$Test = StringMid($line, StringLen("<value>c:\tt\config") )
                ;MsgBox(0,"", $Test)
                    Exit
            EndIf
        Wend
    EndIf
Edited by Clay

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Issue:

So I have a script that reads an xml file that is part of an install.... I search the xml file line by line for a storage path and I want to make sure that there are no leading slashes / or \ before or after the storage path. I am using the stringmid function to get the character before and after the storage path string but I dont like how this function works with Autoit .....(my 1 and only issue with Autoit it since I have started using it for everything haha) is there a better way to do what I would like to accomplish or another funtion that I can use.

#include <File.au3>
#include <String.au3>

Local $tmp
Local $line
Local $file
Local $CONFIGFILEDIR

Local $getId

$CONFIGFILEDIR = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Trading Technologies\Installation", "CONFIGFILEDIR") &"\aconfig_local.txt"; or C:tt\config\aconfig_local.txt
$file = fileopen($CONFIGFILEDIR, 0)
    If $file = -1 Then
        MsgBox(0,"", "Could Not Do It")
    Else
        While 1
            $line = FileReadLine($file)
            If @error = -1 Then MsgBox(0, "Line read:", "It Error")
            $tmp = StringInStr($line, "c:\tt\config", 0)
            If StringInStr($line, "c:\tt\config", 0) Then 
                    MsgBox(0, "Line read:", $line)
                    $Test = StringMid($line,2, 2)
                    
        ;$getId = _StringBetween($line, '<value>', '</value>')
                    MsgBox(0,"", $Test)
                    
        ;$Test = StringMid($line, StringLen("<value>c:\tt\config") )
        ;MsgBox(0,"", $Test)
                    Exit
            EndIf
        Wend
    EndIf
We don't have your registry keys...

Recode your demo with a static string for the example input and just show what you want to do with it. Remove all the irrelevant and commented code. I can't make out from this what the string looks like to start, and what exactly you want to do with it.

:)

P.S. StringMid() works fine, but may not be the best tool for this job, I was thinking of StringRegExp() -- if we can understand what you are trying to do.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Very Similar to a UDF addition of mine

Func _Multi_Trim($tmp)
    While StringLeft($tmp, 1) = "\" Or StringLeft($tmp, 1) = "/"
        $tmp = StringTrimLeft($tmp, 1)
    WEnd
    While StringRight($tmp, 1) = "\" Or StringRight($tmp, 1) = "/"
        $tmp = StringTrimRight($tmp, 1)
    WEnd
    Return $tmp
EndFunc;==>_Multi_Trim

Then just:

$new_path = _Multi_Trim($suspect_path)
to remove any "/" or "\" from the path string ends, while leaving any that are in the middle. Otherwise...

$new = StringRegExpReplace($s_string,"[\\\/]"," ")
strips them all out Edited by MagnumXL

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Very Similar to a UDF addition of mine

Func _Multi_Trim($tmp)
    While StringLeft($tmp, 1) = "\" Or StringLeft($tmp, 1) = "/"
        $tmp = StringTrimLeft($tmp, 1)
    WEnd
    While StringRight($tmp, 1) = "\" Or StringRight($tmp, 1) = "/"
        $tmp = StringTrimRight($tmp, 1)
    WEnd
    Return $tmp
EndFunc ;==>_Multi_Trim

Then just:

$new_path = _Multi_Trim($suspect_path)

to remove any "/" or "\" from the path string.

StringRegExpReplace($s_string, "\A[/\\]{2,}|[/\\]{2,}\z", "")oÝ÷ ØGbµ*+¯'è®
-¶n¶Ø^¶)èÇ­ì°z·«zj/yØÊ°j{^vØZµ«­¢+ÙMÑÉ¥¹IáÁIÁ± ÀÌØíÍ}ÍÑÉ¥¹°ÅÕ½ÐìÀäÈí¡l¼ÀäÈìÀäÈít¤¡l¼ÀäÈìÀäÈít¥ìıõð¡l¼ÀäÈìÀäÈít¤¡l¼ÀäÈìÀäÈít¥ìıôÀäÈíèÅÕ½Ðì°ÅÕ½ÐìÀäÈìÄÀäÈìÌÅÕ½Ðì
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

We don't have your registry keys...

Recode your demo with a static string for the example input and just show what you want to do with it. Remove all the irrelevant and commented code. I can't make out from this what the string looks like to start, and what exactly you want to do with it.

:)

P.S. StringMid() works fine, but may not be the best tool for this job, I was thinking of StringRegExp() -- if we can understand what you are trying to do.

Yea I tried StringRegExp but that blows my mind

#include <File.au3>
#include <String.au3>

Local $tmp
Local $line
Local $file
Local $CONFIGFILEDIR

Local $getId

$CONFIGFILEDIR = "C:\ttb\config\aconfig_local.txt"
$file = fileopen($CONFIGFILEDIR, 0)
    If $file = -1 Then
        MsgBox(0,"", "Could Not Do It")
    Else
        While 1
            $line = FileReadLine($file)
            If @error = -1 Then MsgBox(0, "Line read:", "It Errored out")
            $tmp = StringInStr($line, "c:\ttb\config", 0)
            If StringInStr($line, "c:\ttb\config", 0) Then 
                    MsgBox(0, "Line read:", $line)
                    ;At this point $line would be <value>c:\ttb\config<\value>
                    ; I want to make sure thatthe string between the value tags have no leading or trailing slashes 
                    ; If I understand how to create regular expressions I would love to use it here .. but unfortunately I am not too good with that Func 
                    
                    
                    ;$getId = StringLeft($line, 7) poor mans way of doing this
                    ;MsgBox(0, "StringBetween", $getId )
                    Exit
            EndIf
        Wend
    EndIf 
[\Code]


            
        

        
            
                

    
        
            
        
    


            
        

        
            
                
                
                
            
            
                
            
        
        
    

    
        
            Share this post
            
Link to post
            
            
            

            
                Share on other sites
                

    
        
            

    

        
            

    

        
            

    

        
            

    

        
    

            
        
    


    

                    
                
                    
                    
                    






    
    
        
Clay   

    
        
    
             1
    
        
    

        
            
                Wayfarer
            
            

            
                

    
        
    

            
            Active Members
            
            
                

    
        
    
             1
    
        
    

                82 posts
                
                

            
        
    
    
        


    

    
        
            
            
                
            
            
        

        
    #6 · 
            Posted 
            
            
            
        
    

    


            
        
            
Edit: Sorry, forgot to put them back in once they were removed StringRegExpReplace($s_string, "\A([/\\])([/\\]){1,}|([/\\])([/\\]){1,}\z", "\1\3")

I do not want to remove it... I should have been more specific .. I want to check to see if they are there. If I have slashes then I know my xml file is not configured properly and then I can handle it with another function...

Share this post


Link to post
Share on other sites

I do not want to remove it... I should have been more specific .. I want to check to see if they are there. If I have slashes then I know my xml file is not configured properly and then I can handle it with another function...

Check @extended, if they were removed, it will be greater than zero.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#include <array.au3>
$s_val = "<value>c:\1\2<\value>" & @CRLF & _
    "<value>\c:\3\4<\value>" & @CRLF & _
    "<value>/c:\5\6<\value>" & @CRLF & _
    "<value>c:\7\8/<\value>" & @CRLF & _
    "<value>c:\9\10<\value>" & @CRLF & _
    "<value>\c:\11\12\<\value>" & @CRLF & _
    "<value>/c:\13\14/<\value>"

$a_array = _quickanddirty($s_val)
If IsArray($a_array) Then
    _ArrayDisplay($a_array, "Should be: 1/2 and 9/10")
EndIf

Func _quickanddirty($s_string)
    Local $a_vals = StringRegExp($s_string, "(?i)<value>(.+?)<\\value>", 3)
    If IsArray($a_vals) = 0 Then Return SetError(1, 0, 0)
    Local $a_return = $a_vals, $i_add = 0
    For $icc = 0 To UBound($a_vals) - 1
        StringRegExpReplace($a_vals[$icc], "\A[\\/]|[\\/]\z", "")
        If @extended = 0 Then
            $i_add += 1
            $a_return[$i_add] = $a_vals[$icc]
        EndIf
    Next
    If $i_add = 0 Then Return SetError(2, 0, 0)
    ReDim $a_return[$i_add + 1]
    $a_return[0] = $i_add
    Return $a_return
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Here, quick and dirty was bothering me:

$s_val = "<value>c:\1\2<\value>" & @CRLF & _
    "<value>\c:\3\4<\value>" & @CRLF & _
    "<value>/c:\5\6<\value>" & @CRLF & _
    "<value>c:\7\8/<\value>" & @CRLF & _
    "<value>c:\9\10<\value>" & @CRLF & _
    "<value>\c:\11\12\<\value>" & @CRLF & _
    "<value>/c:\13\14/<\value>"

$a_array = StringRegExp($s_val, "(?i)<value>(?![\\/])(.+?)(?<=[^\\/])<\\value>", 3)
If IsArray($a_array) Then
    _ArrayDisplay($a_array, "Should be: 1/2 and 9/10")
EndIf

1 line: StringRegExp($s_val, "(?i)<value>(?![\\/])(.+?)(?<=[^\\/])<\\value>", 3)


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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