Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Null value error

$
0
0

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"
                    }




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>