1

I am trying to search a directory structure, and files for all instances of where a pattern exists. Than I want that file location recorded in a log file that I can review latter. I looked at various posts, but I have not found a similar example where this is happening. Reviewed posts include:

  1. PowerShell Scripting - Get-ChildItem
  2. Search List for unique pattern
  3. Search directory and sub-directories for pattern in a file
  4. Use an Easy PowerShell Command to Search Files for Information
  5. Get full path of the files in PowerShell

Here is the code I am using to recuse through the folder structure:

#Set variables for paths
$Results = "C:\Results"
$Source = "C:\Test\*"
$Destination = "C:\MyTest\"

#Create file name for each report with date and time of run
$ReportDate = (Get-Date).ToString("dd-MM-yyyy-hh-mm-ss")

$CustomPattern = Read-Host 'What pattern are you looing for?'
$CustomPatternLog = New-Item -itemType File -Path C:\Results -Name $("CustomerPattern_" + $ReportDate + ".txt")
$CustomPattern = foreach ($file in Get-ChildItem -Path $Destination -Recurse | Select-String -pattern $CustomPattern | Select-Object -Unique Path) {$file.path}
$CustomPattern > "$($Results)\$($CustomPatternLog)"

However, this code is returning the following error:

Get-ChildItem : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. At line:19 char:36 + $CustomPattern = foreach ($file in Get-ChildItem -Path $Destination -Recurse | S ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ReadError: (C:\Test\Mor...ofiles\Customer:St ring) [Get-ChildItem], PathTooLongException + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChil dItemCommand

Do you have a better way to do the same operation?

Community
  • 1
  • 1
RadFox
  • 419
  • 1
  • 4
  • 17

1 Answers1

2

replace this

$CustomPatternLog = New-Item -itemType File -Path C:\Results -Name $("CustomerPattern_" + $ReportDate + ".txt")
$CustomPattern = foreach ($file in Get-ChildItem -Path $Destination -Recurse | Select-String -pattern $CustomPattern | Select-Object -Unique Path) {$file.path}
$CustomPattern > "$($Results)\$($CustomPatternLog)"

with this

$files = Get-ChildItem -Path $Destination -Recurse

#in case you would need the path replace FullName with PsParentPath
$result = ($files | ?{$_.name -like "*$CustomPattern*"}).FullName 
$result | out-file ($CustomPattern + "_" + $ReportDate + ".txt")

and since its shell you can do the same with one liner

(Get-ChildItem -Path $Destination -Recurse | ?{$_.name -like "*$CustomPattern*"}).FullName | out-file ($CustomPattern + "_" + $ReportDate + ".txt")
pandemic
  • 1,135
  • 1
  • 22
  • 39
  • I am using the suggested code, but logs are completely empty. I am searching for the word 'false' with plenty of files with that word, but no results reported back. Any idea? – RadFox Mar 23 '17 at 17:07
  • tested it again and the both codes are working for me. Can you share your code and the output of cmdlet Get-ChildItem? – pandemic Mar 23 '17 at 20:55
  • I figured out the problem. $Destination was set to C:\MyTest and once I changed it to C:\MyTest\*.* all my errors went away with your code. Thank you so very much for your help and updated code suggestion. – RadFox Mar 23 '17 at 20:59
  • huh, it should not be that reason. Path can be set as a simple path without wildcards. The wildcard is replaced by `-recurse`. Did again a test just to be sure. – pandemic Mar 23 '17 at 21:05