Jump to content

Find text in files from values in excel


Recommended Posts

I have a folder with a bunch of files (basically JBoss log files). I want to check if the username in the Excel that I have exists in the log file. I tried using various methods like putting a split or searching for a delimiter but the results aren't as efficient as I would like them to be.

 

This is my current code:

 

Global $open=FileSelectFolder("Select Folder","")
    ;$sFolder = ControlGetText("Automation", "", "Edit1")
    Global $FileList = _FileListToArrayRec($open, "*.*",1,1,1,2)

        If @error = 1 Then
            MsgBox(0, "", "No Folders Found.")
            Exit
        EndIf
        If @error = 4 Then
            MsgBox(0, "", "No Files Found.")
            Exit
        EndIf
        
        FileReadToArray($FileList)
        
        For $i = 1 To $FileList[0]
            StringSplit($FileList[$i]," ")
        Next

I want to try using ExcelRangeRead to match it up with the list of users but I'm not sure how.

 

Any help is appreciated.

Edited by pranaynanda
Link to comment
Share on other sites

1 minute ago, pranaynanda said:

I want to check if the username in the Excel that I have exists in the log file

Is the username a part of the filename or of the log files content?

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

 

Link to comment
Share on other sites

Q&D:

#include <File.au3>

Global $aUserNames[] = ["User1", "User2"] ; Usernames need to be read from Excel
Global $sFolderPath = FileSelectFolder("Select Folder", "")
Global $aFileList = _FileListToArrayRec($sFolderPath, "*.*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
If @error = 1 Then Exit MsgBox(0, "", "No Folders Found.")
If @error = 4 Then Exit MsgBox(0, "", "No Files Found.")

For $i = 1 To $aFileList[0]
    $sFileContent = FileRead($aFileList[$i])
    For $j = 0 To UBound($aUserNames) - 1
        If StringInStr($sFileContent, $aUserNames[$j]) Then MsgBox(0, "Info", "User " & $aUserNames[$j] & " found in file " & $aFileList[$i])
    Next
Next

A regular expression might be faster to find all occurrences of user names in the string.

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

 

Link to comment
Share on other sites

Regular Expression is a tool to process strings. So with the correct PCRE all users could be searched in one go and would replace processing the whole string for each user with StringInStr.

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

 

Link to comment
Share on other sites

Using a RegEx :

#include <File.au3>

Global $aUserNames[] = ["User1", "User2"] ; Usernames need to be read from Excel
Global $sFolderPath = FileSelectFolder("Select Folder", "")
Global $aFileList = _FileListToArrayRec($sFolderPath, "*.*", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
If @error = 1 Then Exit MsgBox(0, "", "No Folders Found.")
If @error = 4 Then Exit MsgBox(0, "", "No Files Found.")

Local $sRegEx = "(?i)"
For $i = 0 To UBound($aUserNames) - 1
    $sRegEx &= "\b" & $aUserNames[$i] & "\b|"
Next
$sRegEx = StringTrimRight($sRegEx, 1)

For $i = 1 To $aFileList[0]
    $sFileContent = FileRead($aFileList[$i])
    If StringRegExp($sFileContent, $sRegEx) Then MsgBox(0, "Info", "One of more users found in file " & $aFileList[$i])
Next

 

Link to comment
Share on other sites

12 minutes ago, pranaynanda said:

but I right now I'm unable to comprehend the concept of Regular Expressions here altogether.

Welcome to the club :)

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

 

Link to comment
Share on other sites

Link to comment
Share on other sites

4 hours ago, junkew said:

why use excel and autoit?

Because it's just not one string, it's a whole column in Excel that needs to be searched through multiple files in multiple separate folders. I can manage doing it manually for each folder but I'd rather have automated the other part.

Link to comment
Share on other sites

FileRead tries to read the whole file into a single variable. A variable is limited to 2GB (according tot he help file: MAX_STRINGLEN = 2,147,483,647 Maximum string length.)
Seems you need another approach. Either read the file in chunks or use one of the suggested tools.

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

 

Link to comment
Share on other sites

2 minutes ago, water said:

A variable is limited to 2GB (according tot he help file: MAX_STRINGLEN = 2,147,483,647 Maximum string length.)

There's some answer that I have been looking for. I checked the folders and the largest file is sized approximately around 800 MB which I think according to what you shared should make it work. But it does not? I believe it was very stupid of me to concatenate all those files into a single file around around 5.8 GBs.

Link to comment
Share on other sites

You might run into problems even if the variable should hold less than 2GB. The allocated storage for a variable needs to be contiguous.

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

 

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