Hi All!
I've been working on this script on and off, and I'm stuck!
I have two folders: Dev and QAReady. Each of these folders has 3 txt files in the root, along with 7 sub folders that cant be deleted. You could say they are a mirror of eachother.
I need to compare both Dev and QAReady, find out if they are equal, if so remove any dupe folders or files in Dev and log it. Do nothing for the QAReady folder.
I'm not sure if I should dir -recurse the folders and export a csv or xml, and then compare either the xml or csv file. I've also been thinking about getting the hash of every folder and file and then comparing and going from there. Lots of options, just not sure which is going to be the best path.
I found some code by 'nohandle' which is helpful, I just dont know how to procede with it...
$folderDifference="C:\Builds\QAReady\"
$FolderReferenceContents=Get-ChildItem$folderReference-Recurse | where-object {-not$_.PSIsContainer}
$FolderDifferenceContents=Get-ChildItem$folderDifference-Recurse | where-object {-not$_.PSIsContainer}
#Gets all files in QAReady and prints whats missing in Dev.
foreach ($iin$folderDifference) {
Compare-Object-ReferenceObject$FolderReferenceContents `
-DifferenceObject$FolderDifferenceContents-Property ('Name', 'Length') -PassThru |
where-object { $_.SideIndicator-eq'<='} | select FullName
}
Here's my origional code that I've been trying to work with.... It's not 100% working... Plus the logging sucks and it supposly deleted folders that were no in QAReady..
$Dev="C:\Builds\Dev"
$original= @(Get-Childitem-Path$QAReady | Where-Object {$_.PSIsContainer} | Sort-Object-Property Name)
$copy= @(Get-Childitem-Path$Dev | Where-Object {$_.PSIsContainer} | Sort-Object-Property Name)
$difference= @(Compare-Object–ReferenceObject (Get-ChildItem$QAReady) –DifferenceObject (Get-ChildItem$Dev) –Passthru)
#Creates log
$logsDir="C:\scripts\logs"
$tracefile="$logsDir\SearchAndDestroy-$(get-date -format 'MM-dd-yyyy').txt"
$processedFolders=""
Start-Transcript-path$tracefile
#Gets system name so it prints to log file.
[System.Net.Dns]::GetHostByName(($env:computername))
#Compare if dirs are equal or not.
if ($difference.Count-eq 0)
{
Write-Host""
Write-Host"Folders Are Equal, Removing Dupes from $dev "-ForegroundColor"green"
Write-Host""
#If dir's are equal, purge dupe folders from $Dev, don't touch $QAReady
foreach ($iin$Dev)
{
Get-ChildItem$Dev-Recurse | Where-Object {$_.PSIsContainer} | ForEach-Object {
[string[]]$processedFolders+=$_.FullName
Remove-Item$($_.FullName+"\*") -Force-Recurse-Verbose-Whatif
}
Write-Host""
Write-Host"Processed Folders:"-ForegroundColor"DarkRed"
#Print out the folders it processed.
$processedFolders
#Send $processedFolders to log file.
$processedFolders | Out-File-FilePath"$logsDir\LogFile-$(get-date -format 'MM-dd-yyyy').txt"
} #End Foreach
}
else
{
Write-Host""
Write-Host"Content is different, Differences:"-ForegroundColor"Red"
Write-Host"$difference"
}
Thanks for your assistance!