我只有很少的文件需要通过网络在两台服务器之间移动。 UNC路径将为\ server \ c $ ...,并且在其他服务器上相同。
我正在寻找可以执行此操作的脚本或软件。我知道我可以使用PowerShell或robocopy,但是我想要一些可以监视位置的文件,如果文件显示出来,它将移动它。
一旦检测到文件,我还需要延迟文件移动-如“哦,那里有文件等待5秒 移动文件 '。
做这件事的最好方法是什么?
答案 0 :(得分:2)
首先,请下次尝试自己创建一些代码。即使是一些Get-ChildItem
while循环。
这将监视文件夹位置$watcher.path
中是否有任何新的"Created"
事件,并且在事件发生后大约5秒钟。我不确定我从哪里偷来的,但是很长一段时间都派上用场了。
它将仅监视NEW事件,而不监视先前事件。因此,如果文件夹中包含一些文件,则它将仅根据您的操作对新创建/修改的文件运行该操作。
# Set folder and files to watch and misc flags
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\Source\Location"
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true
# Define actions to be taken when an event is detected
$action = {
$path = $Event.SourceEventArgs.FullPath
$Last = 1
$Current = (Get-Item $path).length
while ($Current -ne $Last) {
$Last = $Current
Start-Sleep -Seconds 1
$Current = (Get-Item $path).length
}
# Change x if you want to increase the time before the move
#sleep x
Move-Item -Path $path -Destination "C:\Destination\Location"
}
# Decide which events to watch
# Changed, Created, Deleted, Renamed events.
Register-ObjectEvent $watcher "Created" -Action $action
while ($true) {sleep 5}