Find Files containing specific Text

This process prompts the user to select a folder and a word or a phrase. It then searches every file in the folder for the specific text and shows the results (i.e. the paths of the files that contain the input text).

Display.SelectFolder Description:'Please select the folder whose files you want to search.' \
                    InitialDirectory:'' \
                    IsTopMost:False \
                    SelectedFolder=> SelectedFolder \
                    ButtonPressed=> ButtonPressed

label PromptForText

Display.InputDialog Title:'Please enter the text to search for.' \
                    Message:'Enter the text that you want to locate in the files.' \
                    DefaultValue:'' \
                    InputType:Display.InputType.SingleLine \
                    IsTopMost:False \
                    UserInput=> TextToLocate \
                    ButtonPressed=> ButtonPressed

if TextToLocate="" then
    goto PromptForText
end

Folder.GetFiles Folder: SelectedFolder  \
                    FileFilter:'*' \
                    IncludeSubfolders:True \
                    FailOnAccessDenied:True \
                    SortBy1:Folder.SortBy.NoSort \
                    SortDescending1:False \
                    SortBy2:Folder.SortBy.NoSort \
                    SortDescending2:False \
                    SortBy3:Folder.SortBy.NoSort \
                    SortDescending3:False \
                    Files=> AllFilesInFolder

File.GetTempPath TempFile=> TempFile

File.WriteText File: TempFile  \
                    TextToWrite: 'The text '+TextToLocate+' has been found in the following files: ' \
                    AppendNewLine:True \
                    IfFileExists:File.IfFileExists.Overwrite \
                    Encoding:File.FileEncoding.Unicode

loop foreach item in AllFilesInFolder
    File.ReadText File: item \
                        Encoding:File.TextFileEncoding.UTF8 \
                        Content=> CurrentFileContents

    text.ParseForFirstOccurrence Text:CurrentFileContents  \
                        TextToFind:TextToLocate  \
                        StartingPosition: 0  \
                        IgnoreCase:False \
                        OccurrencePosition=> TextPos
    if TextPos>-1 then
        File.WriteText File:TempFile  \
                        TextToWrite:item  \
                        AppendNewLine:True \
                        IfFileExists:File.IfFileExists.Append \
                        Encoding:File.FileEncoding.Unicode
    end
end

System.RunApplication ApplicationPath:'notepad'  \
                    CommandLineArguments:TempFile \
                    WorkingDirectory:'' \
                    WindowStyle:System.ProcessWindowStyle.Normal \
                    ProcessId=> AppProcessId

Display.ShowMessage Title:'Process Completed!' \
                    Message:'Process "Find Files containing specific Text" completed.' \
                    Icon:Display.Icon.None \
                    Buttons:Display.Buttons.OK \
                    DefaultButton:Display.DefaultButton.Button1 \
                    IsTopMost:False \
                    ButtonPressed=> ButtonPressed

In more detail:


The process prompts the user to enter the text to search for, as well as the folder whose files will be searched. It then retrieves the files of the selected folder and creates a temporary file where the paths of the files containing the desired text will be recorded. Finally, the results are displayed in a notepad file.

Step 1: Prompt the User to Enter a Text and Select a Folder

Prompt the user to select the folders whose files will be searched. Then, ensure that a text to search for has been entered.

Display.SelectFolder Description:'Please select the folder whose files you want to search.' \
                    InitialDirectory:'' \
                    IsTopMost:False \
                    SelectedFolder=> SelectedFolder \
                    ButtonPressed=> ButtonPressed

label PromptForText

Display.InputDialog Title:'Please enter the text to search for.' \
                    Message:'Enter the text that you want to locate in the files.' \
                    DefaultValue:'' \
                    InputType:Display.InputType.SingleLine \
                    IsTopMost:False \
                    UserInput=> TextToLocate \
                    ButtonPressed=> ButtonPressed

if TextToLocate="" then
    goto PromptForText
end
  1. Use the “Display.SelectFolder” action to prompt the user to select a folder.
  2. Use the “Display.InputDialog” action to prompt the user to enter a text to search for and store it on the “TextToLocate” output variable.
  3. To ensure that a text has been entered, the above action can be placed between a label, named “PromptForText”, and an “if” conditional statement that examines whether the “TextToLocate” variable is empty (i.e. if TextToLocate= “”). If so, the automation flow will be redirected to the label, by using the “goto” statement, so that the user will be asked to enter a text again.

Step 2: Retrieve Files and Create a Temporary File

Retrieve the files of the selected folder and create a temporary file where the paths of the files containing the desired text will be recorded.

Folder.GetFiles Folder: SelectedFolder  \
                    FileFilter:'*' \
                    IncludeSubfolders:True \
                    FailOnAccessDenied:True \
                    SortBy1:Folder.SortBy.NoSort \
                    SortDescending1:False \
                    SortBy2:Folder.SortBy.NoSort \
                    SortDescending2:False \
                    SortBy3:Folder.SortBy.NoSort \
                    SortDescending3:False \
                    Files=> AllFilesInFolder

File.GetTempPath TempFile=> TempFile

File.WriteText File: TempFile  \
                    TextToWrite: 'The text '+TextToLocate+' has been found \
                                   in the following files: ' \
                    AppendNewLine:True \
                    IfFileExists:File.IfFileExists.Overwrite \
                    Encoding:File.FileEncoding.Unicode
  1. Use the “Folder.GetFiles” action to retrieve the files of the selected folder and its subfolders, and store them in the “AllFilesInFolder” output variable:
    1. Set the “Folder” argument to “SelectedFolder”, which is an output variable of the “Display.SelectFolder” action.
    2. Set the “IncludeSubfolders” argument to “True”, so that subfolders are also examined.
  2. Use the “File.GetTempPath” action to create a temporary file.
  3. Use the “File.WriteText” action to write an introductory sentence in the temporary file, by setting the “File” argument to “TempFile”.

Step 3: Examine Each File and Display the Results

Examine whether a file contains the desired text and, if so, append the file’s path to the temporary file. Finally, display the results using the Notepad app.

loop foreach item in AllFilesInFolder
    File.ReadText File: item \
                  Encoding:File.TextFileEncoding.UTF8 \
                  Content=> CurrentFileContents

    Text.ParseForFirstOccurrence Text:CurrentFileContents  \
                        TextToFind:TextToLocate  \
                        StartingPosition: 0  \
                        IgnoreCase:False \
                        OccurrencePosition=> TextPos
    
    if TextPosition>-1 then
        File.WriteText File:TempFile  \
                        TextToWrite:item  \
                        AppendNewLine:True \
                        IfFileExists:File.IfFileExists.Append \
                        Encoding:File.FileEncoding.Unicode
    end
end

System.RunApplication ApplicationPath:'notepad'  \
                    CommandLineArguments:TempFile \
                    WorkingDirectory:'' \
                    WindowStyle:System.ProcessWindowStyle.Normal \
                    ProcessId=> AppProcessId

Display.ShowMessage Title:'Process Completed!' \
                    Message:'Process "Find Files containing specific Text" completed.' \
                    Icon:Display.Icon.None \
                    Buttons:Display.Buttons.OK \
                    DefaultButton:Display.DefaultButton.Button1 \
                    IsTopMost:False \
                    ButtonPressed=> ButtonPressed
  1. Use the “loop foreach” statement to repeat the following actions for each file of the selected folder. In each iteration, the current file will be stored in the “item” loop variable. It will be read and parsed.
  2. Use the “File.ReadText” action to read the contents of each file and store them in the “CurrentFileContents” output variable. Set the “File” argument to “item”, which is the loop variable, holding the current file.
  3. Use the “Text.ParseForFirstOccurrence” to parse the content of each file in order to find the first occurrence of the desired text and store it in the “TextPosition” output variable:
    1. Set the “Text” argument to “CurrentFileContents”, which is the output variable of the previous action.
    2. Set the “TextToFind” argument to “TextToLocate” output variable.
    3. Set the “StartingPosition” to 0.

  4. Use the “if” statement to check whether the “TextPosition” output variable of the previous action is greater than -1, which means that the selected text has been located somewhere in the file’s contents.
  5. Use the “File.WriteText” action, inside the conditional block, so that if the condition is met, the file’s path will be appended to the temporary file.
  6. Close the conditional and loop blocks, using the “end” statement.
  7. Display the results through the Notepad app, using the “System.RunApplication” action:
    1. Set the “ApplicationPath” to “notepad”.
    2. Set the “CommandLineArguments” to “TempFile”.
  8. Finally, inform the user when the process is completed, using the “Display.ShowMessage” action.