powershell - 我无法使用Powershell更改多个文件的扩展名

标签 powershell cmd

要更改位于以下位置的文件的扩展名:C:\ Users \ mohit singh \ Desktop \ spotlight,我在PowerShell中键入了以下命令

C:\Users\mohit singh\Desktop\spotlight> ren *.* *.jpg

但是我收到以下错误:
ren : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path"
argument and run the operation again.
At line:1 char:1
+ ren *.* *.jpg
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Rename-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RenameItemCommand

最佳答案

您正在尝试在PowerShell中使用 ren 命令。但是,Powershell中的ren Rename-Item 的别名,它是另一条命令。 Powershell和cmd是使用不同命令解释器的不同shell。

最简单的选择是从cmd窗口而不是Powershell运行命令。

但是,如果要使用Powershell,可以通过获取当前文件夹中的每个文件,将其通过管道传递到Rename-Item,然后使用ChangeExtension .Net API更改其扩展名(比简单的字符串替换更安全)来实现。

Get-ChildItem -File | Rename-Item -NewName { [io.path]::ChangeExtension($_.Name, "jpg") }

如果您也想对子文件夹执行操作,请将-Recurse添加到Get-ChildItem

关于powershell - 我无法使用Powershell更改多个文件的扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61764539/

相关文章:

powershell - 在Powershell中使用Invoke-Command时,如何在本地显示远程exe的输出?

python - 从命令行传递特殊字符

窗口命令 : problems with for/f with a quoted command with quoted parameters

c# - 从 Windows 窗体内部调用 devcon 不起作用

powershell - 在Powershell中从左到右抓取文件中每行的前10个字符

c# - 从 PowerShell 调用 WNetAddConnection2

powershell - 用powershell用空格替换新行

powershell - 为什么 Send-MailMessage 无法通过端口 587 使用 STARTTLS 发送

windows - 在 .cmd 中使用 Windows 命令测试是 32 位还是 64 位并运行命令

node.js - (错误)ERR 未知命令 'redis-cli'