CSV & File System Watcher

by james2305 at 2012-09-12 03:41:56

Hi

I am trying to create a script that will have a file system watcher that listens for creation, change and deletion of csv files in a folder and subfolders. When there is a event I would like the csv file to be copied to another folder on another server and if the csv file name already exists merge the two together leaving the name remaining.

I can’t work out the csv bit can someone help.

Thanks

James
by willsteele at 2012-09-12 03:58:17
Can you start by posting what you have and any errors you may be encountering?
by poshoholic at 2012-09-12 04:47:47
If you have the event handling worked out already, great, that’s the hard part. While we wait for your script and errors, I can share an example of what this might look like.

For CSV file copying/appending, there are a few cmdlets you’ll want to use.

Copy-Item: This will allow you to copy from a local path to a UNC path on another server.
Test-Path: This allows you to check to see if a file exists or not.

Also, if you are using PowerShell version 2, these commands will be necessary:

Get-Content: Get the content of a file, line-by-line
Add-Content: Add content to a file
Select-Object: This does many things, one of which is to allow you to skip a certain number of objects.

If you are using PowerShell version 3 though, you can simply use Export-Csv with the new -Append parameter.

Here’s an example of what copying/appending a csv file might look like in PowerShell version 2 if the csv file is written with a header but with no type information (like when the -NoTypeInformation parameter is used on Export-Csv):

$localCsvPath = 'C:\test.csv'
$remoteCsvPath = '\dc\c$\test.csv'

if (Test-Path -LiteralPath $remoteCsvPath) {
Get-Content -LiteralPath $localCsvPath | Select-Object -Skip 1 | Add-Content -LiteralPath $remoteCsvPath
} else {
Copy-Item -LiteralPath $localCsvPath -Destination $remoteCsvPath
}

Here is the same thing but simplified because of the new -Append parameter on Export-Csv in PowerShell 3:

$localCsvPath = 'C:\test.csv'
$remoteCsvPath = '\dc\c$\test.csv'

if (Test-Path -LiteralPath $remoteCsvPath) {
Import-Csv -LiteralPath $localCsvPath | Export-Csv -LiteralPath $remoteCsvPath -Append -NoTypeInformation
} else {
Copy-Item -LiteralPath $localCsvPath -Destination $remoteCsvPath
}


Note that I haven’t tested this script. It’s just a sample showing how it might look. It isn’t necessarily the fastest way to do this. It’s just something to help you work out your issues.
by james2305 at 2012-09-12 06:37:15
Hi Kirk

Thanks for the replies :slight_smile:

I had got the filesystemwatcher working with an alert coming on the screen and was trying to attack the issue in sections. I couldn’t work out how to get it to pull the file to be put in as the $localCsvPath

Here is what I had so far




$folder = ‘c:\scripts\test’
$filter = ‘*.csv’


$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $true;NotifyFilter = [IO.NotifyFilters]‘FileName, LastWrite’}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file ‘$name’ was $changeType at $timeStamp" -fore green
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file ‘$name’ was $changeType at $timeStamp"}

Register-ObjectEvent $fsw Deleted -SourceIdentifier FileDeleted -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file ‘$name’ was $changeType at $timeStamp" -fore red
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file ‘$name’ was $changeType at $timeStamp"}

Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "The file ‘$name’ was $changeType at $timeStamp" -fore white
Out-File -FilePath c:\scripts\filechange\outlog.txt -Append -InputObject "The file ‘$name’ was $changeType at $timeStamp"}