重命名目录中的所有项目 [英] Rename all Items in a directory

查看:34
本文介绍了重命名目录中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约 50 个文件的目录,我想在所有文件名前加上CD1".阅读此处后这里.我想出了以下命令(在 Windows 10 上使用 PowerShell ISE):

Get-ChildItem |Rename-Item -NewName {'CD1' + $_.Name}

但是在每个文件名之前多次添加了CD1".我也收到了错误:

<前>重命名项目:找不到路径的一部分.在行:1 字符:17+ Get-ChildItem |Rename-Item -NewName {'CD1' + $_.Name}+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : WriteError: (C:\Users\TOOdhm...CD1CD1CD149.mp3:String) [Rename-Item], DirectoryNotFoundException+ FullQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

它似乎不知道何时停止遍历目录,所以我使用了 ForEach-Object cmdlet 并尝试了这个:

Get-ChildItem |ForEach-Object { Rename-Item -NewName {('CD1' + $_.Name)} }

它什么也没做并给出了错误

<块引用>

重命名项目:无法评估参数NewName",因为它的参数被指定为脚本块并且没有输入.没有输入就无法评估脚本块.

有人知道我为什么会遇到这个问题吗?我现在可以手动重命名文件,但是这两个命令都应该可以工作.我什至找到了一个网站,在那里他们使用了一个几乎完全相同的示例(我认为在他们的示例中他们可能会附加)这种格式的这些命令,这让我感到困扰,它们对我不起作用.

解决方案

避免再次处理已重命名的项目的一种方法是将 Get-ChildItem 括在括号中.

(Get-ChildItem) |Rename-Item -NewName {'CD1'+ $_.name}

为了避免在连续运行中再次添加,排除以 CD1

开头的文件

Get-ChildItem |其中 {$_.Name -notmatch '^CD1'} |Rename-Item -NewName {'CD1'+ $_.name}

gci -排除 CD1* |Ren -NewName {'CD1'+ $_.name}

I have a directory which has about 50 files and I would like to prepend "CD1" to all the file names. After reading here and here. I came up with the following command (using PowerShell ISE on Windows 10):

Get-ChildItem | Rename-Item -NewName {'CD1' + $_.Name}

but that prepended "CD1" several times to each file name. I also got the error:

Rename-Item : Could not find a part of the path.
At line:1 char:17
+ Get-ChildItem | Rename-Item -NewName {'CD1' + $_.Name}
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (C:\Users\TOOdhm...CD1CD1CD149.mp3:String) [Rename-Item], DirectoryNotFoundException
    + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

It seemed like it didn't know when to stop iterating through the directory so I used the ForEach-Object cmdlet and tried this:

Get-ChildItem | ForEach-Object { Rename-Item -NewName {('CD1' + $_.Name)} }

which did nothing and gave the error

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.

Does any one have any clue why I am getting this issue? I could have manually renamed the files by now but both these commands should work. I even found a website where they used an example with almost exactly(I think in their example they may be appending) these commands in this format and it's bugging me that they don't work for me.

解决方案

One way to avoid getting already renamed items processed again is to enclose Get-ChildItem in parentheses.

(Get-ChildItem) | Rename-Item -NewName {'CD1'+ $_.name}

To avoid another prepend on successive runs exclude files beginning with CD1

Get-ChildItem | Where {$_.Name -notmatch '^CD1'} | Rename-Item -NewName {'CD1'+ $_.name}

Or

gci -Exclude CD1* | Ren -NewName {'CD1'+ $_.name}

这篇关于重命名目录中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆