将文件按名称末尾的第三个分隔符重命名。

huangapple go评论54阅读模式
英文:

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

辅助函数的逻辑是根据 _ 分割字符串,并连接除最后三个元素之外的所有元素(在此示例中为 def)。


<details>
<summary>英文:</summary>

I&#39;d use a helper function for this:

```powershell
function getNewName($basename, $extension){
  $s = $basename -split &quot;_&quot;
  $n = ($s[0..($s.Length-4)]) -join &quot;_&quot;
  return &quot;$n&quot;+$extension
}

Usage:

&gt; new-item &quot;a_b_c_d_e_f.txt&quot;; 
&gt; Get-ChildItem &#39;.&#39; -filter *.txt | rename-item -NewName {getNewName $_.Basename $_.Extension}

&gt; 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 &#39;C:\Users\xxx\Projects\testfiles&#39; -Filter &#39;*_*_*_*.txt&#39; -File |
Rename-Item -NewName {&#39;{0}{1}&#39; -f ($_.BaseName -replace &#39;_[^_]+_[^_]+_[^_]+$&#39;), $_.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 &#39;_&#39;, -4)[0] + $_.Extension
  } -WhatIf

<sup>上述命令中的 -WhatIf 通用参数 预览 了操作。确定操作将按预期执行后,删除 -WhatIf 并重新执行。</sup>

  • -split &#39;_&#39;, -4 将输入文件名按分隔符 _ 拆分成子字符串,从字符串的 末尾 提取最多 4 个标记,并返回输入字符串 前面 的剩余部分作为 第一个 元素。

    • 例如,&#39;a_b_c_d_e_f&#39; 被拆分为前缀 &#39;a_b_c&#39; 和令牌数组 @(&#39;d&#39;, &#39;e&#39;, &#39;f&#39;)(这里我们不感兴趣)。
  • + $_.Extension 将现有扩展名附加到结果名称,得到 &#39;a_b_c.txt&#39;

注意:

  • 不包含 任何 _ 实例的文件名保持原样(不进行重命名)。

  • 仅包含 1 到 2 个 _ 实例的文件名将重命名为它们第一个 _ 之前的前缀;例如,&#39;a_b_c.txt&#39; 变为 &#39;a.txt&#39;

  • 如果新的、缩短的名称导致重复,个别重命名操作可能会失败。

英文:

<!-- 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 &#39;_&#39;, -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 &#39;_&#39;, -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., &#39;a_b_c_d_e_f&#39; is split into prefix &#39;a_b_c&#39; and token array @(&#39;d&#39;, &#39;e&#39;, &#39;f&#39;) (which we're not interested in here).
  • + $_.Extension appends the existing extension to the resulting name, resulting in &#39;a_b_c.txt&#39;

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. &#39;a_b_c.txt&#39; becomes &#39;a.txt&#39;

  • Individual rename operations may fail, if the new, shortened names result in duplicates.

huangapple
  • 本文由 发表于 2023年5月14日 20:10:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定