Powershell Script Help

Posted on 2012-09-15
Last Modified: 2012-10-14

I have the following powershell script which executes a command when a new file is created / uploaded / moved to a specified location. The command executes a SSIS package which needs the entire file contents to be present before running. My issue is, I do not want it to execute the command until the move / upload is completely finished. Sometimes large files get uploaded (5gb) and if the powershell script executes the command before the entire file move / upload is complete, the SSIS package will fail.

How can I modify this powershell script to wait until the entire file has been uploaded / moved?

$folder = 'W:\Uploads'
$filter = '*.*'                             # <-- set this according to your requirements
$cmd = 'dtexec.exe /FILE W:SSIS\Import.dtsx'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
 IncludeSubdirectories = $false             # <-- set this according to your requirements
 NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
 invoke-expression -command $cmd

Open in new window

Question by:brandonc7

    Author Comment

    Modified original question

    Author Comment

    Maybe a better way to say this is to make sure the file is not 'locked' by another application?
    LVL 2

    Accepted Solution

    You assume that the file will always get uploaded completely at some time. But this may not be the case. The file may be uploaded partially even if another application stopped writing to it.
    Or it may be damaged. You need to check file integrity in some way.
    One way is to get the file archived before upload, and check archive integrity after upload.
    Another way is to get md5- or sha hash along with the file.
    LVL 76

    Expert Comment

    One way is to look for a change file that was modified a certain time before, I.e. look at the modify date on the file to be five minutes ago this way you will be certain that no process is still writing to it.
    You do not want to act on created.

    Author Comment

    Hmmmmm good point. The uploads are being done via FTP so I am limited because up to client as far as what they send. My ssis script is smart enough to catch a damaged file I think. That being said, is it possible to just modify this power shell script to detect lock / unlock status?
    LVL 39

    Expert Comment

    LVL 67

    Assisted Solution

    AFAIK FTP does not lock a file while transferring.
    That is the reason we usually take an approach of copying the file to a different file name or folder, and after finishing let the FTP script perform a local rename/move to the target name. Another approach is to create a "lock file" before starting the transfer, and deleting it when finished. Of course that requires that you have control over the FTP script, which seems not to be an option here.

    Sadly, FSW does not support a scanning interval, delayed triggering or triggering on no change for some time ... So the only reliable way IMO is to have a scheduled task (or looping script) checking for files older than e.g. 2 seconds. Or wait in the FSW event for at least 1 second, and recheck if the LastWrite date has changed.
    LVL 16

    Assisted Solution

    You could use a scriptable FTP server that can run a script when an upload is complete.  Here is a Robo-FTP Server event script that automatically moves uploaded files to another folder:  

    If you had something like that, your powershell script could monitor the destination folder instead of the FTP upload folder.... or you could just make a Robo-FTP Server event script launch the powershell script directly and pass in the name of the uploaded file.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Viewers will learn how the fundamental information of how to create a table.
    Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now