我对powershell相当陌生,我一直在尝试创建一个脚本,该脚本将标题中包含数字的目录中的所有文件重新命名为模板。
我的剧本看起来是这样的:
$NewObjName = "My New Name"
cd E:\Test
dir | ren -NewName {$_.name -replace '([a-zA-Z])([a-zA-Z])\.(\d?)(\d).*','0$3$4'}
dir | ren -NewName {$_.name -replace '(\d?)(\d)(\d)',{Part $2$3 $NewObjName.txt}}做这件事:
PS E:\Test> ls
Directory: E:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/1/2022 2:14 PM 0 AB.01CDE
-a---- 8/1/2022 2:15 PM 0 AB.02CDE
-a---- 8/1/2022 2:15 PM 0 AB.10CDE
-a---- 8/1/2022 2:15 PM 0 AB.11CDE
PS E:\Test> E:\RenameScript.ps1
PS E:\Test> ls
Directory: E:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/1/2022 2:14 PM 0 Part 01 $NewObjName.txt
-a---- 8/1/2022 2:15 PM 0 Part 02 $NewObjName.txt
-a---- 8/1/2022 2:15 PM 0 Part 10 $NewObjName.txt
-a---- 8/1/2022 2:15 PM 0 Part 11 $NewObjName.txt
PS E:\Test> 所以它只是放入变量名,而不是变量的值。我不确定-replace是不允许正则表达式分组以外的变量,还是只是遗漏了什么?
(预先谢谢:)
编辑:我的预期结果如下:
PS E:\Test> ls
Directory: E:\Test
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/1/2022 2:14 PM 0 Part 01 My New Name.txt
-a---- 8/1/2022 2:15 PM 0 Part 02 My New Name.txt
-a---- 8/1/2022 2:15 PM 0 Part 10 My New Name.txt
-a---- 8/1/2022 2:15 PM 0 Part 11 My New Name.txt 发布于 2022-08-01 19:58:54
$_.name -replace '(\d?)(\d)(\d)',{Part $2$3 $NewObjName.txt}
{ ... }是https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Script_Blocks文本,而不是字符串文本。- When a script block is used in a context where it is coerced to a string, such as in your case, its _verbatim content_ (sans `{` and `}`) is used. Therefore, `$NewObjName` was _not_ expanded (interpolated). - While it can be convenient to use `{ ... }` in lieu of an actual string literal in order to avoid the need for escaping of embedded quoting, it is best avoided in the interest of conceptual clarity.- As an aside: In _PowerShell (Core) 6.1+_, script blocks _are_ now supported as the replacement operand of the [`-replace`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Comparison_Operators#replacement-operator) operator, albeit _as script blocks_, i.e. they provide a piece of code to run for a each match in order to dynamically determine the replacement text - see [this answer](https://stackoverflow.com/a/58608437/45375) for an example.- However, `$2` and `$3` - even tough they look like PowerShell variables - are actually placeholders for what the regex operation matched, and therefore must be _passed through_ to the .NET regex engine.- To that end (to prevent up-front expansion by PowerShell), they must be escaped as ``$2` and ``$3`, using ```, the so-called _backtick_, PowerShell's [escape character](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Special_Characters).- Note: - If you use a [verbatim (single-quoted) string (`'...'`)](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Quoting_Rules#single-quoted-strings) - which is the best choice if interpolation is _not_ needed - placeholders such as `$2` and `$3` can be used as-is. - As an alternative to the expandable string solution below, you can conceptually separate the aspect of up-front expansion and the verbatim string to pass to the .NET regex engine via `-f`, the [format operator](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#format-operator--f):与:“$2$3 $NewObjName.txt”('Part 2$3$ {0}.txt‘-f $NewObjName)相同
因此:
$_.name -replace '(\d?)(\d)(\d)', "Part `$2`$3 $NewObjName.txt"https://stackoverflow.com/questions/73198710
复制相似问题