英文:
Rename files by the third delimiter from the end of the name
问题
我有一个PowerShell代码,用于重命名文件名,并只截取文件名末尾的一些字母。现在我想这样重命名它:例如,我的文件名是"a_b_c_d_e_f.txt",我想将它改成"a_b_c.txt"。有什么想法?
获取-ChildItem 'C:\Users\xxx\Projects\testfiles' -filter *.txt |
重命名-Item -NewName {$_.name.substring(0,$_.BaseName.length-9) + $_.Extension}
英文:
I have a PowerShell code that renaming the files name, and just cutting some letters from the end of the file name. Now I am trying to rename it like this: For example my file name is "a_b_c_d_e_f.txt", so I want to change it to "a_b_c.txt". Any Ideas?
Get-ChildItem 'C:\Users\xxx\Projects\testfiles' -filter *.txt |
Rename-Item -NewName {$_.name.substring(0,$_.BaseName.length-9) + $_.Extension}
答案1
得分: 2
我会为这个任务使用一个辅助函数:
```powershell
function getNewName($basename, $extension){
$s = $basename -split "_"
$n = ($s[0..($s.Length-4)]) -join "_"
return "$n"+$extension
}
用法:
> new-item "a_b_c_d_e_f.txt";
> Get-ChildItem '.' -filter *.txt | rename-item -NewName {getNewName $_.Basename $_.Extension}
> Get-Item *.txt
a_b_c.txt
辅助函数的逻辑是根据 _
分割字符串,并连接除最后三个元素之外的所有元素(在此示例中为 d
、e
和 f
)。
<details>
<summary>英文:</summary>
I'd use a helper function for this:
```powershell
function getNewName($basename, $extension){
$s = $basename -split "_"
$n = ($s[0..($s.Length-4)]) -join "_"
return "$n"+$extension
}
Usage:
> new-item "a_b_c_d_e_f.txt";
> Get-ChildItem '.' -filter *.txt | rename-item -NewName {getNewName $_.Basename $_.Extension}
> Get-Item *.txt
a_b_c.txt
the logic of the helper function is to split on _
and join all but the last three elements (in this case, d
, e
, and f
).
答案2
得分: 2
使用正则表达式 -replace
Get-ChildItem -Path 'C:\Users\xxx\Projects\testfiles' -Filter '*_*_*_*.txt' -File |
Rename-Item -NewName {'{0}{1}' -f ($_.BaseName -replace '_[^_]+_[^_]+_[^_]+$'), $_.Extension}
英文:
Use regex -replace
Get-ChildItem -Path 'C:\Users\xxx\Projects\testfiles' -Filter '*_*_*_*.txt' -File |
Rename-Item -NewName {'{0}{1}' -f ($_.BaseName -replace '_[^_]+_[^_]+_[^_]+$'), $_.Extension}
答案3
得分: 1
<!-- language-all: sh -->
用于补充 Paolo's 有用的 -split
答案,提供 [_PowerShell (Core) 7+] (https://github.com/PowerShell/PowerShell/blob/master/README.md) 解决方案,其中 -split
运算符现在接受 负 索引,用于从字符串的 末尾 进行拆分:
Get-ChildItem C:\Users\xxx\Projects\testfiles -Filter *.txt |
Rename-Item -NewName {
($_.BaseName -split '_', -4)[0] + $_.Extension
} -WhatIf
<sup>上述命令中的 -WhatIf
通用参数 预览 了操作。确定操作将按预期执行后,删除 -WhatIf
并重新执行。</sup>
-
-split
'_', -4
将输入文件名按分隔符_
拆分成子字符串,从字符串的 末尾 提取最多 4 个标记,并返回输入字符串 前面 的剩余部分作为 第一个 元素。- 例如,
'a_b_c_d_e_f'
被拆分为前缀'a_b_c'
和令牌数组@('d', 'e', 'f')
(这里我们不感兴趣)。
- 例如,
-
+ $_.Extension
将现有扩展名附加到结果名称,得到'a_b_c.txt'
注意:
-
不包含 任何
_
实例的文件名保持原样(不进行重命名)。 -
仅包含 1 到 2 个
_
实例的文件名将重命名为它们第一个_
之前的前缀;例如,'a_b_c.txt'
变为'a.txt'
- 如果不希望重命名这样的文件,请考虑 Theo's 有用的基于
-replace
的解决方案 或修改-Filter
参数以排除少于 3 个_
的名称
- 如果不希望重命名这样的文件,请考虑 Theo's 有用的基于
-
如果新的、缩短的名称导致重复,个别重命名操作可能会失败。
英文:
<!-- language-all: sh -->
To complement Paolo's helpful -split
answer with a PowerShell (Core) 7+ solution, where the -split
operator now accepts negative indices, for splitting from the end of a string:
Get-ChildItem C:\Users\xxx\Projects\testfiles -Filter *.txt |
Rename-Item -NewName {
($_.BaseName -split '_', -4)[0] + $_.Extension
} -WhatIf
<sup>The -WhatIf
common parameter in the command above previews the operation. Remove -WhatIf
and re-execute once you're sure the operation will do what you want.</sup>
-
-split
'_', -4
splits the input file name into substrings by separator_
, extracting up to 4 tokens from the end of the string, and returning whatever remains at the front of the input string as the first element.- E.g.,
'a_b_c_d_e_f'
is split into prefix'a_b_c'
and token array@('d', 'e', 'f')
(which we're not interested in here).
- E.g.,
-
+ $_.Extension
appends the existing extension to the resulting name, resulting in'a_b_c.txt'
Note:
-
File names that contain no
_
instances are left as-is (no renaming occurs). -
File names with only 1 to 2
_
instances are renamed to the prefix before their first_
; e.g.'a_b_c.txt'
becomes'a.txt'
- If it's preferable not to rename such files at all, consider Theo's helpful
-replace
-based solution or modify the-Filter
argument to rule out names with fewer than 3_
- If it's preferable not to rename such files at all, consider Theo's helpful
-
Individual rename operations may fail, if the new, shortened names result in duplicates.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论