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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
oBdA wonderful, just wonderful, when can I be like you? :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.