重命名文件夹中的多个文件,添加前缀 (Windows)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20874915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:13:37  来源:igfitidea点击:

Rename multiple files in a folder, add a prefix (Windows)

windowspowershellfilenamesprefixbatch-rename

提问by ofer.sheffer

I'd like to batch rename files in a folder, prefixing the folder's name into the new names. i.e. files in C:\house chores\will all be renamed house chores - $old_name.

我想批量重命名文件夹中的文件,将文件夹的名称作为新名称的前缀。即文件中的C:\house chores\所有文件都将被重命名house chores - $old_name

回答by ofer.sheffer

Option 1: Using Windows PowerShell

选项 1:使用 Windows PowerShell

Open the windows menu. Type: "PowerShell" and open the 'Windows PowerShell' command window.

打开窗口菜单。键入:“PowerShell”并打开“Windows PowerShell”命令窗口。

Goto folder with desired files: e.g. cd "C:\house chores" Notice: address must incorporate quotes "" if there are spaces involved.

转到包含所需文件的文件夹:例如 cd "C:\house chores" 注意:如果涉及空格,地址必须包含引号 ""。

You can use 'dir' to see all the files in the folder. Using '|' will pipeline the output of 'dir' for the command that follows.

您可以使用“dir”查看文件夹中的所有文件。使用“|” 将为后面的命令流水线化 'dir' 的输出。

Notes: 'dir' is an alias of 'Get-ChildItem'. See: wiki: cmdlets. One can provide further functionality. e.g. 'dir -recurse' outputs all the files, folders and sub-folders.

注意:“dir”是“Get-ChildItem”的别名。请参阅:维基:cmdlet。可以提供进一步的功能。例如,'dir -recurse' 输出所有文件、文件夹和子文件夹。

What if I only want a range of files?

如果我只想要一系列文件怎么办?

Instead of 'dir |' I can use:

而不是 'dir |' 我可以用:

dir | where-object -filterscript {($_.Name -ge 'DSC_20') -and ($_.Name -le 'DSC_31')} |

For batch-renaming with the directory name as a prefix:

对于以目录名称为前缀的批量重命名:

dir | Rename-Item -NewName {$_.Directory.Name + " - " + $_.Name}

Option 2: Using Command Prompt

选项 2:使用命令提示符

In the folder press shift+right-click : select 'open command-window here'

在文件夹中按 shift+右键单击:选择“在此处打开命令窗口”

for %a in (*.*) do ren "%a" "prefix - %a"

If there are a lot of files, it might be good to add an '@echo off' command before this and an 'echo on' command at the end.

如果有很多文件,最好在此之前添加“@echo off”命令并在最后添加“echo on”命令。

回答by Dave.Gugg

The problem with the two Powershell answers here is that the prefix can end up being duplicated since the script will potentially run over the file both before and after it has been renamed, depending on the directory being resorted as the renaming process runs. To get around this, simply use the -Excludeoption:

此处的两个 Powershell 答案的问题在于前缀可能最终会被复制,因为脚本可能会在重命名之前和之后运行该文件,具体取决于在重命名过程运行时重新使用的目录。要解决此问题,只需使用以下-Exclude选项:

Get-ChildItem -Exclude "house chores-*" | rename-item -NewName { "house chores-" + $_.Name }

This will prevent the process from renaming any one file more than once.

这将防止进程多次重命名任何一个文件。

回答by Dave.Gugg

Free Software 'Bulk Rename Utility' also works well (and is powerful for advanced tasks also). Downloadand installation takes a minute.

免费软件“批量重命名实用程序”也运行良好(对于高级任务也很强大)。下载和安装需要一分钟。

See screenshotsand tutorialon original website.

请参阅原始网站上的屏幕截图教程

--

——

I cannot provide step-by-step screenshots as the images will have to be released under Creative Commons License, and I do not own the screenshots of the software.

我无法提供分步截图,因为图像必须在知识共享许可发布,而且我不拥有该软件的截图。

Disclaimer:I am not associated with the said software/company in any way. I liked the product for my own task, it serves OP's and similar requirements, thus recommending.

免责声明:我与上述软件/公司没有任何关联。我喜欢该产品用于我自己的任务,它满足 OP 和类似的要求,因此推荐。

回答by user3971138

This worked for me, first cd in the directory that you would like to change the filenames to and then run the following command:

这对我有用,首先 cd 在您要将文件名更改为的目录中,然后运行以下命令:

Get-ChildItem | rename-item -NewName { "house chores-" + $_.Name }

回答by phantom-99w

I was tearing my hair out because for some items, the renamed item would get renamed again (repeatedly, unless max file name length was reached). This was happening both for Get-ChildItem and piping the output of dir. I guess that the renamed files got picked up because of a change in the alphabetical ordering. I solved this problem in the following way:

我正在撕扯我的头发,因为对于某些项目,重命名的项目会再次重命名(重复,除非达到最大文件名长度)。Get-ChildItem 和管道输出 dir 都发生了这种情况。我想重命名的文件是由于字母顺序的变化而被提取的。我通过以下方式解决了这个问题:

Get-ChildItem -Path . -OutVariable dirs
foreach ($i in $dirs) { Rename-Item $i.name ("<MY_PREFIX>"+$i.name) }

This "locks" the results returned by Get-ChildItem in the variable $dirs and you can iterate over it without fear that ordering will change or other funny business will happen.

这将 Get-ChildItem 返回的结果“锁定”在变量 $dirs 中,您可以对其进行迭代,而不必担心排序会改变或发生其他有趣的事情。

Dave.Gugg's tip for using -Exclude should also solve this problem, but this is a different approach; perhaps if the files being renamed already contain the pattern used in the prefix.

Dave.Gugg 使用 -Exclude 的提示也应该可以解决这个问题,但这是一种不同的方法;也许如果被重命名的文件已经包含前缀中使用的模式。

(Disclaimer: I'm very much a PowerShell n00b.)

(免责声明:我非常喜欢 PowerShell n00b。)

回答by roverv

Based on @ofer.sheffer answer, this is the CMD variant for adding an affix (this is not the question, but this page is still the #1 google result if you search affix). It is a bit different because of the extension.

根据@ofer.sheffer 的回答,这是用于添加词缀的 CMD 变体(这不是问题,但如果您搜索词缀,此页面仍然是 #1 google 结果)。由于扩展名,它有点不同。

for %a in (*.*) do ren "%~a" "%~na-affix%~xa"

You can change the "-affix" part.

您可以更改“-affix”部分。

回答by karpa

Based on @ofer.sheffer answer this command will mass rename and append the current dateto the filename. ie "file.txt" becomes "20180329 - file.txt" for all files in the current folder

根据@ofer.sheffer 的回答,此命令将批量重命名并将当前日期附加到文件名。即对于当前文件夹中的所有文件,“file.txt”变为“20180329 - file.txt”

for %a in (*.*) do ren "%a" "%date:~-4,4%%date:~-7,2%%date:~-10,2% - %a"

回答by kmpm

I know it's an old question but I learned alot from the various answers but came up with my own solution as a function. This should dynamically add the parent folder as a prefix to all files that matches a certain pattern but only if it does not have that prefix already.

我知道这是一个老问题,但我从各种答案中学到了很多东西,但提出了我自己的解决方案作为一个功能。这应该动态地将父文件夹作为前缀添加到匹配特定模式的所有文件中,但前提是它没有该前缀。

function Add-DirectoryPrefix($pattern) {
    # To debug, replace the Rename-Item with Select-Object
    Get-ChildItem -Path .\* -Filter $pattern -Recurse | 
        Where-Object {$_.Name -notlike ($_.Directory.Name + '*')} | 
        Rename-Item -NewName {$_.Directory.Name + '-' + $_.Name}
        # Select-Object -Property Directory,Name,@{Name = 'NewName'; Expression= {$_.Directory.Name + '-' + $_.Name}}
}

https://gist.github.com/kmpm/4f94e46e569ae0a4e688581231fa9e00

https://gist.github.com/kmpm/4f94e46e569ae0a4e688581231fa9e00