david1337

First find text, Then do this

14 posts in this topic

#1 ·  Posted (edited)

Hi guys

I have 3 txt files, and the script searches them, to find the txt file containing my username.
If found in one of the txt files, an msgbox will tell which txtfile my username was found in. Works fine.

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $Text[3] ;If changed, remember to change multiple places in the script.

$Text[0] = ("01_TextFile")
$Text[1] = ("02_TextFile")
$Text[2] = ("03_TextFile")

Global $DirforTxtFiles = "C:\Temp\"

$username = @UserName    ;pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




; SCRIPT START

 For $i = 0 to 3 - 1

$TextFile = FileOpen($DirforTxtFiles & $Text[$i] & ".txt", 0)
$ReadText = FileRead($TextFile)
FileClose($TextFile)


If StringRegExp($ReadText, $username) Then


Call("MainScript")



EndIf


Next




Func MainScript()

MsgBox(0,"USER FOUND", "In following textfile: " & $Text[$i])

EndFunc

 

 

But now I want the script to do something else, if my username is NOT found in any of the txtfiles
The following example below here doesn't work, since the Else statement is placed inside the For loop, So it will run the Else statement, every time the StringRegExp doesn't find my username in a txtfile.

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $Text[3] ;If changed, remember to change multiple places in the script.

$Text[0] = ("01_TextFile")
$Text[1] = ("02_TextFile")
$Text[2] = ("03_TextFile")

Global $DirforTxtFiles = "C:\Temp\"

$username = @UserName    ;pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/




; SCRIPT START

 For $i = 0 to 3 - 1

$TextFile = FileOpen($DirforTxtFiles & $Text[$i] & ".txt", 0)
$ReadText = FileRead($TextFile)
FileClose($TextFile)


If StringRegExp($ReadText, $username) Then


Call("MainScript")


Else
Call("NotInTextFile")


EndIf


Next






Func MainScript()

MsgBox(0,"USER FOUND", "In following textfile: " & $Text[$i])

EndFunc





;\/___________________________________________________________________________\/
Func NotInTextFile()

MsgBox(0,"", "USER NOT IN ANY TEXTFILE")

EndFunc

 

What it SHOULD do, is search the txtfiles for my username, and then when all the searching is done, it should react based on the search result.

The answer might be quite obvious, but I'm stuck at this point.

 

Any ideas would be much appreciated :)

Edited by david1337

Share this post


Link to post
Share on other sites



I would try something like this:

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $aText[] = ["01_TextFile", "02_TextFile", "03_TextFile"]
Global $sDirforTxtFiles = "C:\Temp\"
$sUsername = @UserName ; Pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

; SCRIPT START
For $i = 0 To UBound($aText) - 1
    $hTextFile = FileOpen($sDirforTxtFiles & $aText[$i] & ".txt", 0)
    $sReadText = FileRead($hTextFile)
    FileClose($hTextFile)
    If StringRegExp($sReadText, $sUsername) Then
        MainScript()
    Else
        NotInTextFile()
    EndIf
Next

Func MainScript()
EndFunc   ;==>MainScript

Func NotInTextFile()
EndFunc   ;==>NotInTextFile

 


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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi water

Thanks for your time!
I'm afraid that your example gives the same result.
Say my username is found in 03_Textfile.txt, the 2 first searches gives me the Else function, and then the if function
So in your example I get the MsgBox(0,"", "USER NOT IN ANY TEXTFILE") 2 times before the MsgBox(0,"USER FOUND"...

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $aText[] = ["01_TextFile", "02_TextFile", "03_TextFile"]
Global $sDirforTxtFiles = "C:\Temp\"
$sUsername = @UserName ; Pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

; SCRIPT START
For $i = 0 To UBound($aText) - 1
    $hTextFile = FileOpen($sDirforTxtFiles & $aText[$i] & ".txt", 0)
    $sReadText = FileRead($hTextFile)
    FileClose($hTextFile)
    If StringRegExp($sReadText, $sUsername) Then
        MainScript()
    Else
        NotInTextFile()
    EndIf
Next

Func MainScript()
   MsgBox(0,"USER FOUND", "In following textfile: " & $aText[$i])
EndFunc   ;==>MainScript

Func NotInTextFile()
   MsgBox(0,"", "USER NOT IN ANY TEXTFILE")
EndFunc   ;==>NotInTextFile

 

Edited by david1337

Share this post


Link to post
Share on other sites

Then I would set a flag and call the Not-Found-Function after all files have been checked:

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $aText[] = ["01_TextFile", "02_TextFile", "03_TextFile"]
Global $sDirforTxtFiles = "C:\Temp\"
Global $bFound = False
$sUsername = @UserName ; Pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

; SCRIPT START
For $i = 0 To UBound($aText) - 1
    $hTextFile = FileOpen($sDirforTxtFiles & $aText[$i] & ".txt", 0)
    $sReadText = FileRead($hTextFile)
    FileClose($hTextFile)
    If StringRegExp($sReadText, $sUsername) Then
        $bFound = True
        MainScript()
    EndIf
Next
If $bFound = False Then NotInTextFile()

Func MainScript()
    MsgBox(0, "USER FOUND", "In following textfile: " & $aText[$i])
EndFunc   ;==>MainScript

Func NotInTextFile()
    MsgBox(0, "", "USER NOT IN ANY TEXTFILE")
EndFunc   ;==>NotInTextFile

 

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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Another question: If the script finds the username in more than one file do you want to get the MsgBox multiple times?


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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Exactly what I was searching for. The flag did the trick.
Thanks water!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Another question: If the script finds the username in more than one file do you want to get the MsgBox multiple times?

Actually if that is the case, I should still get the MsgBox multiple times yes, but after that it should call a new function ONCE, based on how many times my username is found.
So the script itself must be aware that my username has been found more than once, and react from it. Is that possible also with flags?

Thanks! :)

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $aText[] = ["01_TextFile", "02_TextFile", "03_TextFile"]
Global $sDirforTxtFiles = "C:\Temp\"
Global $bFound = False
$sUsername = @UserName ; Pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

; SCRIPT START
For $i = 0 To UBound($aText) - 1
    $hTextFile = FileOpen($sDirforTxtFiles & $aText[$i] & ".txt", 0)
    $sReadText = FileRead($hTextFile)
    FileClose($hTextFile)
    If StringRegExp($sReadText, $sUsername) Then
        $bFound = True
        MainScript()
    EndIf
Next
If $bFound = False Then NotInTextFile()

Func MainScript()
    MsgBox(0, "USER FOUND", "In following textfile: " & $aText[$i])
    ;If username only found once, do THIS <----------------------------------------------------
    ;If username found more than once, do THAT <-----------------------------------------------
EndFunc   ;==>MainScript

Func NotInTextFile()
    MsgBox(0, "", "USER NOT IN ANY TEXTFILE")
EndFunc   ;==>NotInTextFile

 

Edited by david1337

Share this post


Link to post
Share on other sites

In this case you need a counter:

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $aText[] = ["01_TextFile", "02_TextFile", "03_TextFile"]
Global $sDirforTxtFiles = "C:\Temp\"
Global $iFound = 0
$sUsername = @UserName ; Pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

; SCRIPT START
For $i = 0 To UBound($aText) - 1
    $hTextFile = FileOpen($sDirforTxtFiles & $aText[$i] & ".txt", 0)
    $sReadText = FileRead($hTextFile)
    FileClose($hTextFile)
    If StringRegExp($sReadText, $sUsername) Then
        $iFound = $iFound + 1
        MainScript()
    EndIf
Next
If $iFound = 0 Then NotInTextFile()
If $iFound > 1 Then FoundMultipleTimes()

Func MainScript()
    MsgBox(0, "USER FOUND", "In following textfile: " & $aText[$i])
EndFunc   ;==>MainScript

Func NotInTextFile()
    MsgBox(0, "", "USER NOT IN ANY TEXTFILE")
EndFunc   ;==>NotInTextFile

Func FoundMultipleTimes()
    MsgBox(0, "", "USER FOUND MULTIPLE TIMES")
EndFunc   ;==>FoundMultipleTimes

 

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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Of course! So $iFound = $iFound + 1 adds 1 to the counter every time there is a match, and If $iFound > 1 Then FoundMultipleTimes() determines if that counter is larger than 1, then we have multiple hits.

So logical, so nice :)

water you're the best, thanks!

Edited by david1337

Share this post


Link to post
Share on other sites

Another enhancement could be to do the processing in two steps.

  1. Determine how many files contain the username
  2. Process the files depending on how many files have been found.
    2.1 No files
    2.2. One file
    2.3 Multiple files

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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Yeah you are correct. In the actual setup where there is up to 20 txt files, the current looping process is a bit heavy and clumsy :/

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I suggest something like this:

;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Global $aText[] = ["01_TextFile", "02_TextFile", "03_TextFile"]
Global $aFound[UBound($aText)] ; Table with a flag (True) for each found file
Global $iFound = 0 ; Counter for found files
Global $sDirforTxtFiles = "C:\Temp\"
$sUsername = @UserName ; Pulls username that is logged in
;\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/

; SCRIPT START
For $i = 0 To UBound($aText) - 1
    $hTextFile = FileOpen($sDirforTxtFiles & $aText[$i] & ".txt", 0)
    $sReadText = FileRead($hTextFile)
    FileClose($hTextFile)
    If StringRegExp($sReadText, $sUsername) Then
        $iFound = $iFound + 1 ; Update counter
        $aFound[$i] = True ; Set found flag for this file
    EndIf
Next
If $iFound = 0 Then FoundNone() ; No file found
If $iFound = 1 Then FoundOne()  ; One file found  
If $iFound > 1 Then FoundMany() ; More than one file foun

Func FoundNone()
    MsgBox(0, "", "USER NOT IN ANY TEXTFILE")
EndFunc   ;==>FoundNone

Func FoundOne()
    MsgBox(0, "USER FOUND", "In following textfile: " & $aText[$i])
EndFunc   ;==>FoundOne

Func FoundMany()
    Local $sFoundFiles
    For $i = 1 To UBound($aFound) - 1
        If $aFound[$i] = True Then $sFoundFiles = $sFoundFiles & $aText[$i] & @CRLF
    Next
    MsgBox(0, "", "USER FOUND IN " & $iFound & " FILES:" & @CRLF & @CRLF & $sFoundFiles)
EndFunc   ;==>FoundMany

 

Edited by water
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
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Hi water

I just rewrote the entire script based on your example above. It's a lot more complicated in the actual script, but finally got it working.. phew!

Once again, thanks for all your guidance!

David

Edited by david1337

Share this post


Link to post
Share on other sites

Glad to be of service :)


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
Tutorials:
ADO - Wiki

 

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

    • jjohn
      By jjohn
      Hi all,
      I have the following code, basically, it populates an array with unique characters found in a string, but it turns out only one character is in the array, i don't want to think it is due to a bug of autoit at this point, so i need your help on why, please
      Dim $sx[0] Dim $cn $ss = "Xx" ConsoleWrite(StringLeft($ss, 1) & " " & StringRight($ss, 1) & @CRLF) ConsoleWrite(StringLeft($ss, 1) = StringRight($ss, 1) & @CRLF) ;here show X <> x ConsoleWrite(@CRLF) ;the following is to populate array $sx with different unique characters in $ss For $b = 1 To StringLen($ss) $flg = 0 For $a = 0 To UBound($sx) - 1 ;if asc(StringMid($ss,$b,1)) = asc($sx[$a]) Then ;if this line is used instead of the next, everything is fine If StringMid($ss, $b, 1) = $sx[$a] Then If StringMid($ss, $b, 1) = "x" Then ConsoleWrite("<<->>" & StringMid($ss, $b, 1) & " " & $sx[$a] & @CRLF) $flg = 1 ExitLoop EndIf Next If $flg = 0 Then $cn = $cn + 1 ReDim $sx[$cn] $sx[$cn - 1] = StringMid($ss, $b, 1) EndIf Next ConsoleWrite(UBound($sx) & @CRLF);list the ubound of $sx ;list what is in array $sx For $a = 0 To UBound($sx) - 1 ConsoleWrite($sx[$a] & @CRLF) Next same code in a file is attached as uniqueChar2Array.au3
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening
      I was looking around the forum if I could find a function that allows to convert a string into a 2 dimensional array...
      The first column of the array is always the same, but the rows could change...
      I have a pattern like:

      Column A|Column B
      Static Text 1|Data 1
      Static Text 2|Data 2
      Static Text 3|Data 3
      Static Text 4|Data 4

      Where, Static Text (1...4) will be always the same, and I don't want to change them... But, Data 1...4 are dynamic fields... So, I could have the pattern above AND I coould have the pattern I'm going to show you right below

      Column A|Column B
      Static Text 1|Data 1
      Static Text 2|Data 2
      Static Text 3|Data 3
      Static Text 4|Data 4
      Static Text 1|Data 5
      Static Text 2|Data 6
      Static Text 3|Data 7
      Static Text 4|Data 8

      How can I do in this case?

      Thanks for everyone's help


       
    • ur
      By ur
      I have a string with comma seperated as below.
      Name="Test-win10x64,Test-win10x65,Test-win10x67"
      $machine_names = StringSplit($tempINIValue, ',', $STR_ENTIRESPLIT) with the above line i can get a single dimension array.
      But I want a tabular format array like 4X4 where I want to add the 4 single dimension arrays as different columns in them.
       
      Is there any option to do the same.
    • 232showtime
      By 232showtime
      im getting strange output in array display,$split_[1] is not properly aligned to other arrays and why guictrlsetdata is not writting any data if i put comma(,) at the end of the text in $Input1???
       
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> #Region ### START Koda GUI section ### Form=C:\Users\user\Desktop\Script\StringSplit.kxf $Form1 = GUICreate("Form1", 623, 449, 192, 114) $Input1 = GUICtrlCreateInput("50UGITQ421X, 50UGITQ422X, 50UGITQ423X, 50UGITQ427X, 50UGITQ431X, 50UGITQ435X, 50UGITQ436X, 50UGITQ437X, 50UGITQ441X, 50UGITQ445X, 50UGITQ449X, 50UGITQ453X, 50UGITQ454X, 50UGITQ455X, 50UGITQ459X", 24, 16, 553, 21) $Split = GUICtrlCreateButton("Split", 24, 48, 75, 25) $List1 = GUICtrlCreateList("", 24, 96, 553, 97) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Split $read = GUICtrlRead($Input1) $split_ = StringSplit($read, ",") $Max = UBound($split_, 1) For $i = 1 To UBound($split_) - 1 ConsoleWrite($split_[$i] & @CRLF) ;~ ControlSetText($Form1, "", $List1, $split_[$i]) GUICtrlSetData($List1, $split_[$i]) ;~ GUICtrlSetData($List1, $split_[$i]) ;~ GUICtrlSetData($List1, $i) Next _ArrayDisplay($split_) EndSwitch WEnd  
    • hcI
      By hcI
      Hey !
      I was looking for an array display to see the result of StringSplit but when i called the <Array.au3>, an error is showing up when i try to launch the script.
      I took a screenshoot of the windows dialog box error : imp.bmp
      And when I look into the Array.au3 there is no error :
      Why is it doing this ?