- <#
- .SYNOPSIS
- Defines a function to remove 'invalid' characters
- from a file name.
- .DESCRIPTION
- Some programs do not like certain 'invalid' characters
- in a file name used by that application. The function
- takes a look at each the file name and replaces some invalid
- characters with '-'.
- This function takes a file name and 'fixes' it and returns
- the 'fixed' file name. Needless to say the characters to match
- and what to replace them with is an application specific decision!
- .NOTES
- File Name : Fix-FileName.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 3.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- Psh> .\Fix-FileName.ps1
- File name was: 123{}{{{|[\]
- Fixed name is: 123--------
- #>
- Function Fix-FileName {
- [CMdletbinding()]
- Param (
- $fn = $(throw 'no file name specified - returning')
- )
- Switch -Regex ($fn) {
- "}" { $fn = $fn -replace '{','-' }
- "}" { $fn = $fn -replace '}','-' }
- "\]" { $fn = $fn -replace ']','-' }
- "\[" { $fn = $fn -replace '\[','-' }
- "\\" { $fn = $fn -replace '\\','-' }
- "\|" { $fn = $fn -replace '\|','-' }
- }
- $fn
- }
- $fn = "123{}{{{|[\]"
- $fnf = Fix-FileName $fn
- "File name was: $fn"
- "Fixed name is: $fnf"
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label Replace. Show all posts
Showing posts with label Replace. Show all posts
Thursday, 14 November 2013
Fix-FileName.ps1
Saturday, 29 May 2010
Replace-String.ps1
- <#
- .SYNOPSIS
- This script demonstrates how to do .REPLACE() in a string.
- .DESCRIPTION
- This script is a MSDN sample, recoded in PowerShell
- .NOTES
- File Name : Replace-String.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/05/replace-stringps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/fk49wtc1%28VS.80%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Replace-String.ps1
- The original string is:
- This docment uses 3 other docments to docment the docmentation
- After correcting the string, the result is:
- This document uses 3 other documents to document the documentation
- #>
- ##
- # Start of script
- ##
- # Create a string complete with misspelling and display it
- $errString = "This docment uses 3 other docments to docment the docmentation"
- "The original string is:`n{0}" -f $errString
- ""
- # Correct the spelling of "document" using .Replace() and display
- # the corrected string
- $correctString = $errString.Replace("docment", "document")
- "After correcting the string, the result is: `n{0}" -f $correctstring
- # End of script
Labels:
powershell,
PowerShell scripts,
Replace,
System.String
Subscribe to:
Posts (Atom)