【问题标题】:Renaming the 4th character through the 12th将第 4 个字符重命名到第 12 个字符
【发布时间】:2020-12-24 06:41:06
【问题描述】:

这就是我所拥有的。

get-childitem "\\myfileserver\out\*" | foreach { rename-item $_ $_.Name.Replace("_123456_837P.", ".").Replace(".test.", ".sa.").Replace("_987654_837I." , ".") }

这是我要修复的文件名 999_987654_837I.74161.test

我想从文件名中删除 _987654_837I。我只是要重命名它,但这些数字可能会改变。所以现在我想删除从 _ 开始的第 4 个字符并返回到“I”或第 9 个字符。

【问题讨论】:

    标签: powershell powershell-3.0 powershell-4.0


    【解决方案1】:

    您可以使用正则表达式模式来获取所需的部分。

    查看正则表达式示例 + 说明: https://regex101.com/r/xNoBVD/2

    我使用正向向后看来强制正则表达式在行首 (^) 获取前 3 个字符而不捕获它。以下 12 个字符被捕获,然后可以替换为 ''

    $regexReplacePattern = '(?<=^.{3}).{12}'
    
    '999_987654_837I.74161.test' -replace $regexReplacePattern, ''
    

    【讨论】:

    • 另外值得一提的是,您不需要在 foreach 循环中使用重命名项目。 Get-ChildItem "\\myfileserver\out\*" | Rename-Item -NewName { $_.Name -replace '(?&lt;=^.{3}).{12}', '' }
    猜你喜欢
    • 1970-01-01
    • 2019-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多