Jump to content

Recommended Posts

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
Posted

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 2024-07-28 - Version 1.6.3.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 (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

 

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
Posted

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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

Posted

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 2024-07-28 - Version 1.6.3.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 (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

 

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
Posted

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

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

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
Posted

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 2024-07-28 - Version 1.6.3.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 (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

 

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

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
Posted

Glad to be of service :)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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

 

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
×
×
  • Create New...