Smart File Copy


posted by mbaugher
02-04-2013

Downloads: 627
File size: 5.5kB
Views: 2,481

Embed
Smart File Copy
  1. <################################################################################################### 
  2.         Author: Mark A. Baugher 
  3.         Date: 2/4/2013 
  4.         Email: baugher.mark@gmail.com 
  5.  
  6. ###################################################################################################> 
  7.  
  8. <################################################################################################### 
  9.         First we will set the ExecutionPolicy to RemoteSigned, Tell the user we are connecting 
  10.         the PSDrive and use the "pause" start-sleep feature to give that illusion and chance to cancel. 
  11. ###################################################################################################> 
  12.  
  13. set-executionpolicy RemoteSigned -force 
  14. write-host "Now connecting to remote storage location" 
  15. start-sleep
  16. new-psdrive -name Pictures -psProvider FileSystem -Root \\UNCSERVER\ANDPATH 
  17.  
  18. <################################################################################################### 
  19.         In this section we will define our search area and tell the user how many files were found 
  20.         again, the pause is for a last chance to cancel before anything begins. 
  21. ###################################################################################################> 
  22.  
  23. [int]$Count =
  24. $Location = (get-psdrive pictures).root 
  25. $SearchFolders = get-childitem -path C:\Users -recurse | Where-object {$_.FullName -NotLike "*AppData*"
  26. $PictureList = $SearchFolders | Where-Object {$_.extension -like ".JP*" -and $_.length -gt 500KB} 
  27. [int]$TotalPics = ($PictureList | Measure-object).count 
  28. write-host "There are a total of $TotalPics found." 
  29. start-sleep
  30.  
  31. <################################################################################################### 
  32.         Next we will run a ForEach function to take each and every one of the files found and declare 
  33.         a few useful variables for the copy that will occur such as the files full path, a destination 
  34.         path based on the file's LastWriteTime (This may need to be a different field for some one not 
  35.         working with pictures) Example would be "August 2012" for a picture taken sometime in that month. 
  36. ###################################################################################################> 
  37.  
  38.  
  39.  
  40. ForEach ($Picture in $PictureList) 
  41. { 
  42. $Count++ 
  43. $PicLocation = $picture.FullName 
  44. Write-Host "Now Moving $PicLocation" 
  45. $PathToGo = "{0:MMMM yyyy}" -f (Get-Date ((get-item "$PicLocation").LastWriteTime)) 
  46. $destination = "$location\$PathToGo" 
  47. $path = test-Path $destination 
  48.  
  49. <################################################################################################### 
  50.         Nested within the ForEach is a IF function to test and see if the path already exists within 
  51.         the root of the destination. IF it does, it simply copies the file to the folder, and moves 
  52.         on to the next one in the ForEach function. 
  53. ###################################################################################################> 
  54.  
  55. if ($path -eq $true) { 
  56.     write-Host "Directory Already exists" 
  57.     copy-item  $PicLocation $destination 
  58.     }  
  59.  
  60. <################################################################################################### 
  61.         However, if the folder to which the file should be copied does not exisit, the ElseIf will 
  62.         change to the root of the destination and create the folder based on the current files 
  63.         $PathToGo variable, then copy the file to said folder. 
  64. ###################################################################################################> 
  65.     elseif ($path -eq $false) {             
  66.             cd $location 
  67.             mkdir $PathToGo 
  68.             copy-Item  $PicLocation -Destination $destination 
  69.             } 
  70.  
  71. <################################################################################################### 
  72.         Of course when waiting for a large number of files being moved we want to see a progress. 
  73. ###################################################################################################> 
  74.  
  75. Write-Progress -Activity "Moving Pictures" -status "Moving picture $count of $TotalPics" -percentcomplete ($Count / $Totalpics * 100) 
  76. } 
  77.  
  78. <################################################################################################### 
  79.         Once everything has been copied, you may want to be notified. 
  80. ###################################################################################################> 
  81.  
  82. Write-Host "Congrats dude, your pictures are now backed up!" 
  83.  
  84. <##########################################DISCLAIMER############################################### 
  85.         In my sample script I've used pictures due to the fact that it was the original reason I  
  86.         created the script. With pictures from phones, tablets and on multiple computers I needed a  
  87.         simple way to copy them all to a backup server and to the right folder based on picture date. 
  88.         In a personal copy of the script I've used an additional remove-item within an extra IF 
  89.         function which prompts the user for a "Y" or an "N" if they would like to delete the original 
  90.         files.  
  91.          
  92.  
  93.         PLEASE NOTE: To ensure the script does exactly what you need please remember to fully 
  94.         test it's results with test/sample data, source and destinations. Although this script uses  
  95.         only a copy-item CMDLet, it would be easy to mess things up a little while you alter the  
  96.         script for your own purposes. 
  97. ###################################################################################################> 

The script finds all files based on location, type and size that you're looking for. Then for each one evaluates the date of the file, then copies each file to a destination based on the date. If the destination directory doesn't exist, it will first create it, copy, then move on to the remaining files. The script provides a progress of the current overall copy process.

Copyright 2012 PowerShell.com. All rights reserved.