2017-02-22 67 views
0

使用powershell運行一個命令將文件從源文件複製到目標文件時,我遇到了如何複製沒有任何操作的文件的問題?複製完成的文件?

如果有3個文件A/B/C,日誌仍然在A上發生,而B和C完成。我只想使用PowerShell複製B和C.

任何想法都會有所幫助。

+0

這應該讓您識別的文件,如果正在使用由另一個進程。 http://stackoverflow.com/a/9411199/5212566 –

回答

0

下面是一些你可以開始:

function Test-FileNotInUse 
{ 
    [cmdletbinding()] 
    param(
     [parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('FullName')] [string] $filePath 
    ) 

    process { 

     $inUse = $false 

     try { 
      $fileInfo = New-Object System.IO.FileInfo $filePath 
      $fileStream = $fileInfo.Open([System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::ReadWrite, [System.IO.FileShare]::None) 

      if ($fileStream) { 
       $fileStream.Close() 
      } 
     } 

     catch { 
      $inUse = $true 
     } 

     if (!$inUse) { 
      Write-Output $filePath 
     } 
    } 
} 


$source = "C:\source" 
$destination = "C:\destination" 

dir $source -recurse | Test-FileNotInUse | %{ copy $_ $destination }