I have a folder called C:\2014-15 and new sub folders are created every month which contain csv files i.e
- C:\2014-15\Month 1\LTC
- C:\2014-15\Month 2\LTC
- C:\2014-15\Month 3\LTC
How to write a script which would detect when a LTC sub folder is created for every month and move the csv files to N:\Test?
UPDATED:
$folder = 'C:\2014-15'
$filter = '*.*'
$destination = 'N:Test\'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host
Copy-Item -Path $path -Destination $destination
}
The error I get is:
Register-ObjectEvent : Cannot subscribe to event. A subscriber with source identifier 'FileCreated' already exists. At line:8 char:34 + $onCreated = Register-ObjectEvent <<<< $fsw Created -SourceIdentifier FileCreated -Action { + CategoryInfo : InvalidArgument: (System.IO.FileSystemWatcher:FileSystemWatcher) [Register-ObjectEvent], ArgumentException + FullyQualifiedErrorId : SUBSCRIBER_EXISTS,Microsoft.PowerShell.Commands.RegisterObjectEventCommand
Register-ObjectEvent. Would it be easier if your script was run monthly, and creates the /Month x/LTC directories as well as moving the log files? - xXhRQ8sD2L7Z