仅更改文件名的一部分的多个文件的名称
change the name of multiple file for just a part of the filename
我有 50 个文件,文件名的格式如下:
SIGMOID 01012019 - 01082019.XLS
SIGMOID 01012019 - 01022019.XLS
我想改成:
SIGMOID_01012019 - 01082019.XLS
SIGMOID_01012019 - 01022019.XLS
我想更新部分文件名并添加下划线而不是名称中的第一个空格 space。
谢谢
我尝试在 powershell 中编写脚本
dir |
Where-Object { $_.name.Contains(" ") } |
Rename-Item -NewName { $_.name -replace " ","_" }
但这会更新每个空白 space。
也许不是最优雅的解决方案,但也许正是您要找的:
Rename-Item -NewName { $_.name -replace "SIGMOID ","SIGMOID_" }
假设您有如下文件:
SIGMOID 01012019 - 01082019.XLS
SIGMOID 01012019 - 01022019.XLS
SOMETHINGELSE 01012019 - 02022019.XLS
YETANOTHERPREFIX 01012019 - 03022019.XLS
然后下面的代码将重命名它们:
$sourceFolder = 'THE PATH OF THE ROOTFOLDER THAT STORES THE XLS FILES TO RENAME' #'# enter your rootpath here
Get-ChildItem -Path $sourceFolder -File |
Where-Object { $_.Name -match '^\w+ +\d{8}.*\.xlsx?' } |
ForEach-Object {
$_ | Rename-Item -NewName ($_.Name -replace '^(\w+) +(\d{8}.*\.xlsx?)', '_') -WhatIf
}
成为
SIGMOID_01012019 - 01082019.XLS
SIGMOID_01012019 - 01022019.XLS
SOMETHINGELSE_02012019 - 01022019.XLS
YETANOTHERPREFIX_03012019 - 01022019.XLS
注意:如果您对结果感到满意,请删除 -WhatIf
正则表达式详细信息:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ Match the character “ ” literally
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\d Match a single digit 0..9
{8} Exactly 8 times
. 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
xls Match the characters “xls” literally
x Match the character “x” literally
? Between zero and one times, as many times as possible, giving back as needed (greedy)
)
将我的评论变成答案,
仅在第一个 space 拆分名称并使用下划线连接
'* *'
确保文件名中至少有一个 space。
Get-ChildItem '* *' |
Rename-Item -NewName {($_.Name -split ' ',2 -join '_')} -Whatif
如果输出正常,删除 -WhatIf
在连续运行中,这会将示例中的 -
更改为 _-
等
为了防止这种情况,您需要一个 Where-Object
来排除带下划线的文件。
Get-ChildItem '* *' |
Where-Object Name -notmatch '^\S+_' |
Rename-Item -NewName {($_.Name -split ' ',2 -join '_')} -Whatif
嗯,换两次怎么样:
'SIGMOID 01012019 - 01082019.XLS' -replace ' ','_' -replace '_-_',' - '
SIGMOID_01012019 - 01082019.XLS
或(将 space 替换为两边都没有破折号):
'SIGMOID 01012019 - 01082019.XLS' -replace '[^-] [^-]','_'
SIGMOI_1012019 - 01082019.XLS
试试 rename-item -whatif ...
我有 50 个文件,文件名的格式如下:
SIGMOID 01012019 - 01082019.XLS
SIGMOID 01012019 - 01022019.XLS
我想改成:
SIGMOID_01012019 - 01082019.XLS
SIGMOID_01012019 - 01022019.XLS
我想更新部分文件名并添加下划线而不是名称中的第一个空格 space。
谢谢
我尝试在 powershell 中编写脚本
dir |
Where-Object { $_.name.Contains(" ") } |
Rename-Item -NewName { $_.name -replace " ","_" }
但这会更新每个空白 space。
也许不是最优雅的解决方案,但也许正是您要找的:
Rename-Item -NewName { $_.name -replace "SIGMOID ","SIGMOID_" }
假设您有如下文件:
SIGMOID 01012019 - 01082019.XLS SIGMOID 01012019 - 01022019.XLS SOMETHINGELSE 01012019 - 02022019.XLS YETANOTHERPREFIX 01012019 - 03022019.XLS
然后下面的代码将重命名它们:
$sourceFolder = 'THE PATH OF THE ROOTFOLDER THAT STORES THE XLS FILES TO RENAME' #'# enter your rootpath here
Get-ChildItem -Path $sourceFolder -File |
Where-Object { $_.Name -match '^\w+ +\d{8}.*\.xlsx?' } |
ForEach-Object {
$_ | Rename-Item -NewName ($_.Name -replace '^(\w+) +(\d{8}.*\.xlsx?)', '_') -WhatIf
}
成为
SIGMOID_01012019 - 01082019.XLS SIGMOID_01012019 - 01022019.XLS SOMETHINGELSE_02012019 - 01022019.XLS YETANOTHERPREFIX_03012019 - 01022019.XLS
注意:如果您对结果感到满意,请删除 -WhatIf
正则表达式详细信息:
^ Assert position at the beginning of a line (at beginning of the string or after a line break character)
( Match the regular expression below and capture its match into backreference number 1
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\ Match the character “ ” literally
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
\d Match a single digit 0..9
{8} Exactly 8 times
. 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
xls Match the characters “xls” literally
x Match the character “x” literally
? Between zero and one times, as many times as possible, giving back as needed (greedy)
)
将我的评论变成答案,
仅在第一个 space 拆分名称并使用下划线连接
'* *'
确保文件名中至少有一个 space。
Get-ChildItem '* *' |
Rename-Item -NewName {($_.Name -split ' ',2 -join '_')} -Whatif
如果输出正常,删除 -WhatIf
在连续运行中,这会将示例中的 -
更改为 _-
等
为了防止这种情况,您需要一个 Where-Object
来排除带下划线的文件。
Get-ChildItem '* *' |
Where-Object Name -notmatch '^\S+_' |
Rename-Item -NewName {($_.Name -split ' ',2 -join '_')} -Whatif
嗯,换两次怎么样:
'SIGMOID 01012019 - 01082019.XLS' -replace ' ','_' -replace '_-_',' - '
SIGMOID_01012019 - 01082019.XLS
或(将 space 替换为两边都没有破折号):
'SIGMOID 01012019 - 01082019.XLS' -replace '[^-] [^-]','_'
SIGMOI_1012019 - 01082019.XLS
试试 rename-item -whatif ...