Select Page

 

If you read our blog “Automate Moving Files” you will know that our original solution did not meet the customer’s need. We needed to integrate a solution with their existing infrastructure. While we achieved the initial request to move files, we found out that instead of moving files we needed to copy files.

This was a very simple change by changing the cmdlet from Move-Item to Copy-Item on line 65.

We know that you just want a solution and might not want to know what a cmdlet is. However, if you did want to know a little more, Microsoft defines a cmdlet as “A cmdlet is a lightweight command that is used in the PowerShell environment. The PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line”.

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 montiored will constantly be checked for new PDF files.
2. It copies a PDF file from the source folder to the destination folder.
3. The file that is copied islogged 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 copy 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:\Users\jonsl\Desktop\source”

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

# 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:\Users\jonsl\Desktop\logFile\file-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

# copy the file to a specific folder
Copy-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

For more info on cmdlets you can reference the Cmdlet Overview by Microsoft. Here is the link.

To read more about the Copy-Item cmdlet and all of its parameters, take a look at the Copy-Item documentation by Microsoft. Here is the link.