在Powershell中使用new

编程入门 行业动态 更新时间:2024-10-26 20:33:35
本文介绍了在Powershell中使用new-guid重命名每个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在尝试使用文件名$New-Guid .txt重命名文件夹中的每个txt文件,因此它们都应该具有唯一的名称.

有人知道这样做的简单方法吗?

解决方案

tl; dr

Get-ChildItem ... | Rename-Item -NewName { "$(New-Guid).txt" }

请注意使用脚本块({ ... })以及对$(...)内部$(...)中的New-Guid的调用,该脚本嵌入在 可扩展字符串("...").

使用 延迟绑定脚本块 ,其中传递给-NewName/-Destination参数的脚本块({ ... })计算每个输入文件的新名称/名称和/或位置 ,从而可以指定新文件名:

  • 作为 input 文件名的转换
  • 和/或作为自动生成的文件名,该文件名基于例如序列号或您的GUID.

以下仅讨论Rename-Item -NewName,但类似地适用于Move-Item -Destination,但请注意一个重要区别:

    Rename-Item的
  • -NewName为位于其当前位置的每个输入项 指定新的名称.

    Move-Item的
  • -Destination为每个输入项指定新的 path ,并指定 relative 路径,包括一个纯文件 name ,与调用者的位置有关 ,与输入文件的位置无关.

常规模式是:

Get-ChildItem ... | Rename-Item -NewName { ... }

请注意,输入文件也可以通过Get-Item,路径 strings 或具有.Path属性的对象指定.

转化示例:

假设您要通过使用foo-前缀为当前目录中的所有*.txt文件重命名.

在脚本块{ ... }中,自动变量$_指代手边的输入对象,如PowerShell中的习惯. 使用Get-ChildItem中的文件输入,$_是类 [System.IO.FileInfo] ,允许您访问其.Name属性以获取文件 name .

Get-ChildItem *.txt | Rename-Item -NewName { 'foo-' + $_.Name }

自动生成示例,其中包含序列号 :

假设您要重命名当前目录中的所有*.txt文件,方法是在文件名前添加一个基于数字的1序列号,后跟-,例如01-.

$index = 0 Get-ChildItem *.txt | Rename-Item -NewName { '{0:00}-{1}' -f ++([ref] $index).Value, $_.Name }

注意:通过++([ref] $index).Value而不是仅通过++$index递增$index是必要的,因为延迟绑定脚本块在 child 变量范围内运行,其中++$index会隐式创建一个 local $index变量-有关详细说明,请参见此答案.

I have been trying to rename each txt file in a folder with the filename $New-Guid.txt, so they should all have unique names.

Anyone know the simple way to do this?

解决方案

tl;dr

Get-ChildItem ... | Rename-Item -NewName { "$(New-Guid).txt" }

Note the use of a script block ({ ... }) and the call to New-Guid inside $(...) embedded in an expandable string ("...").

File renaming / moving operations with multiple input files are most efficiently performed with delay-bind script blocks, where a script block ({ ... }) passed to the -NewName / -Destination parameter calculates the new name / name and/or location for each input file, which enables specifying the new filename:

  • as a transformation of the input filename
  • and/or as an auto-generated filename based on, e.g., a sequence number, or, as in your case, a GUID.

The following discusses only Rename-Item -NewName, but it applies analogously to Move-Item -Destination, but note an important difference:

  • -NewName for Rename-Item specifies the new name for each input item in its current location, wherever that is.

  • -Destination for Move-Item specifies the new path for each input item, and specifying a relative path, including a mere file name, is relative to the caller's location, irrespective of where the input files are located.

The general pattern is:

Get-ChildItem ... | Rename-Item -NewName { ... }

Note that input files may alternatively be specified via Get-Item, as path strings, or as objects with a .Path property.

Transformation example:

Let's say you want to rename all *.txt files in the current directory by prefixing their names with foo-.

Inside the script block { ... }, automatic variable $_ refers to the input object at hand, as is customary in PowerShell. With file input from Get-ChildItem, $_ is an instance of class [System.IO.FileInfo], allowing you to access its .Name property to get the file name.

Get-ChildItem *.txt | Rename-Item -NewName { 'foo-' + $_.Name }

Auto-generation example with sequence number:

Let's say you want to rename all *.txt files in the current directory by prefixing their names with a 1-based two-digit sequence number followed by -, e.g., 01-.

$index = 0 Get-ChildItem *.txt | Rename-Item -NewName { '{0:00}-{1}' -f ++([ref] $index).Value, $_.Name }

Note: Incrementing $index via ++([ref] $index).Value rather than just ++$index is necessary, because delay-bind script blocks run in a child variable scope, where ++$index would implicitly create a local $index variable - for a more detailed explanation, see this answer.

更多推荐

在Powershell中使用new

本文发布于:2023-11-05 19:24:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Powershell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!