PoshCode Archive  Artifact Content

Artifact bd984d535d9341e7faacd6159ea0384dc66080311ae4da6f2f310f9c75caefed:

  • File Set-Extension.ps1 — part of check-in [2977437172] at 2018-06-10 13:08:37 on branch trunk — Change the extension on files (user: unknown size: 1408)

# encoding: ascii
# api: powershell
# title: Set-Extension
# description: Change the extension on files
# version: 0.1
# type: function
# license: CC0
# function: Set-Extension
# x-poshcode-id: 2314
# x-archived: 2010-10-21T23:39:26
#
#
function Set-Extension { 
#.Synopsis
#  Change the extension on file paths
#.Example
#  ls *.ps1 | Set-Extension bak | ls
#
#  Lists all .bak files that correspond to .ps1 scripts
#.Example
#  ls *.txt | move -dest { Set-Extension log -path $_.PSPath }
#
#  Renames all .txt files by changing their extension to .log
#.Example
#  ls *.txt -Rename
#
#  Renames all .txt files by changing their extension to .log
#.Parameter Path
#  The file path to change the extension on
#.Parameter Extension
#  The new extension to use
#.Parameter Rename
#  Actually rename the file instead of just outputting the altered path
param(
   [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Mandatory=$true)]
   [Alias("FullName")]
   [string]$Path
,
   [Parameter(Mandatory=$true,Position=0)]
   [string]$Extension
,
   [Parameter()]
   [switch]$Rename
,
   [Parameter()]
   [switch]$Passthru
) 
process { 
   if($Rename) {
      Move-Item -Literal $Path -Destination ([IO.Path]::ChangeExtension($path, $extension)) -Passthru:$Passthru
   } else {
      [IO.Path]::ChangeExtension($path, $extension) 
   }
}
}