Jump to content

Extract All Strings


SmOke_N
 Share

Recommended Posts

  • Moderators

Has anyone scripted something to extract all possible string combinations from a script then write them to a $StringVariable or something similar?

Example:

Run(@ComSpec & " /c " & 'command', "", @SW_HIDE)
$var = '"whatever"'
$var2 = "'whatever again'"
$var3 = ""

then it would return using FileWriteLine()

Dim $StringVar1 = " /c "
Dim $StringVar2 = 'command'
Dim $StringVar3 = ""
Dim $StringVar4 = '"whatever"'
Dim $StringVar5 = "'whatever again'"

Run(@ComSpec & $StringVar1 & $StringVar2, $StringVar3, @SW_HIDE)
$var = $StringVar4
$var2 = $StringVar5
$var3 = $StringVar3; it's already been found, no need to make another variable

I've been at it for a little while now, and maybe I'm just making it more difficult thant what it needs to be, just wondering if someone might have a working example on it or something similar. I don't have even a semi working example to post... But if I get one working somewhat, I'll post it.

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.

Link to comment
Share on other sites

Has anyone scripted something to extract all possible string combinations from a script then write them to a $StringVariable or something similar?

Example:

Run(@ComSpec & " /c " & 'command', "", @SW_HIDE)
$var = '"whatever"'
$var2 = "'whatever again'"
$var3 = ""

then it would return using FileWriteLine()

Dim $StringVar1 = " /c "
Dim $StringVar2 = 'command'
Dim $StringVar3 = ""
Dim $StringVar4 = '"whatever"'
Dim $StringVar5 = "'whatever again'"

Run(@ComSpec & $StringVar1 & $StringVar2, $StringVar3, @SW_HIDE)
$var = $StringVar4
$var2 = $StringVar5
$var3 = $StringVar3; it's already been found, no need to make another variable

I've been at it for a little while now, and maybe I'm just making it more difficult thant what it needs to be, just wondering if someone might have a working example on it or something similar. I don't have even a semi working example to post... But if I get one working somewhat, I'll post it.

what exactly are you trying to do? I mean, could you elaborate a little? i have a few string manipulation functions i've written for myself, but i'm not sure if they'd help solve your problem
Link to comment
Share on other sites

  • Moderators

1. Find all quoted strings in within the string

2. Create a Dim Variable of that quoted string

3. Replace original found quoted string with Variable name

Thanks

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.

Link to comment
Share on other sites

1. Find all quoted strings in within the string

2. Create a Dim Variable of that quoted string

3. Replace original found quoted string with Variable name

Thanks

actually... couldn't you just:

$test = FileRead("c:\test.txt",FileGetSize("C:\test.txt"));reads in the file where substitutions are to be made
$test = StringReplace($test,"whatever","$replacement",0);replaces all occurances
FileWrite("C:\output.txt",$test);writes output to new file
Link to comment
Share on other sites

  • Moderators

actually... couldn't you just:

$test = FileRead("c:\test.txt",FileGetSize("C:\test.txt"));reads in the file where substitutions are to be made
$test = StringReplace($test,"whatever","$replacement",0);replaces all occurances
FileWrite("C:\output.txt",$test);writes output to new file
Assuming I know that the information between the quotes off hand, I'm sure.

I'm using _FileReadToArray() seems a bit quicker than FileRead(). Then trying to add the found strings to an array using _ArrayAdd().

There are 4 different variations of quotes to start off the Quote string, it's easy to do, if there are only one set of quotes on one line... But that's the extent that I'm at currently.

$QUOTE_1 = '"' & "'"; beginnings of quotes
$QUOTE_2 = "'" & '"'
$QUOTE_3 = '"'
$QUOTE_4 = "'"

The problem I'm having is getting all the "quoted" strings on a given line.

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.

Link to comment
Share on other sites

Assuming I know that the information between the quotes off hand, I'm sure.

I'm using _FileReadToArray() seems a bit quicker than FileRead(). Then trying to add the found strings to an array using _ArrayAdd().

There are 4 different variations of quotes to start off the Quote string, it's easy to do, if there are only one set of quotes on one line... But that's the extent that I'm at currently.

$QUOTE_1 = '"' & "'"; beginnings of quotes
$QUOTE_2 = "'" & '"'
$QUOTE_3 = '"'
$QUOTE_4 = "'"

The problem I'm having is getting all the "quoted" strings on a given line.

are you sure that _FileReadToArray() is quicker than FileRead() ? I ask because it seems like the opposite would be true... I haven't checked out the code to _FileReadToArray, but i would think (based on the parameters it's run with) that it reads in the file with fileread() and then essentially does a stringsplit() on the stored contents... If that's the way that it is, it's taking longer because it's doing the same thing, then more...

anyway... i have a function that may help...

Func _StringSubLoc($ack, $blah)
    $bl = StringLen($blah)
    $al = StringLen($ack)
    Dim $occ[1]
    $occ[0] = 0
    If StringInStr($ack, $blah) Then
        For $x = 1 To $al - $bl
            If StringMid($ack, $x, $bl) = $blah Then
                $occ[0] = $occ[0] + 1
                _ArrayAdd($occ, $x + 1)
            EndIf
        Next
    Else
        Return (-1)
        Exit
    EndIf
    Return ($occ)
EndFunc  ;

if you read in the file the same way as my last example, then use run that function passing your double quotes as the substring, it will return an array, with 0 being the number of occurances and each successive index being the location in the string of the corresponding occurance. You could then loop through that array, grabbing the values between paired quotes and replacing them with whatever you want. ex:

If Mod($returnedvalue,2) <> 0 Then 
    MsgBox(0,"No","Uneven number of quotes.  exiting")
    Exit
EndIf
For $x = 1 to $returnedvalue[0] Step 2
    $FileContents = StringLeft($FileContents,$returnedvalue[$x]-1) & "$replacement" & StringRight($FileContents,StringLen($FileContents) - $returnedvalue[$x+1])
Next
Link to comment
Share on other sites

  • Moderators

Hey cameronsdad, thanks alot for the try... Your function would work indeed (tested), if I were only looking for one type of quote or maybe I'm just doing it wrong (wouldn't doubt that :P ).

As I stated before, the quotes could be "'something'" where I would need to extract quoted string "'something'", or the quote string could be '"something"' again extracting the whole string or "something" or 'something'.

I'm still messing around with it a bit, just when I have one set, another set throws me for a loop << No Pun. But thanks again!! :lmao:

Edit:

Well, I got success... and oops forgot the strings could be something like: ToolTip('This is a "Test" : ', 0, 0) Needing the variable to be: $Var_1 = 'This is a "Test" : ' , and the new written line to be: ToolTip($Var_1, 0, 0)

Aww well, I had never played with strings in an outside source before, this is actually a good learning experience for me... If I get it before I shoot the computer, I'll post what I did for others.

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.

Link to comment
Share on other sites

Hey cameronsdad, thanks alot for the try... Your function would work indeed (tested), if I were only looking for one type of quote or maybe I'm just doing it wrong (wouldn't doubt that :P ).

As I stated before, the quotes could be "'something'" where I would need to extract quoted string "'something'", or the quote string could be '"something"' again extracting the whole string or "something" or 'something'.

I'm still messing around with it a bit, just when I have one set, another set throws me for a loop << No Pun. But thanks again!! :lmao:

Edit:

Well, I got success... and oops forgot the strings could be something like: ToolTip('This is a "Test" : ', 0, 0) Needing the variable to be: $Var_1 = 'This is a "Test" : ' , and the new written line to be: ToolTip($Var_1, 0, 0)

Aww well, I had never played with strings in an outside source before, this is actually a good learning experience for me... If I get it before I shoot the computer, I'll post what I did for others.

actually, i did see that you had multiple types of quotes, i only exampled one, but you could use the diff quote schemes (for the cases where they're together), starting with '" and "' so that the occurances with more than one kind of quote are not broken up by a subsitution of a single... as far as the other scenario you mentioned, that would be grabbed correctly if you used the example with a ' single quote passed as second parameter. if you typically pass double quoted strings inside of single quoted strings, then you'd just run the single quote sub first.... like so:

Func _StringSubLoc($ack, $blah)
    $bl = StringLen($blah)
    $al = StringLen($ack)
    Dim $occ[1]
    $occ[0] = 0
    If StringInStr($ack, $blah) Then
        For $x = 1 To $al - $bl
            If StringMid($ack, $x, $bl) = $blah Then
                $occ[0] = $occ[0] + 1
                _ArrayAdd($occ, $x + 1)
            EndIf
        Next
    Else
        Return (-1)
        Exit
    EndIf
    Return ($occ)
EndFunc ;==>_StringSubLoc

$filecontents = FileRead("c:\test.txt", FileGetSize("c:\test.txt"))
$returnedvalue1 = _StringSubLoc($filecontents, "'"")
$returnedvalue2 = _StringSubLoc($filecontents, '"'')
$returnedvalue3 = _StringSubLoc(($filecontents, "'")
$returnedvalue3 = _StringSubLoc(($filecontents, '"')
For $z = 1 To 4
    
    If Mod(Eval($returnedvalue & $z), 2) <> 0 Then
        MsgBox(0, "No", "Uneven number of quotes.  skipping type " & $z)
        ContinueLoop
    EndIf
    For $x = 1 To Eval($returnedvalue & $z) [0] Step 2
        $filecontents = StringLeft($filecontents, Eval($returnedvalue & $z) [$x] - 1) & "$replacement" & StringRight($filecontents, StringLen($filecontents) - Eval($returnedvalue & $z) [$x + 1])
    Next
Next

***edit*** didnt' mention that this is untested, just kind of threw it together, but hopefully would work.

Edited by cameronsdad
Link to comment
Share on other sites

  • Moderators

$filecontents = FileRead("c:\test.txt", FileGetSize("c:\test.txt"))

$returnedvalue1 = _StringSubLoc($filecontents, "'"") should be >> "'" & '"'

$returnedvalue2 = _StringSubLoc($filecontents, '"'') should be >> '"' & "'"

$returnedvalue3 = _StringSubLoc(($filecontents, "'")

$returnedvalue3 = _StringSubLoc(($filecontents, '"')

For $z = 1 To 4

If Mod(Eval($returnedvalue & $z), 2) <> 0 Then

MsgBox(0, "No", "Uneven number of quotes. skipping type " & $z)

ContinueLoop

EndIf

For $x = 1 To Eval($returnedvalue & $z) [0] Step 2

$filecontents = StringLeft($filecontents, Eval($returnedvalue & $z) [$x] - 1) & "$replacement" & StringRight($filecontents, StringLen($filecontents) - Eval($returnedvalue & $z) [$x + 1])

Next

Next

***edit*** didnt' mention that this is untested, just kind of threw it together, but hopefully would work.

Thanks again for the effort cameronsdad, the red were errors that were thrown, the blue the right replacement, I couldn't figure out how to get anything other than the character position (which I'm assuming was character position) when I tried to correct the $returnedvalue & z) [0], and I had to create a 2 dimensional array to do that even.

I'm thinking your's could be setup as a UDF nicely, if I could have figured out the latter part. But, I got frustrated with working with it, and decided to give it another go on my own.

This seems to work nicely, If you see mistakes or can think of better improvements... feel free :P :

#include <file.au3>
#include <ARRAY.AU3>
Dim $sLine_Array
Dim $sArray_Add[1]
Dim $sz_SSP[1]
Dim $NewCount
Dim $sz_Mark = 0

$timer = TimerInit()

$File = @ProgramFilesDir & "\AutoIt3\SciTe\Tidy\Tidy.au3"
_FileReadToArray($File, $sLine_Array)

For $i = 0 To $sLine_Array[0]
    $sz_SSW = StringStripWS($sLine_Array[$i], 7)
    $sz_Len = StringLen($sz_SSW)
    If StringInStr($sz_SSW, '"') Or StringInStr($sz_SSW, "'") Then
        For $x = 1 To $sz_Len
            $sz_Mid = StringMid($sz_SSW, $x)
            $sz_Left = StringLeft($sz_Mid, 1)
            If StringLeft($sz_Mid, 1) = ";" And $sz_Mark = 0 Then
                ExitLoop
            EndIf
            If $sz_Left = '"' Or $sz_Left = "'" Then
                While 1
                    If $sz_Left = '"' Then
                        If $sz_Mark = 1 Then
                            $NewCount = $x
                            $sz_Mark = 2
                            ExitLoop
                        ElseIf $sz_Mark = 2 Then
                            $NewCount = $x - $NewCount
                            $sz_Replace = StringLeft(StringMid($sz_SSW, $x - $NewCount), $NewCount + 1)
                            _ArrayAdd($sArray_Add, $sz_Replace)
                            $sz_Mark = 0
                            ExitLoop
                        Else
                            If $sz_Mark = 0 Then
                                $sz_Mark = 1
                            Else
                                ExitLoop
                            EndIf
                        EndIf
                    Else
                        ExitLoop
                    EndIf
                WEnd
                While 1
                    If $sz_Left = "'" Then
                        If $sz_Mark = 3 Then
                            $NewCount = $x
                            $sz_Mark = 4
                            ExitLoop
                        ElseIf $sz_Mark = 4 Then
                            $NewCount = $x - $NewCount
                            $sz_Replace = StringLeft(StringMid($sz_SSW, $x - $NewCount), $NewCount + 1)
                            _ArrayAdd($sArray_Add, $sz_Replace)
                            $sz_Mark = 0
                            ExitLoop
                        Else
                            If $sz_Mark = 0 Then
                                $sz_Mark = 3
                            Else
                                ExitLoop
                            EndIf
                        EndIf
                    Else
                        ExitLoop
                    EndIf
                WEnd
            EndIf
        Next
    EndIf
    ToolTip("Line: " & Number($i), 0, 0)
Next
ClipPut(TimerDiff($timer) / 1000)
MsgBox(0, "","Number Of Strings RePlaced = " & UBound($sArray_Add) - 1, 3)
_ArrayDisplay($sArray_Add, "Quote Array")

The TimerInit() was for my own benefit, wanting to see how long it took for "Large" files, The MsgBox() before _ArrayDisplay() was also for my own benefit wanting to see how many quoted strings there were.

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.

Link to comment
Share on other sites

Thanks again for the effort cameronsdad, the red were errors that were thrown, the blue the right replacement, I couldn't figure out how to get anything other than the character position (which I'm assuming was character position) when I tried to correct the $returnedvalue & z) [0], and I had to create a 2 dimensional array to do that even.

I'm thinking your's could be setup as a UDF nicely, if I could have figured out the latter part. But, I got frustrated with working with it, and decided to give it another go on my own.

This seems to work nicely, If you see mistakes or can think of better improvements... feel free :P :

#include <file.au3>
#include <ARRAY.AU3>
Dim $sLine_Array
Dim $sArray_Add[1]
Dim $sz_SSP[1]
Dim $NewCount
Dim $sz_Mark = 0

$timer = TimerInit()

$File = @ProgramFilesDir & "\AutoIt3\SciTe\Tidy\Tidy.au3"
_FileReadToArray($File, $sLine_Array)

For $i = 0 To $sLine_Array[0]
    $sz_SSW = StringStripWS($sLine_Array[$i], 7)
    $sz_Len = StringLen($sz_SSW)
    If StringInStr($sz_SSW, '"') Or StringInStr($sz_SSW, "'") Then
        For $x = 1 To $sz_Len
            $sz_Mid = StringMid($sz_SSW, $x)
            $sz_Left = StringLeft($sz_Mid, 1)
            If StringLeft($sz_Mid, 1) = ";" And $sz_Mark = 0 Then
                ExitLoop
            EndIf
            If $sz_Left = '"' Or $sz_Left = "'" Then
                While 1
                    If $sz_Left = '"' Then
                        If $sz_Mark = 1 Then
                            $NewCount = $x
                            $sz_Mark = 2
                            ExitLoop
                        ElseIf $sz_Mark = 2 Then
                            $NewCount = $x - $NewCount
                            $sz_Replace = StringLeft(StringMid($sz_SSW, $x - $NewCount), $NewCount + 1)
                            _ArrayAdd($sArray_Add, $sz_Replace)
                            $sz_Mark = 0
                            ExitLoop
                        Else
                            If $sz_Mark = 0 Then
                                $sz_Mark = 1
                            Else
                                ExitLoop
                            EndIf
                        EndIf
                    Else
                        ExitLoop
                    EndIf
                WEnd
                While 1
                    If $sz_Left = "'" Then
                        If $sz_Mark = 3 Then
                            $NewCount = $x
                            $sz_Mark = 4
                            ExitLoop
                        ElseIf $sz_Mark = 4 Then
                            $NewCount = $x - $NewCount
                            $sz_Replace = StringLeft(StringMid($sz_SSW, $x - $NewCount), $NewCount + 1)
                            _ArrayAdd($sArray_Add, $sz_Replace)
                            $sz_Mark = 0
                            ExitLoop
                        Else
                            If $sz_Mark = 0 Then
                                $sz_Mark = 3
                            Else
                                ExitLoop
                            EndIf
                        EndIf
                    Else
                        ExitLoop
                    EndIf
                WEnd
            EndIf
        Next
    EndIf
    ToolTip("Line: " & Number($i), 0, 0)
Next
ClipPut(TimerDiff($timer) / 1000)
MsgBox(0, "","Number Of Strings RePlaced = " & UBound($sArray_Add) - 1, 3)
_ArrayDisplay($sArray_Add, "Quote Array")

The TimerInit() was for my own benefit, wanting to see how long it took for "Large" files, The MsgBox() before _ArrayDisplay() was also for my own benefit wanting to see how many quoted strings there were.

ack, sorry for the errors, i messed up the parameters for the eval() calls... i'll patch it up at work tomorrow, going to be crashing for the night here though (work in a few hours)
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...