-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRename-Items.ps1
65 lines (51 loc) · 1.7 KB
/
Rename-Items.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.SYNOPSIS
Renames the files passed in using regular expressions.
.DESCRIPTION
Uses the InPattern and OutPattern parameters to rename a file list. These can be regular expressions. This script has the option to preview changes by using the -n option.
.PARAMETER Files
The list of files to rename.
.PARAMETER n
A switch to only display the output of the renaming action without actually making changes.
.PARAMETER InPattern
The pattern to match in the passed in filenames.
.PARAMETER OutPattern
The replacement expression
.EXAMPLE
PS C:\SomeDirectory> ls | rename -n "little" "BIG"
Lists the changes that would be made by replacing "little" with "BIG" in all files in the current directory.
.EXAMPLE
PS C:\SomeDirectory> ls | rename "Some-Prefix(.*)Some-Suffix" "`$1"
Renames all files in the current directory by removing the prefix "Some-Prefix" and the suffix "Some-Suffix".
#>
param
(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[alias('FullName')]
[string[]] $Files,
[switch] $n,
[Parameter(Position=1, Mandatory=$true)]
[string] $InPattern,
[Parameter(Position=2, Mandatory=$true)]
[AllowEmptyString()]
[string] $OutPattern
)
process
{
foreach($file in $Files)
{
$oldName = $(Get-Item $file).Name
$newName = $oldName -Replace $InPattern, $OutPattern
if ($oldName -cne $newName)
{
if ($n.IsPresent)
{
$oldName + " will be renamed as " + $newName
}
else
{
Rename-Item $file $newName
}
}
}
}