Tuesday 10 March 2009

SET-Extension.ps1

  1. <#
  2. .SYNOPSIS
  3. Changes file extension
  4. .DESCRIPTION
  5. This script uses the ChangeExtension method to change a file extension
  6. .NOTES
  7. File Name : Change-Extension.ps1
  8. Author : Thomas Lee - tfl@psp.co.uk
  9. Requires : PowerShell V2 CTP3
  10. .LINK
  11. http://pshscripts.blogspot.com/2009/03/change-extensionps1.html
  12. .EXAMPLE
  13. PSH [C:\foo]: .\Change-Extension.ps1
  14. Set Extension(C:\mydir\myfile.com.extension,'.old') returns 'C:\mydir\myfile.com.old'
  15. Set Extension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
  16. Set Extension(C:\mydir\, '.old') returns 'C:\mydir\.old'
  17. #>
  18. ##
  19. # Start of Script
  20. ##
  21. # Setup file names
  22. $goodFileName = "C:\mydir\myfile.com.extension"
  23. $badFileName = "C:\mydir\"
  24. # Change file name extensions
  25. $result = [System.IO.Path]::ChangeExtension($goodFileName, ".old")
  26. Set Extension({0},'.old') returns '{1}'" -f $goodFileName, $result
  27. $result = [System.IO.Path]::ChangeExtension($goodFileName, "")
  28. Set Extension({0}, '') returns '{1}'" -f $goodFileName, $result
  29. $result = [System.IO.Path]::ChangeExtension($badFileName, ".old")
  30. Set Extension({0}, '.old') returns '{1}'" -f $badFileName, $result
  31. # End of Script

No comments: