Thursday 9 July 2009

Copy-File.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the usage of the Exists and the 
  4.     copy methods of System.IP.File   
  5. .DESCRIPTION 
  6.     This script sets up two file names, then checks to see if 
  7.     the sorucefile exists. if so, it's copied to a new file. 
  8. .NOTES 
  9.     File Name  : copy-file.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Copy-File.PS1' 
  19.     Source File (c:\foo\Test.txt) copied to (c:\foo\Test2.txt) 
  20. #> 
  21.  
  22. ## 
  23. # start of script 
  24. ## 
  25.  
  26. # Setup source and destination files 
  27. $SourceFile = "c:\foo\Test.txt"
  28. $NewFile    = "c:\foo\Test2.txt"
  29.  
  30. # Now - check to see if $Sourcefile exists, and if so, 
  31. # copy it to $newfile  
  32. if ([System.IO.File]::Exists($SourceFile))  { 
  33.    [System.IO.File]::Copy($SourceFile, $NewFile
  34.    "Source File ($SourceFile) copied to ($newFile)" 
  35. else
  36. "Source file ($Sourcefile) does not exist." 
  37. # End of script 

No comments: