Search notes:

ff.ps1

ff.ps1 reports files whose content matches a regular expression in a directory tree or displays the text what was matched by the regular expression (parameter -selectRegex).
Find SQL statements that insert into a table whose name starts with foo:
ff -suffixes *.sql 'insert +into +foo*\w+' -selectRegex
Just list files that contains insert into:
ff 'insert +into'

Source code

#
#   V0.3
#
param (
    [parameter(mandatory=$true) ]
    [string                     ] $regex,
    [parameter(mandatory=$false)]
    [string[]                   ] $suffixes = '*',
    [parameter(mandatory=$false)]
    [string                     ] $root     = $pwd,
    [parameter(mandatory=$false)]
    [switch                     ] $selectRegex
)

set-strictMode -version latest

$errorActionPreference = 'continue'

$files = get-childItem -path $root -attributes !directory -recurse -include $suffixes

if ($selectRegex) {
   foreach ($file in $files) {
      $found = $file | get-content | foreach-object { select-regex $regex $_ }
      if ($found) {
         $file.fullName
         foreach ($f in $found) {
            "  $f"
         }
      }
   }
}
else {
   foreach ($file in ($files | select-string -list $regex)) {
      $file.path
   }
}
Github repository scripts-and-utilities, path: /ff.ps1

History

V0.2 2021-07-17: Add errorAction ignore in get-childItem and select-string so that files that cannot be read don't cause a warning to be written.
V0.3 2021-08-11: Add -selectRegexp (which uses the PowerShell module regex)

See also

Scripts

Index