powershell导出到文本文件

时间:2014-03-07 10:39:22

标签: windows powershell

我正在编写一个检查特定目录中文件夹的脚本。例如,我第一次运行脚本,它生成一个包含目录中文件夹的txt文件。

我需要脚本在脚本再次运行时添加找到先前创建的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 个答案:

答案 0 :(得分: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    
}