A N

November 24, 2014

Copy-Item, Compare-Item, Move-Item

Filed under: Tips — donrsh @ 11:38 am
<#	
	.NOTES
	===========================================================================
	 Created with: 	Powershell ISE Version 4.0
	 Created on:   	2014-11-24 
	 Created by:   	Me
	 Organization: 	Me
	 Filename:     	MoveFiles.ps1
	===========================================================================
	.DESCRIPTION
		The script copies files from source to a temporary folder. It then compare the files and if the 
		result is OK, it will move the files from temporary folder to destination.
#>
$ErrorActionPreference = "stop"

$Source = "C:\utdelat\2\Archived\*"
$Destination = "C:\utdelat\SlutDestination"
$days = "-63" #Everything before this day
$FileName = (Get-Date).tostring(“yyyy-MM-dd-HH-mm-ss”)
$TempFolderDir = "C:\utdelat\Mellan"
 

try
{
	
	
	$TotalSource = Get-ChildItem -Path $Source | Measure-Object
	Write-Host "There is $($TotalSource.count) objects in $Source" -ForegroundColor Green
	Write-Host "Getting list of the files before $days days..." -ForegroundColor Green
	$LastTotal = Get-Childitem -Path $Source | where-object { $_.LastWriteTime -lt (get-date).AddDays($days).date } 
	Write-host "$($LastTotal.count) objects will move from $source" -ForegroundColor Green
	
        if (get-childitem -Path $Source | where-object { $_.LastWriteTime -lt (get-date).AddDays($days).Date })
	    {
			
		$TempFolder = New-Item -itemType Directory -Path $TempFolderDir -Name ($FileName + “---Archive”) -Verbose
		Write-Host "Copying the files to temporary folder $TempFolder" -ForegroundColor Green
		copy-item -Path (Get-Childitem -Path $Source | where-object { $_.LastWriteTime -lt (get-date).AddDays($days).Date }) -destination $TempFolder -force -Verbose
		Write-host "Comparing files in $source with $TempFolder" -ForegroundColor Green
		$diff = Compare-Object -ReferenceObject (dir $source -Recurse | Where-Object { !$_.psiscontainer -and $_.LastWriteTime -lt (get-date).AddDays($days).Date }) -DifferenceObject (dir $TempFolder -Recurse) -property name | Where-Object { $_.sideindicator -eq "<=" }
		
		    if ($diff -eq $null)
		    {			
    		    Write-host "The result is TRUE. Continuing to Move items to $destination" -ForegroundColor Green
			    Move-Item -Path "$TempFolder\*" -destination $Destination -force -Verbose
			}
		        else
		        {
			        Write-Host "Something went wrong. No file copyied to $TempFolder Probably The files are not in sync. " -ForegroundColor Red
		        }

		

	    }
	    else
	    {
		    Write-Host "There is no files less than: $days" -ForegroundColor Red
                    Return
		}
    
	$diff2 = Compare-Object -ReferenceObject (Get-ChildItem $source -Recurse | Where-Object { !$_.psiscontainer -and $_.LastWriteTime -lt (get-date).AddDays($days).Date }) -DifferenceObject (Get-ChildItem $Destination\* -Recurse) -property name | Where-Object { $_.sideindicator -eq "=>" }
                    
             if ($diff2 -eq $null)
             {                      
                 Write-host "The result is TRUE. Continuing to REMOVE items FROM $source with $days days" -ForegroundColor Green
                 Remove-Item  -Path (Get-Childitem -Path $Source | where-object { $_.LastWriteTime -lt (get-date).AddDays($days).Date }) -Verbose -Force
             }
                 else
                  {
                     Write-Host "Something went wrong. No file removed from $Source with $days days. The files are not in sync. " -ForegroundColor Red
                  }
}
catch
{
	
	Write-Host "An error has occurred...  Error details: $_." -ForegroundColor Red
	
	
}

finally
{
	#$TotalDestination = (Get-ChildItem -Path $Destination | where-object { $_.LastWriteTime -lt (get-date).AddDays($days) }) | Measure-Object
	#Write-Host "There is $($TotalDestination.count) objects in $Destination with $days days" -ForegroundColor Green
	Write-Host "End" -ForegroundColor Green
	
}


Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: