0

I'm working on a script that checks folders in specific directory. For example, I run the script for first time, it generates me a txt file containing folders in the directory.

I need the script to add any new directories that are found to the previously created txt file when the script is run again.

Does anyone have any suggestions how to make that happen?

Here is my code so far:

$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 Answer 1

1

something like this?

You can specify what directory to check adding path to get-childitem cmdlet in first line

$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    
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! Everything is working! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.