I have a simple script to rename some files
#cls
cd e:\scripts\br
Get-ChildItem -Filter “13615*.pdf” -Recurse | Rename-Item -NewName {$.name -replace ‘136150’,‘0’ }
Get-ChildItem -Filter “11111*.pdf” -Recurse | Rename-Item -NewName {$.name -replace ‘111110’,‘0’ }
#Write-Output -Recurse | {$.name}
if ( $.name.contains(“-01”) -ne “True” ) {
Get-ChildItem -Filter “*.1.pdf” -Recurse | Rename-Item -NewName {$_.name -replace ‘.1.pdf’,‘-01x.1.pdf’ }
}
Write-Output Done
what i get when i run it is
Done
PS E:\scripts\br> powershell.exe .\renamer.ps1
You cannot call a method on a null-valued expression.
At E:\scripts\br\renamer.ps1:6 char:6
- if ( $_.name.contains(“-01”) -ne “True” ) {
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : InvalidOperation: ( , RuntimeException
- FullyQualifiedErrorId : InvokeMethodOnNull
so clearly $_name.contains(“-01”) isnt working the way I expect…any ideas on where I am going wrong?