Reputation: 83
My code will not display "No files to process" on the screen. It is supposed to count the files in a directory and if there are none then it should display "No files to process" and then exit.
# Function Measure, counts files to see if there are any to process.
Function Measure
{
$Measure = ( Get-ChildItem C:\temp\BDMDump\ | Measure-Object ).Count
If ($Measure = "0")
{Write-Host "No files to process"|Exit}
else
{Write-Host "There are files to process.."}
}
I expect to see "No files to process".
Upvotes: 2
Views: 79
Reputation: 184
There are 4 issues here:
; return
to exit the function. Even this is probably not needed if the function doesn't do anything else.Additionally, you could remove | Measure-Object
because the object System.IO.FileInfo which is returned by Get-ChildItem already has a "Count" method.
Here's a revised copy of your code with all the changes:
Function Measure-Files {
$Measure = Get-ChildItem "C:\temp\BDMDump\"
If ($Measure.Count -eq 0)
{ Write-Host "No files to process"; return }
else
{ Write-Host "There are files to process.." }
}
Upvotes: 5
Reputation: 107
There are 3 problems
1. Exit
can not be Piped to. If you want to exit the session useExit-PSSession
this will close the window.
2. "Is equal to" should be changed from =
to -eq
3. "0"
should be changed to 0
as it is an integer
If ($Measure -eq 0)
{Write-Host "No files to process"|Exit-PSSession}
else
{Write-Host "There are files to process.."}
Upvotes: 0