Showing posts with label Replace. Show all posts
Showing posts with label Replace. Show all posts

Thursday, 14 November 2013

Fix-FileName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Defines a function to remove 'invalid' characters 
  4.     from a file name. 
  5. .DESCRIPTION 
  6.     Some programs do not like certain 'invalid' characters 
  7.     in a file name used by that application. The function 
  8.     takes a look at each the file name and replaces some invalid 
  9.     characters with '-'
  10.  
  11.     This function takes a file name and 'fixes' it and returns 
  12.     the 'fixed' file name. Needless to say the characters to match 
  13.     and what to replace them with is an application specific decision! 
  14. .NOTES 
  15.     File Name  : Fix-FileName.ps1 
  16.     Author     : Thomas Lee - tfl@psp.co.uk 
  17.     Requires   : PowerShell Version 3.0 
  18. .LINK 
  19.     This script posted to: 
  20.         http://www.pshscripts.blogspot.com 
  21. .EXAMPLE 
  22.     Psh> .\Fix-FileName.ps1 
  23.     File name was: 123{}{{{|[\] 
  24.     Fixed name is: 123-------- 
  25.  
  26. #> 
  27.  
  28.  
  29. Function Fix-FileName { 
  30. [CMdletbinding()] 
  31. Param ( 
  32. $fn = $(throw 'no file name specified - returning'
  33.  
  34. Switch -Regex ($fn) { 
  35.   "}"  { $fn = $fn -replace '{','-'  } 
  36.   "}"  { $fn = $fn -replace '}','-'  } 
  37.   "\]" { $fn = $fn -replace ']','-'  } 
  38.   "\[" { $fn = $fn -replace '\[','-'
  39.   "\\" { $fn = $fn -replace '\\','-' } 
  40.   "\|" { $fn = $fn -replace '\|','-' } 
  41. } 
  42. $fn 
  43. } 
  44.  
  45. $fn = "123{}{{{|[\]" 
  46. $fnf = Fix-FileName $fn 
  47. "File name was: $fn" 
  48. "Fixed name is: $fnf

Saturday, 29 May 2010

Replace-String.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates how to do .REPLACE() in a string. 
  4. .DESCRIPTION 
  5.     This script is a MSDN sample, recoded in PowerShell 
  6. .NOTES 
  7.     File Name  : Replace-String.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/05/replace-stringps1.html 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/fk49wtc1%28VS.80%29.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Replace-String.ps1 
  17.     The original string is: 
  18.     This docment uses 3 other docments to docment the docmentation 
  19.      
  20.     After correcting the string, the result is: 
  21.     This document uses 3 other documents to document the documentation 
  22. #> 
  23.  
  24. ## 
  25. # Start of script 
  26. ## 
  27.   
  28. # Create a string complete with misspelling and display it 
  29. $errString = "This docment uses 3 other docments to docment the docmentation" 
  30. "The original string is:`n{0}" -f $errString 
  31. "" 
  32.  
  33. # Correct the spelling of "document" using .Replace() and display  
  34. # the corrected string 
  35. $correctString = $errString.Replace("docment", "document"
  36. "After correcting the string, the result is: `n{0}" -f $correctstring 
  37. # End of script