# Bitvise SSH Server Usage FAQ, Q287 # # https://bitvise.com/ssh-server-usage-faq#Q287 # # Example PowerShell script to move files from a staging directory to a network share, # with check to avoid moving files that are currently being copied to staging. # # If files may be moved to the staging directory from a different local volume, # such a move involves copying the file. This type of move is not atomic. In this case, # the script must avoid moving files from staging which are not yet done moving to staging. $staging = "C:\Staging" $dest = "\\server\share\drop" Get-ChildItem -LiteralPath $staging -File | ForEach-Object { $path = $_.FullName try { # Can we open an exclusive handle? $s = [System.IO.File]::Open( $path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None ) $s.Dispose() # If test succeeded, we can assume the file is done being copied Move-Item -LiteralPath $path -Destination $dest -ErrorAction Stop } catch [System.IO.IOException] { # File in use / sharing violation / transient I/O: skip the file in this cycle } }