Jump to content

[HELP] Find "something" after a given string


Recommended Posts

Hello,

I have a piece of code that searches a specific part of a string from a file.

Case $msg = $btnBase
$rFile = FileRead ( $data )
MsgBox(1,"2", $rFile)
$Line = StringInStr ( $rFile, 'WebAppServerSession.Initialize("Electos.Websites.',1 )
MsgBox(1,"3", $Line & " Error:" &@error)

While the complete string in this case would be WebAppServerSession.Initialize("Electos.Websites.testsite")

And I need to put that testsite into a variable, but I don't know the exact name yet if you know what I mean.

Full script so far down here:

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Fileversion=1.2.0.20
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=highestAvailable
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#Include <EditConstants.au3>
#include <vshadowlib.au3>
#include <ProgressConstants.au3>
#include <Constants.au3>
#include <FTPEx.au3>
#include <Mail.au3>

$Author = "by Yoshi"
$sConfigIni = @ScriptDir & "\config.ini"
$sSourceStartFolder = IniRead($sConfigIni, "settings", "SourceStartFolder", "")
$sDestination = IniRead($sConfigIni, "settings", "Destination", "")
$ZipExe = IniRead($sConfigIni, "settings", "ZipExe", "")
$sArguments = IniRead($sConfigIni, "settings", "Arguments", "")
$sArchExt = IniRead($sConfigIni, "settings", "ArchExt", "")
$sHost = IniRead($sConfigIni, "FTP", "Host", "")
$sDate = @YEAR & "-" & @MON & "-" & @MDAY
$sRemoteApp = IniRead($sConfigIni, "settings", "RemoteApp", False)
;Max voor progress bar
const $max = 8

#region GUI
$main = GUICreate("Electos Site Indexing Tool Helper", 360, 100)
GUICtrlCreateLabel("File:", 10, 20)
GUICtrlCreateLabel($Author, 308, 80)
$tbxSource= GUICtrlCreateInput('', 70, 16, 250)
$btnSource = GUICtrlCreateButton("...", 320, 15,30,23)

$btnBase = GUICtrlCreateButton("Base", 9, 50, 170, 25)
$btnManual = GUICtrlCreateButton("Manual", 180, 50, 170, 25)
#endregion

GUICtrlSetState($tbxSource, $GUI_DISABLE)

_GUI()
Func _GUI()
    GUISetState(@SW_SHOW, $main)
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
;~             Case $msg = $btnManual
;~                 If $sRemoteApp = True And $CmdLine[0] = 0 Then
;~                     If MsgBox(4, "Program will logoff", "Can I log off from " & @ComputerName & "?") = 6 Then
;~                         RunWait('logoff.exe')
;~                     EndIf
;~                 EndIf
;~                 Exit
            Case $msg = $btnSource
                ;Show open dialog to let the user choose a source directory
;~                 $data = FileSelectFolder("Select a Global.ASA file.", $sSourceStartFolder, 2, "", $main)
                $data = FileOpenDialog ("Browse for Folder", $sSourceStartFolder & "\", "ASA files (*.ASA)", 1 + 2)
                MsgBox(1,"1", $data)

                ;If the user selected a folder
                If $data = $sSourceStartFolder Then
                    MsgBox(16, 'Error', 'You can not zip the websites folder.')
                ElseIf @error = 0 then
                    ;Set the source directory
                    GUICtrlSetData($tbxSource,$data) ; set tbxSource to $data
                EndIf
            Case $msg = $btnBase
                $rFile = FileRead ( $data )
                MsgBox(1,"2", $rFile)
                $Line = StringInStr ( $rFile, 'WebAppServerSession.Initialize("Electos.Websites.',1 )
                MsgBox(1,"3", $Line & " Error:" &@error)

        EndSelect
    WEnd
EndFunc

Any help is appreciated quite a lot!

Link to comment
Share on other sites

You could use a regular expression with function StringRegExpReplace or pure string manipulation functions. Example:

$sString = 'WebAppServerSession.Initialize("Electos.Websites.testsite")'
$sSearch = 'Electos.Websites.'
$iPos = StringInStr($sString, $sSearch)
If $iPos > 0 Then
    $sResult = StringMid($sString, $iPos + StringLen($sSearch)) ; Copy rest of string
    $sResult = StringReplace($sResult, ')', '') ; Remove )
    $sResult = StringReplace($sResult, '"', '') ; Remove "
    ConsoleWrite($sResult & @LF)
Endif

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

You could use a regular expression with function StringRegExpReplace or pure string manipulation functions. Example:

$sString = 'WebAppServerSession.Initialize("Electos.Websites.testsite")'
$sSearch = 'Electos.Websites.'
$iPos = StringInStr($sString, $sSearch)
If $iPos > 0 Then
$sResult = StringMid($sString, $iPos + StringLen($sSearch)) ; Copy rest of string
$sResult = StringReplace($sResult, ')', '') ; Remove )
$sResult = StringReplace($sResult, '"', '') ; Remove "
ConsoleWrite($sResult & @LF)
Endif

Thanks for answering so quickly, but I cannot put this:

$sString = 'WebAppServerSession.Initialize("Electos.Websites.testsite")' into a variable because the last name is not always the same. It's a name of a site. and every Global.ASA has a different name there. (Global.ASA is the file that we opened with FileOpen.

You could use a regular expression with function StringRegExpReplace

I could try that.
Link to comment
Share on other sites

$sString = 'WebAppServerSession.Initialize("Electos.Websites.testsite")' into a variable because the last name is not always the same. It's a name of a site. and every Global.ASA has a different name there. (Global.ASA is the file that we opened with FileOpen.

That's just a simple reproducer example.

In your script you read the content of the file into a variable or array. Use this as input for the StringInStr function.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Although Water's approach is the best, I used a UDF function here:

#include <String.au3>

;I created a file first containing your string (not a variable)

FileWrite('url-list.txt', 'WebAppServerSession.Initialize("Electos.Websites.testsite")')
FileWrite('url-list.txt', 'WebAppServerSession.Initialize("Electos.Websites.www.google.com")')

;Now I will find the website name in that string

$YourVariable = _StringBetween(FileRead('url-list.txt'), 'WebAppServerSession.Initialize("Electos.Websites.', '")')
For $i = 0 to UBound($YourVariable)-1
msgbox(64, "", $YourVariable[$i])
Next

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

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...