-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindString.ps1
More file actions
62 lines (49 loc) · 2.09 KB
/
FindString.ps1
File metadata and controls
62 lines (49 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<#
.SYNOPSIS
Searches for a specific text string inside files within a directory tree.
.DESCRIPTION
This script recursively searches for files containing a specific string.
It returns the file paths of the matches. It uses case-sensitive search.
.PARAMETER SearchTerm
The string you want to find inside the files. (Required)
.PARAMETER Path
The root directory to start the search. Defaults to the current directory (.).
.PARAMETER Filter
File extension filter (e.g., "*.mqh", "*.mq4"). Defaults to "*" (all files).
.EXAMPLE
# Run from script file:
.\FindString.ps1 -SearchTerm "ATR_Calculator.mqh"
.EXAMPLE
# Search only in specific file types:
.\FindString.ps1 -SearchTerm "ATR_Calculator.mqh" -Filter "*.mq*"
.EXAMPLE
# One-liner command (without saving this script):
Get-ChildItem -Recurse -Filter *.mq* | Select-String -Pattern "ATR_Calculator.mqh" -CaseSensitive -List | Select-Object Path
#>
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$SearchTerm,
[string]$Path = ".",
[string]$Filter = "*"
)
# Visual feedback for the user
Write-Host "Searching for: '$SearchTerm' (Case Sensitive) in '$Path'..." -ForegroundColor Cyan
try {
# Performing the search
# -List: Stop after first match in a file (performance optimization)
# -CaseSensitive: Matches exact casing only
# Note: Variable name changed to avoid conflict with automatic variable $matches
$searchResults = Get-ChildItem -Path $Path -Recurse -Filter $Filter -File -ErrorAction SilentlyContinue |
Select-String -Pattern $SearchTerm -CaseSensitive -List
if ($searchResults) {
Write-Host "`nFound matches in the following files:" -ForegroundColor Green
foreach ($item in $searchResults) {
Write-Host $item.Path
}
Write-Host "`nTotal files found: $($searchResults.Count)" -ForegroundColor Gray
} else {
Write-Host "`nNo files found containing '$SearchTerm'." -ForegroundColor Yellow
}
} catch {
Write-Error "An error occurred: $_"
}