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

Moh*_*ngh 1 powershell cmd

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

C:\Users\mohit singh\Desktop\spotlight> ren *.* *.jpg
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

msa*_*ord 6

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

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

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

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

如果您也想对子文件夹进行操作,请添加-RecurseGet-ChildItem.