Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

rename each file with new-guid in powershell

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ++(Get-Variable -Scope 1 index).Value, $_.Name }

Note: Incrementing $index via ++(Get-Variable -Scope 1 $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 that goes out of scope after every invocation - for a more detailed explanation, see this answer.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...