使用新值附加文本文件,不重复

时间:2015-07-03 04:17:33

标签: powershell

我尝试将现有文件添加到仅添加新值但值始终重复

$arrList=New-object System.Collections.ArrayList       
$line="201506291500-1600-1.gz","201506291500-1600-2.gz","201506291500-1600-    3.gz","201506291500-1600-9.gz"
$file="c:\users\admin\desktop\ftp\list.txt"      

$a=get-content $file

$arrList.add($a)

$line|%{if(!$arrList.contains($_)){$arrList.Add($_)}}


$arrList|out-file $file -append

list.txt包含 201506291500-1600-9.gz 201506291500-1600-10.gz 201506291500-1600-11.gz

但我得到的list.txt的输出是重复值

201506291500-1600-9.gz

201506291500-1600-10.gz

201506291500-1600-11.gz

201506291500-1600-9.gz

201506291500-1600-10.gz

201506291500-1600-11.gz

201506291500-1600-1.gz

201506291500-1600-2.gz

201506291500-1600-3.gz

201506291500-1600-9.gz

我的期望是:

201506291500-1600-1.gz

201506291500-1600-2.gz

201506291500-1600-3.gz

201506291500-1600-9.gz

201506291500-1600-10.gz

201506291500-1600-11.gz

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

也许这会有所帮助:

$line="201506291500-1600-1.gz","201506291500-1600-2.gz","201506291500-1600-7.gz","201506291500-1600-9.gz"
$list = Get-Content -Path C:\temp\list.txt 
$line  | Where-Object {$_ -notin $list} | Out-File -FilePath C:\temp\list.txt -Append -Encoding ascii