[英]Rename-Item : Source and destination path must be different error in Powershell
I am using Powershell and trying to return the child item of a directory, which happens to be a subdirectory, and then use the Rename-Item
cmdlet to rename the subdirectory name to something else.我正在使用 Powershell 并尝试返回目录的子项,该目录恰好是子目录,然后使用Rename-Item
cmdlet 将子目录名称重命名为其他名称。
I feel like the following code should work:我觉得下面的代码应该可以工作:
Get-ChildItem "C:\Users\Admin\Desktop\mydirectory\subdirectory" | Rename-Item -NewName {$_.Name -replace 'test'}
But I am getting this error:但我收到此错误:
Rename-Item: Source and destination path must be different.
What am I missing here?我在这里想念什么? Thanks in advance!提前致谢!
Since you're using Get-ChildItem
without limiting the result to files (via the -File
switch), both files and directories can be among the output items.由于您使用Get-ChildItem
而不将结果限制为文件(通过-File
开关),因此文件和目录都可以在 output 项目中。
While Rename-Item
results in a quiet no-op if a file is being renamed to the same name that it currently has, trying the same on a directory results in the error you saw.如果将文件重命名为当前具有的相同名称,则Rename-Item
会导致安静的无操作,但在目录上尝试相同的名称会导致您看到的错误。
This applies to all items whose name does not contain substring 'test'
, in which case the这适用于名称不包含 substring 'test'
所有项目,在这种情况下-replace
operation passes the input string through as-is. -replace
操作按原样传递输入字符串。
If your intent is to rename files only , the solution is to simply add the -File
switch :如果您的意图只是重命名文件,则解决方案是简单地添加-File
开关:
Get-ChildItem -File "C:\Users\Admin\Desktop\mydirectory\subdirectory" |
Rename-Item -NewName { $_.Name -replace 'test' }
If directories are (also) targeted , as in your case, you need to explicitly filter out input items for which no actual renaming would occur :如果目录是(也)目标,就像您的情况一样,您需要明确过滤掉不会发生实际重命名的输入项:
Get-ChildItem -Directory -Filter *test* "C:\Users\Admin\Desktop\mydirectory\subdirectory" |
Rename-Item -NewName { $_.Name -replace 'test' }
-Filter *test*
ensures that only subdirectories that contain the word 'test'
are output, which guarantees that actual renaming occur (though note that the command would fail if a subdirectory's entire name were 'test'
, as that would make the script block return the empty string ). -Filter *test*
确保只有包含单词'test'
的子目录是 output,这保证了实际重命名发生(但请注意,如果子目录的全名是'test'
,该命令将失败,因为这会使脚本阻塞返回空字符串)。
If you simply want to rename a single subdirectory to a fixed new name , you don't need a delay-bind script block at all:如果您只是想将单个子目录重命名为固定的新名称,则根本不需要延迟绑定脚本块:
# NOTE: Works only if only a SINGLE subdirectory is returned.
Get-ChildItem -Directory "C:\Users\Admin\Desktop\mydirectory\subdirectory" |
Rename-Item -NewName 'test'
If you have multiple subdirectories and you want incorporate a sequence number into the new names , you do again need a delay-bind script block:如果您有多个子目录,并且您希望将序列号合并到新名称中,您确实需要一个延迟绑定脚本块:
$num = 0
Get-ChildItem -Directory "C:\Users\Admin\Desktop\mydirectory\subdirectory" |
Rename-Item -NewName { 'test' + ++(Get-Variable -Scope 1 num).Value } -WhatIf
This renames the subdirectories to test1
, test2
, ...这会将子目录重命名为test1
, test2
,...
For an explanation of this technique (the need for a Get-Variable
call), see this answer .有关此技术的说明(需要调用Get-Variable
),请参阅此答案。
If you want to preview the renaming operations that would take place, you can add the -WhatIf
common parameter to the Rename-Item
call, which will show for each input file what it would be renamed to.如果要预览将要发生的重命名操作,可以将-WhatIf
通用参数添加到Rename-Item
调用,这将为每个输入文件显示它将被重命名为的内容。
However, you have to infer from the output the cases when no actual renaming takes place , due to the delay-bind script block passed to -NewName
returning the same name as before .但是,您必须从 output推断没有实际重命名发生的情况,因为传递给-NewName
的延迟绑定脚本块返回与以前相同的名称。
Eg, an input file named foo
would not be renamed, because 'foo' -replace 'test'
returns 'foo'
unmodified, which with -WhatIf
would show as follows (line breaks added for readability) - note how the target and the destination path are the same:例如,名为foo
的输入文件不会被重命名,因为'foo' -replace 'test'
返回未经修改'foo'
,使用-WhatIf
会显示如下(为便于阅读而添加了换行符) - 注意目标和目标如何路径相同:
What if: Performing the operation "Rename File" on target "
Item: C:\path\to\foo
Destination: C:\path\to\foo
"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.