gpt4 book ai didi

powershell - 如何使用 PowerShell 从文件名中删除句点?

转载 作者:行者123 更新时间:2023-12-02 23:07:20 25 4
gpt4 key购买 nike

我正在尝试在 PowerShell 中编写一个脚本,用于在文件夹中搜索包含不必要句点的文件,然后从文件名中大量删除句点。

例如。 Example.File.doc ---> ExampleFile.doc

每当我运行代码时,控制台都会返回以下内容:“Rename-Item:无法将参数绑定(bind)到参数‘NewName’,因为它是一个空字符串。”

有人知道问题出在哪里吗?

在此先感谢您的帮助!

$files = get-childitem -path "C:\XXXX\XXXX\XXXX" -Recurse 
foreach($file in $files) {

if($file.Name -match ".") {
$newName = $file.Name -ireplace (".", "")
Rename-Item $file.FullName $newName
Write-Host "Renamed" $file.Name "to $newName at Location:" $file.FullName
}
}

最佳答案

关于字符串替换的一件事:当我在没有转义句点的情况下尝试你的例子时,它没有替换任何东西并且没有返回任何东西(空字符串),我相信这回答了你的“无法将参数绑定(bind)到参数'NewName'因为它是一个空字符串”

这是通过转义句点来实现的。此外,它在有或没有括号的情况下都有效。

$string = "the.file.name"

$newstring = $string -ireplace "\.", ""
// output: thefilename
$newstring.length
// output: 11

$othernewstring = $string -ireplace ("\.", "")
// output: thefilename
$othernewstring.length
// output: 11

// What the OP tried
$donothingstring = $string -ireplace (".", "")
// output:
$donothingstring.length
// output: 0

这里有一些关于字符串替换的额外信息,仅供引用https://vexx32.github.io/2019/03/20/PowerShell-Replace-Operator/

关于powershell - 如何使用 PowerShell 从文件名中删除句点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59330808/

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