问题描述
调用此 powershell 命令并出现错误.快把我逼疯了.
Calling this powershell command and getting an error. Driving me nuts.
Prompt> get-childitem -recurse ./ *NYCSCA* | where-object { $_.Name -like
"*NYCSCA*" } | rename-item $_ -newname $_.Name.Replace(" ","_") -whatif
回复如下:
You cannot call a method on a null-valued expression.
At line:1 char:140
+ get-childitem -recurse ./ *NYCSCA* | where-object { $_.Name -like "*NYCSCA*" } | select FullName | rename-item $_ -n
ewname $_.Name.Replace <<<< (" ","_") -whatif
+ CategoryInfo : InvalidOperation: (Replace:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
如果我删除最后一部分,我会得到一个文件列表.有什么线索吗?显然,我还没有使用过 powershell.
If I remove the last part, I get a list of files. Any clues? I have not grocked powershell yet, obviously.
注意:我试图将此发布给超级用户,但该网站现在一直在失败 - 不会让我添加这个确切的问题.
Note: I tried to post this to superuser, but the site is consistently failing now - won't let me add this exact question.
这里大大简化了.我什至无法让这个经典示例发挥作用.
Here it is greatly simplified. I cannot even get this classic example to work.
gci *NYCSCA* | ren $_ ($_.Name).Replace("foo","bar")
谢谢@JNK,% 做到了.如果您有兴趣,我需要的解决方案是:
Thank you @JNK, the % did it. The solution I needed is this, in case you're interested:
gci -recurse | where-object{ $_.Name -like "*NYCSCA*"} | %{rename-item $_.FullName $_.FullName.Replace("NYCSCA","SDUSD") }
推荐答案
我认为你需要 foreach-object:
I think you need foreach-object:
get-childitem -recurse ./ *NYCSCA* | where-object { $_.Name -like
"*NYCSCA*" } | % {rename-item $_ -newname $_.Name.Replace(" ","_") -whatif}
无法将管道数组重命名为集合.
The piped array can't be renamed as a set.
这篇关于在Powershell中递归重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!