问题描述
我正在尝试更新一堆.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 ofGet-ChildItem
(and its built-in aliasesdir
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:-
在 Windows 上,表达式只能包含
*
和?
通配符-请参见这个答案.
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).您不能使用通配符表达式来模拟正则表达式
[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).[仅在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 literala
, 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.Get-ChildItem -File -Recurse -Filter *.jpg | Where-Object BaseName -match '^[0-9]+$' | Rename-Item -NewName { $_.BaseName + 'a' + $_.Extension } -WhatIf
这篇关于在PowerShell中批量重命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
-