Upon execution of the following code:
$n = 1
$array = "L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9"
ls *.pdf | sort lastwritetime | foreach-object {
if ($_.name.substring(0,2) -cnotin $array) {
ren -newname { "L$global:n " + $_.name -f $global:n++ }
} else {
$global:n++
}}
$n = 0
I encounter the following error:
Rename-Item : Cannot evaluate parameter 'NewName' because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input. At line:3 char:14
- ren -newname { "L$global:n " + $_.name -f $global:n++ }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : MetadataError: (:) [Rename-Item], ParameterBindingException
- FullyQualifiedErrorId : ScriptBlockArgumentNoInput,Microsoft.PowerShell.Commands.RenameItemCommand
Where am I going wrong? The pipeline has the name of a file which is in the current directory but still says it receives no input.
ren.$_ |ren -newname { ...}Get-ChildItem(ls) finds more files? If$nreaches 10, it will also matchL1...$_ |just beforeren, exactly as shown in my previous comment