- <#
- .SYNOPSIS
- Formats a number using .NET - an MSDN Sample recoded in PowerShell
- .DESCRIPTION
- This script createa a number and formats it in Hex. Then a negative number
- is created and formatted several ways. Uses int32.parse method to do the
- parsing.
- .NOTES
- File Name : Get-ParsedString
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Posted to:
- http://pshscripts.blogspot.com/2008/12/get-parsedstringps1.html
- Script also posted to MSDN at:
- http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles(VS.85).aspx
- .EXAMPLE
- PS c:\foo> .\Get-ParsedString.ps1
- A in hex = 10 in decimal
- ' -42 ' parsed to an int32 is '-42'.
- ' (42) ' parsed to an int32 is '-42'.
- #>
- ###
- # Start of Script
- ###
- # Parse the string as a hex value and display the value as a decimal.
- $num = "A"
- $val = [system.int32]::Parse($num, [System.Globalization.NumberStyles]::HexNumber)
- "{0} in hex = {1} in decimal" -f $num,$val
- # Parse the string, allowing a leading sign, and ignoring leading and trailing white spaces.
- $num = " -42 "
- # create parsing value using value__
- $format = [System.Globalization.NumberStyles]::AllowLeadingSign.value__+
- [System.Globalization.NumberStyles]::AllowLeadingWhite.value__ +
- [System.Globalization.NumberStyles]::AllowTrailingWhite.value__
- $parsing = [System.Globalization.NumberStyles] $format
- # parse and display
- $val = [system.int32]::Parse($num,$parsing)
- "'{0}' parsed to an int32 is '{1}'." -f $num, $val
- # Now try a negative number
- $num = " (42) "
- $format = [System.Globalization.NumberStyles]::AllowParentheses.value__ +
- [System.Globalization.NumberStyles]::AllowLeadingSign.value__ +
- [System.Globalization.NumberStyles]::AllowLeadingWhite.value__ +
- [System.Globalization.NumberStyles]::AllowTrailingWhite.value__
- $parsing = [System.Globalization.NumberStyles] $format
- # And parse/display it
- $val = [system.int32]::Parse($num, $parsing)
- "'{0}' parsed to an int32 is '{1}'." -f $num, $val
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label parsename. Show all posts
Showing posts with label parsename. Show all posts
Monday, 22 December 2008
Get-ParsedString.ps1
Tuesday, 15 July 2008
Unzip-File.ps1
<# .SYNOPSIS This script unzips a particular file from an archive (zip file) into a folder. .DESCRIPTION For this script, c:\foo\zip1.zip must exist and contain test.txt The script unzips test.txt into c:\foo and tests it's creation. NB: if c:\foo\test.txt alreadyexists, you'll be told and asked (by Windows) to confirm your actions .NOTES File Name : unzip-file.ps1 Author : Thomas Lee - tfl@psp.co.uk requires : PowerShell V2 .LINK http://pshscripts.blogspot.com/2008/07/unzip-fileps1.html .EXAMPLE PS C:\foo> .\unzip-file.ps1 Before Copy: Target (C:\foo\test.txt)) does not exist After copy: Target (C:\foo\test.txt) now exists #> ## # Start of Script ## $sh = new-object -com shell.application # Zip File to unzip from: $zipfolder = $sh.namespace("C:\foo\zip1.zip") # where the .zip is $item = $zipfolder.parsename("Test.txt") # the item in the zip $targetfolder = $sh.namespace("c:\foo") # where the item is to go # first does file exist? cd c:\foo $f = ls test.txt -erroraction silentlycontinue "Before Copy:" if ($f) {"Target ($($f.versioninfo.filename)) exists and you will be promoted for an action"} if (!$f) {"Target (C:\foo\test.txt)) does not exist"} # do the copy of zipfile item to target folder $targetfolder.copyhere($item) # check that it exists "After copy:" cd c:\foo $f = ls test.txt -erroraction silentlycontinue if ($f) {"Target ($($f.versioninfo.filename)) now exists"} # End of Script
Labels:
copyhere,
namespace,
parsename,
shell.application
Subscribe to:
Posts (Atom)