通过替换/删除一些文本来重命名单个文件

时间:2014-12-16 21:05:32

标签: powershell rename

我正在尝试通过从名称中删除一些文本来重命名单个文件。发生以下错误:

PS> rni .\SomeFileName.todo.asc { $_ -replace ".todo" }

Rename-Item : Cannot evaluate parameter 'NewName' because its argument is specified
as a script block and there is no input. A script block cannot be evaluated without
input.

我也尝试过以下方法,但也是错误的:

PS> SomeFileName.todo.asc | rni -newname { $_ -replace ".todo" }

SomeFileName.todo.asc : The term 'SomeFileName.todo.asc' is not recognized as the name
of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.

1 个答案:

答案 0 :(得分:1)

您可以在以下几行中执行此操作:

$filename = ".\SomeFileName.todo.asc";
$newname = $filename -replace "\.todo", "";
rni $filename $newname;

您也可以在一行中执行此操作:

gi SomeFileName.todo.asc | rni -newname { $_ -replace "\.todo" }

这两个都给出了以下结果:

SomeFileName.asc