如何在Powershell中更改扩展名文件?

如何在Powershell中更改扩展名文件?,powershell,Powershell,我想更改一些文件的扩展名。 我尝试了这段代码,但仍然返回错误 The input to the script block for parameter 'NewName' failed. 有人能帮忙吗 $b = "TA" $c = "70" $Path_1 = "C:\Users\hh\Documents" $Found = Get-ChildItem -Name "$Path_1\*$b-$c*.txt" | Rename-Item -NewName { $_.Name.Replace

我想更改一些文件的扩展名。 我尝试了这段代码,但仍然返回错误

The input to the script block for parameter 'NewName' failed.
有人能帮忙吗

 $b = "TA"
 $c = "70"
 $Path_1 = "C:\Users\hh\Documents"
 $Found = Get-ChildItem -Name "$Path_1\*$b-$c*.txt" | Rename-Item -NewName { $_.Name.Replace('.txt','.csv') }
这应该做到:

$b = "TA"
 $c = "70"
 $Path_1 = "C:\Users\hh\Documents"
 $Found = Get-ChildItem -Filter ($Path_1 + "\*" + $b + "-" + $c + "*.txt")
foreach ($entry in $Found){
  Rename-Item -Path $entry.FullName -NewName ($entry.Name.Replace('.txt','.csv'))
}
这应该做到:

$b = "TA"
 $c = "70"
 $Path_1 = "C:\Users\hh\Documents"
 $Found = Get-ChildItem -Filter ($Path_1 + "\*" + $b + "-" + $c + "*.txt")
foreach ($entry in $Found){
  Rename-Item -Path $entry.FullName -NewName ($entry.Name.Replace('.txt','.csv'))
}

在这种情况下,在
Get-ChildItem
cmdlet上同时使用
-Path
-Filter
参数要容易得多。
如果要确保不会意外更改文件夹名称,请在PowerShell 3.0及更高版本中添加
-File
开关,就像我在这里所做的那样:

$b = "TA"
$c = "70"
$Path_1 = "C:\Users\hh\Documents"
Get-ChildItem -Path $Path_1 -Filter "*$b-$c*.txt" | Rename-Item -NewName { '{0}.csv' -f $_.BaseName } -WhatIf
# For PowerShell versions below 3.0, you need to add an extra Where-Object clause:
# Get-ChildItem -Path $Path_1 -Filter "*$b-$c*.txt" | Where-Object { !$_.PSIsContainer } | Rename-Item -NewName { '{0}.csv' -f $_.BaseName } -WhatIf
当然,您也可以使用.NET[System.IO.Path]

Get-ChildItem -Path $Path_1 -Filter "*$b-$c*.txt" | Rename-Item -NewName { [System.IO.Path]::ChangeExtension($_.Name, ".csv") } -WhatIf

如果您对结果感到满意,请删除
-WhatIf
开关。

在这种情况下,在
Get-ChildItem
cmdlet上同时使用
-Path
-Filter
参数要容易得多。
如果要确保不会意外更改文件夹名称,请在PowerShell 3.0及更高版本中添加
-File
开关,就像我在这里所做的那样:

$b = "TA"
$c = "70"
$Path_1 = "C:\Users\hh\Documents"
Get-ChildItem -Path $Path_1 -Filter "*$b-$c*.txt" | Rename-Item -NewName { '{0}.csv' -f $_.BaseName } -WhatIf
# For PowerShell versions below 3.0, you need to add an extra Where-Object clause:
# Get-ChildItem -Path $Path_1 -Filter "*$b-$c*.txt" | Where-Object { !$_.PSIsContainer } | Rename-Item -NewName { '{0}.csv' -f $_.BaseName } -WhatIf
当然,您也可以使用.NET[System.IO.Path]

Get-ChildItem -Path $Path_1 -Filter "*$b-$c*.txt" | Rename-Item -NewName { [System.IO.Path]::ChangeExtension($_.Name, ".csv") } -WhatIf

如果您对结果感到满意,请删除
-WhatIf
开关。

错误消息的其余部分是“您无法对空值表达式调用方法”。“get childitem-name”仅输出字符串,而不是具有属性的对象。在get childitem之后去掉-name,它就可以工作了。

错误消息的其余部分是“不能对空值表达式调用方法”。“get childitem-name”仅输出字符串,而不是具有属性的对象。在get childitem之后去掉-name,它就会工作。

完整的错误消息是什么?
get childitem-name
的输出是什么?我认为您需要转换
调用周围的scriptblock括号。Replace()
调用具有执行优先级参数才能使其正常工作。完整的错误消息是什么?
get childitem-name
output是什么?我认为您需要将
.Replace()
调用与执行参数的优先级一起转换,以使其正常工作。我不会执行
.Replace())
避免像
TA-70.txt.txt这样具有双扩展名的文件变成
TA-70.csv.csv
。如果确实要使用replace,请使用带有锚点的Regex
-replace
。类似于
-替换“\.txt$”、“.csv”
我认为,在这种情况下,这似乎是一个理论问题。该脚本假定有一个包含大量文件的文件夹,其末尾只有一个.txt文件。即使有更多的扩展名,这里似乎也没有什么大问题。我不会使用
.Replace()
来避免像
TA-70.txt.txt这样的双扩展名文件变成
TA-70.csv.csv
。如果确实要使用replace,请使用带有锚点的Regex
-replace
。类似于
-替换“\.txt$”、“.csv”
我认为,在这种情况下,这似乎是一个理论问题。该脚本假定有一个包含大量文件的文件夹,其末尾只有一个.txt文件。即使有更多的人,这似乎也不是什么大问题。