gpt4 book ai didi

loops - move 项目在循环中不起作用

转载 作者:行者123 更新时间:2023-12-03 19:11:42 30 4
gpt4 key购买 nike

ls *.gif | Foreach { $newname = $_.Name -replace '\[','' -replace '\]',''
write-host $_.Name $newname
move-Item -Path $_.Name -Destination $newname; }
ls *.gif

因此,在尝试帮助某人使用 [] 重命名文件时,我发现 move-item 无法循环使用。它似乎在循环外工作得很好。

想法?

最佳答案

更新:根据下面的评论,我想澄清这一点:文件名中的特殊字符要求您使用 -LiteralPath 参数。 -Path 无法处理这些字符。在循环之外, -Path 有效,因为您使用 ` 转义特殊字符。在浏览集合时这是不可能的。

在循环中,您需要使用 -LiteralPath 参数而不是 -Path。

-LiteralPath <string[]>
Specifies the path to the current location of the items. Unlike Path, the value of
LiteralPath is used exactly as it is typed. **No characters are interpreted as
wildcards. If the path includes escape characters, enclose it in single quotation
marks.** Single quotation marks tell Windows PowerShell not to interpret any
characters as escape sequences.

所以,这将是:
GCI -Recurse *.txt | % { Move-Item -LiteralPath $_.FullName -Destination "SomenewName" }

关于loops - move 项目在循环中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5328396/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com