Untitled

 avatar
unknown
plain_text
2 months ago
1.1 kB
3
Indexable
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Users\[YourUsername]\AppData\Local\Temp"
$watcher.Filter = "*.tmp"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true

$destination = "C:\SafeFolder" # Change to your desired safe folder path
if (!(Test-Path $destination)) {
    New-Item -ItemType Directory -Path $destination
}

$startTime = Get-Date
$timeout = 60 # Set duration in seconds

Write-Host "Monitoring Temp folder for .tmp files... Press Ctrl+C to stop."

Register-ObjectEvent $watcher Created -Action {
    $filePath = $Event.SourceEventArgs.FullPath
    $fileName = $Event.SourceEventArgs.Name
    $destinationPath = Join-Path -Path $destination -ChildPath $fileName

    try {
        Copy-Item -Path $filePath -Destination $destinationPath -ErrorAction Stop
        Write-Host "Copied: $fileName to $destinationPath"
    } catch {
        Write-Host "Failed to copy $fileName: $_"
    }
}

# Run the monitoring loop for the specified duration
while ((Get-Date) -lt $startTime.AddSeconds($timeout)) {
    Start-Sleep -Seconds 1
}

Write-Host "Monitoring finished."
Leave a Comment