Powershell count file improvement.

Dear Export

I want to improve the code below, thanks for the effort of oBdA the kindly help every time.

Now I tried to run this script and it's running fine until I bug testing this with different way, in Path input i simply just type c:\wojpijwg, now what Get-ChildItem do is, it starts to search through everything under the c:\, The reason why i know this is, it returns several errors that some folder under c:\windows\system is not permitted, well of course It's windows 10 security for protection of virus or malware, this way I knew that Get-Childitem is searching all the folders below c:\ or maybe it could be the $Index that it tries to Index everything in c:\?

Anyway I would add a function that when I type in the path, the script will first check if the path exist or not, if the path exist  do all the Get-ChildItem if not, jump down to $response.
   
Thanks

Function FindPattern {
Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
)
	$Script:Results = $false
	$AllItems = Get-ChildItem -Path $Path -Recurse -File | Select-Object -Property Fullname, LastWriteTime
	$Index = 0
	$AllItems | ForEach-Object {
		$item = $_
		Write-Progress -Activity "Söker '$($Pattern)'" -Status $item.FullName -PercentComplete ((100 * $Index++) / $AllItems.Count)
		Select-String -Path $item.FullName -Pattern $Pattern |
			Select-Object -Property Path, LineNumber, @{n='LWT'; e={
			$item.LastWriteTime
			$Script:Results = $true
		}}
	If (-not $Script:Results) {
		Write-Host "Cannot find '$($Pattern)' under '$($Path)'"
	}
	Write-Progress -Activity 'Done' -Status 'Done' -Completed
}

Do {
	$Path = Read-Host 'Path'
	$Pattern = Read-Host 'String'
	If (-not [string]::IsNullOrEmpty($Path) -and -not [string]::IsNullOrEmpty($Pattern)) {
		FindPattern -Path $Path -Pattern $Pattern
	}
	$response = Read-Host 'Search again?'
} Until ($response -ne 'Yes')

Open in new window

LVL 1
WeTiAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
This should do the trick.
Note that function names in PowerShell should follow the <Verb>-<Noun> syntax, with <Verb> coming form the list obtained by "Get-Verb".
Function Find-FileByPattern {
Param(
	[Parameter(Position=0, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Path,
	[Parameter(Position=1, Mandatory=$True)][ValidateNotNullOrEmpty()]
	[String]$Pattern
)
	$Script:Results = $false
	$AllItems = Get-ChildItem -Path $Path -Recurse -File | Select-Object -Property Fullname, LastWriteTime
	$Index = 0
	$AllItems | ForEach-Object {
		$item = $_
		Write-Progress -Activity "Searching for '$($Pattern)'" -Status $item.FullName -PercentComplete ((100 * $Index++) / $AllItems.Count)
		Select-String -Path $item.FullName -Pattern $Pattern |
			Select-Object -Property Path, LineNumber, @{n='LWT'; e={
				$item.LastWriteTime
				$Script:Results = $true
			}}
	}
	If (-not $Script:Results) {
		Write-Host "Cannot find '$($Pattern)'"
	}
	Write-Progress -Activity 'Done' -Status 'Done' -Completed
}

Do {
	$Path = Read-Host 'Path'
	If (-not [string]::IsNullOrEmpty($Path)) {
		If (Test-Path -Path $Path) {
			$Pattern = Read-Host 'String'
			If (-not [string]::IsNullOrEmpty($Pattern)) {
				Find-FileByPattern -Path $Path -Pattern $Pattern
				Write-Host "Search done." -ForegroundColor Green
			} Else {
				Write-Host "Pattern may not be empty!" -ForegroundColor Red
			}
		} Else {
			Write-Host "Path '$($Path)' not found!" -ForegroundColor Red
		}
	} Else {
		Write-Host "Path may not be empty!" -ForegroundColor Red
	}
	$response = Read-Host 'Search again?'
} Until ($response -ne 'Yes') 

Open in new window

0
 
WeTiAuthor Commented:
oBdA wonderful, just wonderful, when can I be like you? :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.