PowerShell重命名文件

花花公子

我有一个充满.pdf和.dwf文件的数据库。我需要重命名这些。

这些文件的名称如下:

123456 text text.pdf

并且应如下所示:

123456000_text_text.text.pdf

我可以使用以下命令替换空格:

dir | rename-item -NewName {$_.name -replace " ","_"}

现在,我需要一个命令在前6位数字后插入3次“ 0”。

有人能帮我吗?

已经谢谢你了

根据

您仅需要过滤* .pdf和* .dwf文件,并且文件名是否符合以6位数字开头和空格字符开头的条件。然后,您可以像这样使用正则表达式替换:

Get-ChildItem -Path D:\Test -File | Where-Object { $_.Name -match '^\d{6} .*\.(dwf|pdf)$' } | 
    Rename-Item -NewName { $_.Name -replace '^(\d{6}) ', '${1}000_' -replace '\s+', '_'}

之前:

D:\TEST
    123456 text text.dwf
    123456 text text.pdf
    123456 text text.txt

后:

D:\TEST
    123456 text text.txt
    123456000_text_text.dwf
    123456000_text_text.pdf

文件名-match的正则表达式详细信息:

^               Assert position at the beginning of the string
\d              Match a single digit 0..9
   {6}          Exactly 6 times
\               Match the character “ ” literally
.               Match any single character that is not a line break character
   *            Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\.              Match the character “.” literally
(               Match the regular expression below and capture its match into backreference number 1
                Match either the regular expression below (attempting the next alternative only if this one fails)
      dwf       Match the characters “dwf” literally
   |            Or match regular expression number 2 below (the entire group fails if this one fails to match)
      pdf       Match the characters “pdf” literally
)              
$               Assert position at the end of the string (or before the line break at the end of the string, if any)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

PowerShell重命名文件名并保留扩展名

基于文件内容的一线PowerShell批次重命名

Powershell移动和重命名文件

PowerShell-循环浏览文件并重命名

使用PowerShell重命名具有多个句点的文件

重命名Powershell文件,无法重命名文件不存在

如何使用Powershell移动和重命名多个文件?

如果名称包含<string>,则Powershell重命名文件

如何在PowerShell中使用条件语句重命名文件

使用Powershell重命名多个工作表和多个文件

Powershell文件和子文件夹文件重命名

在Powershell中使用前缀批量重命名多个文件

通过对多个条件进行过滤来重命名Powershell文件

Powershell重命名文件,同时保持原始文件顺序

Powershell文件重命名语法

PowerShell重命名多个文件,文件名的特定部分

在Powershell中使用Get-ChildItem的结果重命名文件

Powershell使用LastWriteTime前缀重命名多个文件

Powershell递归文件名读取并重命名

Powershell重命名目录中的所有文件,重命名值来自被重命名的文件

Powershell - 如何在移动文件后重命名文件?

在 PowerShell 中重命名文件名

PowerShell - 重命名对象数组中的重复文件名

在使用 Powershell 移动文件之前重命名文件

Powershell 重命名目录中的文件

powershell 比较文件名并重命名

使用 CSV 源在 powershell 中重命名部分文件

Powershell - 从“组对象”输出重命名文件

Powershell:使用格式化日期重命名文件夾