简体   繁体   English

如何在Powershell中的-Newname中包含变量

[英]How to include variable in -Newname in powershell

Via power-shell script, trying to add $Album variable to naming sequence. 通过power-shell脚本,尝试将$ Album变量添加到命名序列。

Tried write host, variable is working. 尝试写入主机,变量正在工作。 Have tried things like () [] {} "" '' . 尝试过类似()[] {}“”“之类的东西。 ect. 等。

The goal is to get $Album to work in this line below: {0:D2}$Album.mxf 目标是使 $Album 在下面的这一行中工作: {0:D2}$Album.mxf

$i = 1

    $Artist = " Name"
    $Type = "Type"
    $Location = "Loc"
    $Month = "Month"
    $Year = "2019"
    $Album = "$Artist $Type $Location $Month $Year"

# Write-Host -ForegroundColor Green -Object $Album;

Get-ChildItem *.mxf | %{Rename-Item $_ -NewName ('{0:D2}$Album.mxf' -f $i++)}

Before: 之前:

  • Misc name - 1.mxf 杂项名称-1.mxf
  • Misc name - 4.mxf 杂项名称-4.mxf
  • Misc name - 6.mxf 杂项名称-6.mxf

Current: 当前:

  • 01$Album.mxf 01 $ Album.mxf
  • 02$Album.mxf 02 $ Album.mxf
  • 03$Album.mxf 03 $ Album.mxf

Goal: 目标:

  • 01 Name Type Loc Month 2019.mxf 01名称类型Loc Month 2019.mxf
  • 02 Name Type Loc Month 2019.mxf 02名称类型Loc Month 2019.mxf
  • 03 Name Type Loc Month 2019.mxf 03名称类型Loc Month 2019.mxf

Or like this (works both in an interactive session and in a script). 或者像这样(在交互式会话和脚本中都可以工作)。 And you want a space before $album . 并且您想要$album之前的空间。

Get-ChildItem *.mxf | Rename-Item -NewName { "{0:D2} $Album.mxf" -f $script:i++ }

EDIT: There is another way. 编辑:还有另一种方法。 It is little known that foreach-object can take 3 script blocks (begin/process/end). 鲜为人知的是foreach-object可以占用3个脚本块(开始/过程/结束)。 -process can take a script block array as documented. -process可以采用记录的脚本块数组。 You can always test these things with -whatif. 您始终可以使用-whatif测试这些东西。

Get-ChildItem *.mxf | 
foreach { $i = 1 } { Rename-Item $_ -NewName ("{0:D2} $Album.mxf" -f $i++) -whatif }  

Your own answer is effective, but it is awkward in two respects: 您自己的答案是有效的,但在两个方面都很尴尬:

  • It mixes expandable strings (string interpolation inside "..." ) with template-based string formatting via the -f operator. 它通过-f运算符将可扩展字符串"..."内的字符串插值)与基于模板的字符串格式混合在一起。

  • It uses % ( ForEach-Object ) to launch Rename-Item for every input object, which is quite inefficient. 它使用%ForEach-Object )为每个输入对象启动Rename-Item ,这效率很低。

Here's a solution that provides a remedy, by using -f consistently and by using a delay-bind script block : 这是一个解决方案,可以通过始终使用-f并使用delay-bind脚本块来提供补救措施:

$Artist = " Name"
$Type = "Type"
$Location = "Loc"
$Month = "Month"
$Year = "2019"
$Album = "$Artist $Type $Location $Month $Year"

$i = 1
Get-ChildItem *.mxf |
  Rename-Item -NewName { '{0:D2} {1}.mxf' -f ([ref] $i).Value++, $Album }

Note the use of ([ref] $i).Value++ to increment the value of $i , which is necessary, because the delay-bind script block passed to -NewName runs in a child scope - see this answer for details. 注意使用([ref] $i).Value++来增加$i的值是必要的,因为传递给-NewName的delay-bind脚本块在子作用 -NewName运行-有关详细信息,请参见此答案

Note that $script:i++ is a pragmatic alternative, but less flexible than the solution above - see the linked answer. 请注意, $script:i++是一种实用的选择,但比上面的解决方案更不灵活-请参阅链接的答案。

Answered by @Theo in comment 回复@Theo的评论

  • Use double-quotes here "{0:D2}$Album.mxf" so the variable $Album gets expanded. 在此处使用双引号"{0:D2}$Album.mxf"以便扩展变量$ Album。
$i = 1

    $Artist = " Name"
    $Type = "Type"
    $Location = "Loc"
    $Month = "Month"
    $Year = "2019"
    $Album = "$Artist $Type $Location $Month $Year"

# Write-Host -ForegroundColor Green -Object $Album;

Get-ChildItem *.mxf | %{Rename-Item $_ -NewName ("{0:D2}$Album.mxf" -f $i++)}

Before: 之前:

  • Misc name - 1.mxf 杂项名称-1.mxf
  • Misc name - 4.mxf 杂项名称-4.mxf
  • Misc name - 6.mxf 杂项名称-6.mxf

After: 后:

  • 01 Name Type Loc Month 2019.mxf 01名称类型Loc Month 2019.mxf
  • 02 Name Type Loc Month 2019.mxf 02名称类型Loc Month 2019.mxf
  • 03 Name Type Loc Month 2019.mxf 03名称类型Loc Month 2019.mxf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM