Skip to content

Instantly share code, notes, and snippets.

@Newlifer
Last active August 29, 2015 14:05
Show Gist options
  • Save Newlifer/aff564ef27a2342bd0fa to your computer and use it in GitHub Desktop.
Save Newlifer/aff564ef27a2342bd0fa to your computer and use it in GitHub Desktop.
param(
$place = $PSScriptRoot,
$what
)
$regexPattern = "\\"
if($caseSensitive -eq $false) { $regexPattern = "(?i)$regexPattern" }
$regex = New-Object System.Text.RegularExpressions.Regex $regexPattern
# Function for highlighting pattern in the line.
function Write-HostAndHighlightPattern( [ string ]$inputText )
{
$index = 0
while( $index -lt $inputText.Length )
{
$match = $regex.Match($inputText, $index)
if( $match.Success -and $match.Length -gt 0 )
{
Write-Host $inputText.SubString( $index, $match.Index - $index ) -nonewline
Write-Host $match.Value.ToString() -ForegroundColor Red -nonewline
$index = $match.Index + $match.Length
}
else
{
Write-Host $inputText.SubString( $index ) -nonewline
$index = $inputText.Length
}
}
}
$time1 = Get-Date -format HH:mm:ss
[array]$list = Get-ChildItem $place -recurse | %{ Write-Host "Examining file:" | Write-HostAndHighlightPattern $_.fullname; $_ } | ? { $_.psiscontainer -eq $false } | ?{ gc $_.pspath | select-string -pattern $what } | %{ $_.FullName }
$time2 = Get-Date -format HH:mm:ss
$TimeDiff = New-TimeSpan $time1 $time2
if( $TimeDiff.Seconds -lt 0 ) {
$Hrs = ( $TimeDiff.Hours ) + 23
$Mins = ( $TimeDiff.Minutes ) + 59
$Secs = ( $TimeDiff.Seconds ) + 59
}
else {
$Hrs = $TimeDiff.Hours
$Mins = $TimeDiff.Minutes
$Secs = $TimeDiff.Seconds
}
$Difference = '{0:00}:{1:00}:{2:00}' -f $Hrs, $Mins, $Secs
Write-Host "Time elapsed: " $Difference
Write-Host "Files found: " $list.Length
foreach( $file in $list ) {
Write-HostAndHighlightPattern $file
}
Write-Host "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment