File copy using Microsoft Powershell





2.00/5 (1 vote)
Powershell script to copy files greater than certain date
Introduction
Microsoft Powershell is a nifty scripting tool which is deeply integrated with .NET framework. Recently I had an opportunity to write an automated script for copying files from one location to another. This is a rudimentary task, and there are a ton of tested approaches such as robocopy etc., but I decided to write my own script in powershell.
Background
File copy in Powershell (ps) is relatively straightforward.
copy-Item c:\ source\file.txt c:\destination
The task I had to do was to copy the most recent files from a folder on a FTP site to a destination folder on a daily basis. The copied file was used by a SSIS package and later purged. There is no set frequency of how many files to expect each day and some days there might be no file at all. The script should copy only the new files.
Using the code
Create a text file call it LogTimeStamp.txt in the destination folder. This
text file is used to for logging the most recent timestamp of the file copied.
Step1:
Define variables to set the source and destination directories and an empty hash table. Set the last time stamp from the previous operation to a variable “$lastdate”.
# variables $SrcDir = 'C:\source' $DestDir = 'C:\dest\OIT_Files' # empty hash table $ht = @{} # Get the last time stamp from log file $LastDate = Get-Content C:\dest\LogTimeStamp.txt
Step 2:
Get all the files from the destination greater than the last time stamp. Powershell supports piped operations and some neat SQL/LINQ type query operations.
# Get all files greater than $LastDate and copy to dest Get-ChildItem -Recurse $SrcDir | Where-Object{$_.LastWriteTime -gt $LastDate} | Copy-Item -Destination $DestDir
Step 3:
From the source directory sort the lastwritetime property of each file and load it into the hash table. The key is the extension of the file and value is the lastwritetime.
# load lastwritetime to hash table from source folder Get-ChildItem -Recurse $SrcDir | sort lastwritetime |foreach { $ht[$_.Extension] = $_.LastWriteTime}
Step 4:
The last step is to clear the LogTimeStamp.txt file and update with the latest time stamp from the destination.
#Clear and update the time stamp Clear-Content C:\dest\LogTimeStamp.txt # Add the latest time stamp to log file Add-Content C:\dest\LogTimeStamp.txt ($ht.GetEnumerator() | Sort value -Descending | select -First 1).Value
Source Code
The complete code can be found here: https://github.com/tkmallik/Powershell/blob/master/PSFileCopy.ps1