在PowerShell中批量重命名 [英] Batch renaming in PowerShell

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

问题描述

我正在尝试更新一堆.jpg图片,以便它们在我的网站上刷新.它们当前以如下方式存储在我的PC上:

I'm trying to update a bunch of .jpg images so that they refresh on my website. They're currently stored on my PC like this:

media
+- random-project
|  +- 1.jpg
|  +- 2.jpg
|  +- 3.jpg
|  `- thumb.png
+- another-random-project
|  +- 1.jpg
|  `- thumb.png

我正在尝试将所有1.jpg, 2.jpg等重命名为1a.jpg, 2a.jpg.

I'm trying to batch rename all 1.jpg, 2.jpg etc. to 1a.jpg, 2a.jpg.

我正在考虑使用Windows PowerShell和类似

I'm thinking using Windows PowerShell and something like

Get-ChildItem -Recurse -Filter "[0-9]+.jpg" | foreach { $_.FullName}

Dir *.jpg | Rename-Item -NewName { $_.Name  -replace "[0-9]+.jpg","[0-9]a.jpg" }

,但以前从未使用过该程序.你觉得呢?

but have never used the program before. What do you think?

推荐答案

  • 如果有可用的话,总是值得使用-Filter参数:它在源头执行过滤 ,因此通常比过滤快得多稍后.

    • It's always worth using the -Filter parameter, if available: it performs filtering at the source and is therefore typically much faster than filtering later.

      -Filter参数接受的(字符串)值的语法 cmdlet -/ PowerShell驱动器特定于提供商.
      对于 Get-ChildItem (及其内置别名dir,在 Windows PowerShell中,为ls),它接受单个通配符表达式-类似于,但不同于PowerShell自己的

      The syntax of the (string) value accepted by the -Filter parameter is cmdlet- / PowerShell drive provider-specific.
      In the case of Get-ChildItem (and its built-in aliases dir and, in Windows PowerShell, ls), it accepts a single wildcard expression - similar to, but distinct from PowerShell's own wildcard expressions - whose supported syntax is platform-dependent:

      • On Windows, the expression may only contain * and ? wildcards - see this answer.

      Unix 平台上的PowerShell Core中,表达式也可能包含字符集,例如[0-9].

      In PowerShell Core on Unix platforms, the expression may also contain character sets such as [0-9].

      [0-9]+.jpg不能 用作-Filter值,因为它是 regex (正则表达式).

      [0-9]+.jpg does not work as a -Filter value, because it is a regex (regular expression).

      顺便说一句:您可能打算将.用作 literal ,在这种情况下,您必须在正则表达式中转义,例如\.,因为.是表示 any 字符的正则表达式元字符.在输入中.

      As an aside: You probably meant to use . as a literal, in which case you'd have to escape it in the regex as \., because . is a regex metacharacter representing any char. in the input.

      您不能使用通配符表达式来模拟正则表达式[0-9]+.jpg,因为即使通配符表达式确实支持诸如[0-9]之类的字符集,它们也会缺少子表达式重复符号(量词),例如为+ *?.
      (相比之下,通配符元字符*/?独立构造,表示任何可能为空的字符序列/完全是一个字符).

      You cannot emulate regular expression [0-9]+.jpg with a wildcard expression, because even when wildcard expressions do support character sets such as [0-9], they lack subexpression duplication symbols (quantifiers) such as + and * and ?.
      (By contrast, wildcard metacharacters * / ? are stand-alone constructs that represent any possibly empty sequence of characters / exactly one character).

      注意:-WhatIf已添加到下面的Rename-Item调用中,以预览进行将会进行的任何更改.
      删除-WhatIf以执行实际的重命名.

      Note: -WhatIf has been added to the Rename-Item calls below to preview any changes that would be made.
      Remove -WhatIf to perform actual renaming.

      [仅在Unix上的PowerShell Core ] 如果所有感兴趣的*.jpg文件都具有基本名称(文件名root) (仅限于一位数字数字)(例如,1.jpg,...,9.jpg),则可以通过传递通配符表达式从em> [0-9].jpg-Filter:

      [PowerShell Core on Unix only] If all *.jpg files of interest have a base name (filename root) that is limited to a single-digit number, (e.g., 1.jpg, ..., 9.jpg), you can get away with passing wildcard expression [0-9].jpg to -Filter:

      Get-ChildItem -File -Recurse -Filter [0-9].jpg |
        Rename-Item -NewName { $_.BaseName + 'a' + $_.Extension } -WhatIf
      

      请注意,如何将脚本块参数{ $_.BaseName + 'a' + $_.Extension }传递给参数-NewName,以从输入文件的基本名称,后跟原义a,后跟输入文件的扩展名构造新文件名.

      Note how passing script-block argument { $_.BaseName + 'a' + $_.Extension } to parameter -NewName constructs the new filename from the input file's base name, followed by literal a, followed by the input file's extension.

      否则

      Otherwise,

      • 使用通配符表达式*.jpg进行有效的 pre 过滤

      • use wildcard expression *.jpg for efficient pre-filtering,

      然后通过Where-Object调用将预先过滤的结果缩小到特定的基于 regex 的匹配项,该调用使用-match来比较每个输入文件的 regex ^[0-9]+$的基本名称,即测试它是否仅由十进制数字组成.

      then narrow the pre-filtered results down to specific regex-based matches with a Where-Object call that uses -match to compare each input file's base name to regex ^[0-9]+$, i.e., to test if it is only composed of decimal digits.

      注意:
      *以下命令使用PSv3 + Get-ChildItemWhere-Object语法.
      *传递给-match的正则表达式使用引号('...')而不是双引号("..."),以确保PowerShell的字符串插值不会受到干扰.

      Note:
      * The command below uses PSv3+ Get-ChildItem and Where-Object syntax.
      * The regex passed to -match uses single-quoting ('...') rather than double-quoting ("...") to ensure that PowerShell's string interpolation doesn't get in the way.

      Get-ChildItem -File -Recurse -Filter *.jpg |
        Where-Object BaseName -match '^[0-9]+$' |
          Rename-Item -NewName { $_.BaseName + 'a' + $_.Extension  } -WhatIf
      

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

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