2014-03-07 92 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很高興幫助! –