Click here to Skip to main content
12,360,223 members (62,334 online)
Click here to Skip to main content
Add your own
alternative version

Stats

21.1K views
2 bookmarked
Posted

File copy using Microsoft Powershell

, 23 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to 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 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Venkata Turlapati
Database Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 23 Aug 2013
Article Copyright 2013 by Venkata Turlapati
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid