Powershell数字符号匹配和重命名项目逻辑

Powershell数字符号匹配和重命名项目逻辑

本文介绍了Powershell数字符号匹配和重命名项目逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下重命名文件的解决方案中,该文件取自对我的问题的回答重命名错误,请执行以下操作一点代码呢?

In the below solution for renaming files taken from an answer to my question Rename-Item error, what do the following bits of code do?

  1. ^\d{4}
  2. e={$Matches[1]}
  3. Rename-item -Newname {"{0:D4} - sp - C - {1}" -f ++$Count.Value,$_.Name}
  1. ^\d{4}
  2. e={$Matches[1]}
  3. Rename-item -Newname {"{0:D4} - sp - C - {1}" -f ++$Count.Value,$_.Name}

对于1.我认为这是说一个四位数的数字,但我想了解所使用的表示法.

For 1. I think this is saying a four digit number but I would like to understand the notation used.

对于2.$Matches尚未在任何地方设置,这是Select-Object专用的变量吗?

For 2. $Matches hasn't been set anywhere, is this a variable specific to Select-Object?

对于3.,{0:D4}在做什么,而{1}在同一字符串的末尾.另外,该行是否用逗号分隔符连接两个字符串?

For 3. what is {0:D4} doing and the {1} at the end of the same string. Also, is this line concatenating two strings with the comma delimiter?

$Count = [Ref][math]::Max(1000,
    [int](Get-ChildItem -Path $Folder -Filter *.sql -File|
            Where-Object Name -match '^(\d{4}) - sp - C -' |
            Select-Object @{n='Count';e={$Matches[1]}} -Last 1).Count)

Get-ChildItem -Path $Folder -Filter *.sql -File |
  Where-Object Name -NotMatch '^\d{4} - sp - C - ' |
    Rename-item -Newname {"{0:D4} - sp - C - {1}" -f ++$Count.Value,$_.Name}

推荐答案

  1. ^\d{4}正则表达式
    ^行开始处的锚点
    \d代表数字
    {4}是一个量词,恰好指定了前面的4个数字,此处为数字
    ()括号标记捕获组

  1. ^\d{4} is a Regular Expression
    ^ anchors at line begin
    \d represents a digit
    {4} is a quantifier, specifying exactly 4 of the previous, here digits
    () parentheses mark a capture group

Get-Help about_Comparison_Operators引用

其中$Matches[1]代表RegEx的第一个捕获组.
Select-Object根据匹配项(4位数字)构建计算所得的属性,并且仅使用最后/最高

Where $Matches[1] represents the 1st capture group of the RegEx.
The Select-Object builds a calculated property from the match (4 digit number) and only uses the last/highest

获得以前使用的最高数字是我应该解释得更好的第一名的奖金.
由于获取的数字现在包含4位数字(不再有固定的1和3个零),因此-format 运算符(简称为-f)用于通过插入变量内容代替{x}(其中x是从零开始的数字)来构建新文件名.

Obtaining the previously used highest number was a bonus I should have explained better 1st place.
As the obtained number now contains 4 digits (no more a fixed 1 and three zeroes) the -format operator (shorted to just -f) is used to build the new file name by inserting variable contents in place of the {x} where x is a zero based number.

这篇关于Powershell数字符号匹配和重命名项目逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:45