PowerShell进程仅在第一场比赛时进行

时间:2014-07-02 18:11:13

标签: powershell

Is there a way in powershell to match file against files in a directory and on the     
first match write the file in the directory to another directory
file            directory      output directory 
test1.txt       test3.txt          test1.txt since this matched and it was the first   
                                               match. only write this one process ends.
test2.txt       test2.txt                     
test3.txt       test1.txt

据我所知,我知道这里需要一个第一个参数吗?

$objFolder = "C:\Users\Bruce\Serena\"  (directory)
$objFile = "C:\Users\Bruce\process.txt"  (File)
$outFile = "C:\Users\Bruce\Deploy\"  Output Directory
$FolderReferenceContents = Get-ChildItem $objFolder 
$FolderDifferenceContents = Get-Content $objFile 
 $Compare-Object -ReferenceObject $FolderReferenceContents -DifferenceObject 

1 个答案:

答案 0 :(得分:0)

您想要管道到Select-Object cmdlet。

Compare-Object -ReferenceObject $FolderReferenceContents -DifferenceObject $FolderDifferenceContents | Select -First 1

好的,我刚试过这个,它确实有效。如果您遇到错误,则需要检查路径。

$objFolder = "C:\Users\Bruce\Serena\"  #(directory)
$objFile = "C:\Users\Bruce\process.txt"  #(File)
$outFile = "C:\Users\Bruce\Deploy\"  #Output Directory
Compare-Object -ReferenceObject (gc $objFile) -DifferenceObject (gci $objFolder|select -ExpandProperty Name) -ExcludeDifferent -IncludeEqual|select -first 1 -Expand InputObject | %{Copy-Item "objFolder\$_" -Dest $outFile}