2014-03-07 78 views
0

我正在研究检查特定目录中的文件夹的脚本。例如,我第一次运行脚本,它会生成一个包含目录中文件夹的txt文件。powershell导出到文本文件

我需要脚本来在脚本再次运行时向先前创建的txt文件中添加找到的任何新目录。

有没有人有任何建议如何做到这一点?

这里是我到目前为止的代码:

$LogFolders = Get-ChildItem -Directory mydirectory ; 


If (-Not (Test-Path -path "txtfilelocated")) 

{ 

Add-Content txtfilelocated -Value $LogFolders 
break; 

}else{ 

$File = Get-Content "txtfilelocatedt" 

$File | ForEach-Object { 
$_ -match $LogFolders 
} 

} 
$File 

回答

1

这样的事情?

您可以指定目录,检查添加路径get-childitem cmdlet的在第一线

$a = get-childitem | ? { $_.psiscontainer } | select -expand fullname #for V2.0 and above 
$a = get-childitem -Directory | select -expand fullname #for V3.0 and above 

if (test-path .\list.txt) 
{  
    compare-object (gc list.txt) ($a) -PassThru | Add-Content .\list.txt 
} 
else 
{  
$a | set-content .\list.txt  
} 
+0

太谢谢你了!一切都在工作!谢谢! – user22401

+0

@ user22401很高兴帮助! –