2017-02-22 63 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 }