在Powershell中捕获和复制创建的文件

时间:2018-09-25 19:02:51

标签: powershell

我正在使用此脚本扫描文件夹中的已创建文件。不幸的是,这是临时文件,并由我们使用的程序创建和删除。

Code:
$folder = 'C:\Users\JuanMa\Desktop\RestBar\RestBar'
$filter = '*.*'                             # <-- set this according to your requirements
$destination = 'C:\Users\JuanMa\Desktop\UNB'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
 IncludeSubdirectories = $true              # <-- set this according to your requirements
 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 "The file '$name' at '$path'was $changeType at $timeStamp"
 Copy-Item $path -Destination $destination -Force -Verbose # Force will overwrite files with same name
}

脚本无法将文件复制到目标文件夹。林假设文件被删除之前可以复制。是否有一种锁定文件的方法,以便它可以在删除操作后被删除?

谢谢!

0 个答案:

没有答案