powershelldirectorycopyscheduled-taskswindows-scripting

Copy new files from one folder to another folder automatically


I have a problem and don't know how to solve it myself. I haven't found anything on google either.

I have a folder called Trace, in this folder several devices save their trace files. When the devices are in operation, new trace files are added. The trace files are all saved as "xxxxxx.trace". I have written a Powershell script that automatically turns all .trace files into txt files and then merges these txt files into one single txt file. The txt files are deleted after merging so that they are not copied several times into the merged txt file.

This is my Powershell script

while (1)
{
Get-ChildItem -Path "C:\xxx\xxx\xxx\Trace2"*.trace | Rename-Item -NewName { $_.name -Replace '\.trace$','.txt' }
C:\xxx\xxx\xxx\Powershell\Combine-Files.ps1 -output "C:\xxx\xxx\xxx\Test\test.txt" -source "C:\xxx\xxx\xxx\Trace2\" -filter "*.txt" -append 
Get-ChildItem C:\xxx\xxx\xxx\Trace2\*.txt -File | Remove-Item -Force
Start-Sleep -Seconds 10
}

The script works, but the problem is that I am not allowed to delete the trace files from the original trace folder. For this reason I need a second folder. From now on I will call the original trace folder "folder1" and the second folder I will call "folder2".

These are the conditions that folder2 must fulfill:

-All files that are already on folder1, or will be added to it, must be copied to folder2.

-Each file is only allowed to be copied from folder1 to folder2 one time. So if a file is deleted in folder2, it cannot be copied from folder1 to folder2 again.

If this is not possible, a script or program that copies every newly added file in folder1 to folder2 would be another option. I would then manually copy the files that are already in folder1.

I have found this script and it almost does what i need, but because the files are deleted almost immediately from folder2, it is not possible to compare the files from folder1 and folder2 and see if the file has been copied before.

I have already tried robocopy /mir. However, robocopy /mir copies all files that were deleted from folder2 back into folder2, because robocopy /mir creates an exact image of folder1.

Edit 1: This is the code from Sirtao's answer, where I have adjusted the paths.

    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\dabetschart\Documents\Trace2"
    $watcher.Filter = "*.txt"
    $watcher.EnableRaisingEvents = $true  
    $DestinationPath = "C:\Users\dabetschart\Documents\Test"

    $action = { 
                $FileCreated = $Event.SourceEventArgs.FullPath
                Start-Sleep -Seconda 1
                Copy-Item -Path $FileCreated -Destination $DestinationPath
              }    
    Register-ObjectEvent $watcher "Created" -Action $action
    while ($true) {sleep 5}

The newly created files in Trace2 are not copied to Test. There are no error messages. I have the filter set to *.txt because I want to test the code with txt files first.

Edit 2:

I have found the problem. It worked with this code:

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\dabetschart\Documents\Trace2"
    $watcher.Filter = "*.*"
    $watcher.EnableRaisingEvents = $true  
    $DestinationPath = "C:\Users\dabetschart\Documents\Test"

    
### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { 
                $Path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                Copy-Item -Path $Path -Destination $DestinationPath
              }    
### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
### Register-ObjectEvent $watcher "Changed" -Action $action
### Register-ObjectEvent $watcher "Deleted" -Action $action
### Register-ObjectEvent $watcher "Renamed" -Action $action
    while ($true) {sleep 1}

@sirtao Thank you very much, you have helped me a lot.


Solution

  • Implementing this solution

    ### SET FOLDER TO WATCH + FILES TO WATCH
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = '"C:\xxx\xxx\xxx\Trace2'
    $watcher.Filter = '*.trace'
    $watcher.EnableRaisingEvents = $true  
    
    ### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { 
        # get the full path of the file that has been created
        $FileCreated = $Event.SourceEventArgs.FullPath
    
        # wait one second after the creation of the file to make sure it's completely written. It shouldn't be an issue most of the time so test if you can remove it.
        Start-Sleep -Seconda 1
    
        # No need for other files you can get the content from the source
        Get-Content -Path $FileCreated | Add-Content 'C:\xxx\xxx\xxx\Test\test.txt' 
    }    
    
    ### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher 'Created' -Action $action
    # other events can have the same or different actions
    # Register-ObjectEvent $watcher "Changed" -Action $action
    # Register-ObjectEvent $watcher "Deleted" -Action $action
    # Register-ObjectEvent $watcher "Renamed" -Action $action