Geeks With Blogs

News
The Wrecking Bawl Destructuring query language, one keyword at a time.

Here's a Powershell script I whipped up to copy all bak files from C:\TEMP to Y:\DEST.  It checks to make sure the file is present at the destination before it deletes it from the source.  I placed it in a ps1 (the extension for Powershell files) file and scheduled a task to execute a batch file that executes the ps1 file.  The reason I had to check for null is that Powershell is kinda dumb and if the source folder is empty it picks it up as a child and performs the Copy-item.

$oldPath = "C:\TEMP"
$newPath = "Y:\DEST"
$GciFiles = get-childitem $oldPath

foreach ($file in $GciFiles){
  Write-host $file.Name
  if(!($file.Name -eq $null) -and $file.Name.EndsWith(".bak")){
      Write-host "copying item "+($newPath+'\'+$file)
      Copy-item $oldPath\$file $newPath
      if (Test-path ($newPath+'\'+$file)) {
          Write-host "removing item "+($oldPath+'\'+$file)
          Remove-item ($oldPath+'\'+$file) -recurse
      }
  }
}

Posted on Wednesday, April 15, 2009 4:57 AM all tech stuff | Back to top


Comments on this post: powershell cut and paste

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Alex Bransky | Powered by: GeeksWithBlogs.net