Saturday, April 21, 2012

Comparing two folders.

I recently was backing up my data and the backup device had lots of space so i turned up copying folders from multiple directories to the disc. But now was at loss when it came to deleting data from my hard drive. So wrote this little script. Thanks to Ying lin for showing the contains operator in powershell.

http://www.myitforum.com/articles/1/view.asp?id=10092

#usage = .\Compare-Items-in-two-directories.ps1 -path1 "e:\to write\dataondisk" -path2 "f:\dataoncd"

param([String] $path1,$path2)

$Diskdir = @()

$CDdir = @()

Get-childitem -Path$path1-recurse | %{$Diskdir=$Diskdir+$_.name}

$Diskdir.count Get-childitem -Path$path2-recurse | %{$CDdir=$CDdir+$_.name} $CDdir.count

$arraymatch= @()

$arraynotmatch= @()

foreach ($Itemin$Diskdir)

{

if($CDDir-contains$Item)

{

$arraymatch=$array+$item

}

else      {

$arraynotmatch=$arraynotmatch+$item

}

}

$arraynotmatch

 

No comments:

Post a Comment