Here is my code and the error I am getting. How do I avoid the null value error by testing for a .pdf file prior to the rename?
Error:You cannot call a method on a null-valued expression.
At C:\Users\casj03\Desktop\PDF_MOVE_COMPRESSION.ps1:21 char:45
+ $zipfile = $name.Replace <<<< (".pdf",".zip")
+ CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
$filePath = "\\jcc.jccsystems.com\omafiles\SPC-ProductionDatabases\Mailstream\Archive"
$pdf = Get-ChildItem -Recurse -Path $filePath | Where-Object { $_.Extension -eq ".pdf" }
########### END of VARABLES
foreach ($file in $pdf) {
$name = $file.name
$directory = $file.DirectoryName
$zipfile = $name.Replace(".pdf",".zip")
sz a -tzip "$directory\$zipfile" "$directory\$name"
}