通过重新排列文件名来批量重命名目录中的文件 [英] Batch Rename Files in a Directory By Rearranging Filenames

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

问题描述

我在目录中有大量文件,其命名方式为:"[日期] [姓氏] 345.pdf".注意:所有345都是常数

I have a large number of files in a directory with this type of naming convention: "[Date] [Lastname] 345.pdf". Note: the 345 is constant for all of them

我想重命名所有文件,以便它们显示为"[姓氏] [日期] 345.pdf".

I would like to rename all of the files so that they read "[Lastname] [Date] 345.pdf".

我最初在这里输入错字

我在以下方面取得了一些成功:

I have had some success with:

gci -Filter *.pdf| %{Rename-Item $_.fullname $_.Name.Split(" ")[1]}

但是,这只给了我"[Date] .pdf".基本上,我不知道如何使用上述命令来组合拆分的两个不同部分.我基本上想要"[1] [0] [2] .pdf".

But that only gives me "[Date].pdf". Basically, I can't figure out how to use the above command to combine two different parts of the split. I basically want "[1] [0] [2].pdf" if that makes any sense.

谢谢.

推荐答案

使用正则表达式的另一种方法:

Another approach, using regular expressions:

Get-ChildItem *.pdf | % {
  $newName = $_.Name -replace '^(.*) (\S+) (\S+\.pdf)$', '$2 $1 $3'
  Rename-Item $_ $newName
}

正则表达式细分:

  • ^(.*)匹配字符串开头的任意数量的字符,并将它们捕获到一个组中. [Date]在您的示例文件名[Date] [Lastname] 345.pdf中.
  • (\S+)匹配一个或多个连续的非空白字符,并将它们捕获在第二组中. [Lastname]在您的示例文件名[Date] [Lastname] 345.pdf中.
  • (\S+\.pdf)$匹配一个或多个连续的非空白字符,然后在字符串的末尾匹配字符串.pdf并将其捕获在第三组中. 345.pdf在您的示例文件名[Date] [Lastname] 345.pdf中.
  • ^(.*) matches any number of characters at the beginning of the string and captures them in a group. [Date] in your example filename [Date] [Lastname] 345.pdf.
  • (\S+) matches one or more consecutive non-whitespace characters and captures them in a second group. [Lastname] in your example filename [Date] [Lastname] 345.pdf.
  • (\S+\.pdf)$ matches one or more consecutive non-whitespace characters followed by the string .pdf at the end of the string and captures that in a third group. 345.pdf in your example filename [Date] [Lastname] 345.pdf.

$1$2$3引用了这些组,因此替换字符串'$2 $1 $3'以所需的方式对这些组进行重新排序.

The groups are referred to by $1, $2 and $3, so the replacement string '$2 $1 $3' re-orders the groups the way you want.

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

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