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 条评论
登录 后参与评论

相关文章