Select Page


Recently we had a customer ask us to automate the movement of PDF files from one folder to another. They had already invested in an off the shelf solution to automate the downloading of files sent to their FTP site. They wanted to move the files from the FTP download to their production system. We came up with a solution using PowerShell.

Before we begin, we want to make it a point to state that the purpose of this blog post – and the purpose of all our blogs – will be to try and benefit others without being too technical in our explanation. We want to try and translate different technical solutions into a means for a person without advanced technical skills to be able to understand and implement. In other words, these aren’t going to be too technical but hopefully explain it enough that can benefit others.

So here goes

PowerShell can be used on Windows, Apple Mac OS, and Linux. For this blog, we will focus on Windows as our customer is using Windows. PowerShell is free and if you have Windows 7 or higher, you have it.

Side note: if you are using Windows 7 or earlier, you should upgrade if possible. PowerShell allows you to manipulate the Windows operating system from a command-line interface. However, the code we are going to show you doesn’t require extensive knowledge of using a command line. Remember, we want you to implement this without you having to do a lot of research. There are many great in-depth articles about what PowerShell is, and many great in-depth articles of different applications for PowerShell available if you want to dive into it. This is not one of them.

Cut to the chase

In the script that follows, text in between these marks <# and #> are comments. Lines of text preceded with # are also comments. The comments are meant to explain the functionality of what the code that follows actually does. Comments are not executed. For you to customize this solution you only need to focus on changing the paths marked in Section A. You can change the filter if you are wanting to move file types other than pdf. You can do this in Section B.

Expand to see the code solution

<#
The purpose of this script is really three parts.

1. It creates a watched/hot folder. This means that the folder identified as the folder to be monitored will constantly be checked for new PDF files.
2. It moves a PDF file from the source folder to the destination folder.
3. The file that is moved is logged to the log file

To run this script you can either run from powershell.exe or from PowerShell ISE.

Either case, Powershell needs to be launched and running this script.

If the script is running, you can use Unregister-Event FileAdded from the command prompt to unregister the hot folder watcher.

You can also close the script to unregister the watcher. Unregister means that no monitoring will take place.

#>

#——————————————————————————————

# Section A

# set in this section three things, folder to watch, the folder where to move the file and path to log file

# add path to folder where PDF files will be dropped in
# to customize this for your use, replace the text in between the quotes to the path of your source folder.
$source_folder = “C:UsersjonslDesktopsource”

# add path to folder where the file will be moved
# to customize this for your use, replace the text in between the quotes to the path of your destination folder.
$destination_folder = “C:UsersjonslDesktopdestination”

# add path to log file
# to customize this for your use, replace the text in between the quotes to the path of your log file folder.
$log_file = “C:UsersjonslDesktoplogFilefile-copy-log.txt”

# the script log file has 3 columns – TAB delimited – TimeStamp FileName MovedTo
# TimeStamp date time stamp of this event
# FileName name of file that is dropped in folder to watch
# MovedTo full path of moved file

# this writes the column headers to the log file
Out-File -FilePath $log_file -Append -InputObject “TimeStamp`tFileName`tMovedTo”

 

#——————————————————————————————

# Section B

# set monitoring folder action when CREATED event it is triggered
$createdAction =

{
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated

# This is a brief 10 second pause to allow the file write to complete before moving
# you can change the amount of time by changing the 10 to any othernumber. The unit of time is in seconds.
Start-Sleep -s 10

# move the file to a specific folder
Move-Item -path $($source_folder + “” + $name) -destination $destination_folder -force

# write to log file , tab delimited ,
Out-File -FilePath $log_file -Append -InputObject “$timeStamp`t$name`t$where_to_move_folder$name”

}

# set file mask to pdf files – note if you have different file types to move – change the filter
# for example – if you only want to move jpg files change the filter to ‘*.jpg’
$filter = ‘*.pdf’

# create a FileSystemWatcher event
$fsw = New-Object IO.FileSystemWatcher

$fsw.Path = $source_folder
$fsw.Filter = $filter
$fsw.IncludeSubdirectories = $false #note if you want to watch folders within your source folder – change $false to $true
$fsw.NotifyFilter = [IO.NotifyFilters]’FileName, LastWrite’

 

# Subscribe to the events that are generated by a Microsoft .NET Framework object
Register-ObjectEvent $fsw -EventName Created -SourceIdentifier FileAdded -Action $createdAction

$fsw.EnableRaisingEvents = $true

# the line below is used to unregister the watcher, copy it and paste it in the PowerShell command prompt
#Unregister-Event FileAdded

Resolution achieved?

Did this solve the customer’s problem? No, it did not. The off the shelf solution the customer had implemented was a sync program. In other words, it constantly checked to make sure the folder contents were the same. So, moving a file would cause the off the shelf program to re-download the file. In other words, it constantly kept downloading the file. Instead of a move script, we needed a copy script. That will be our next blog.