Solved

Compare two folders (Total size, Total number of files, Total number of Folders)

Posted on 2012-04-07
6
1,297 Views
Last Modified: 2012-05-23
We are moving user's folder from one location to another....Once we finsih we need to verify the source and target location to confirm the Total size, Total number of files, Total number of Folders are same at both locations....

Fine with VB or Powershell
0
Comment
Question by:principiamanagement
6 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819595
$fso = Get-ChildItem -Recurse -path C:\fso

$fsoBU = Get-ChildItem -Recurse -path C:\fso_BackUp

Compare-Object -ReferenceObject $fso -DifferenceObject $fsoBU

 The output tells me that inputobject (this is the difference object parameter) is missing three files: a.txt, b.txt, and c.txt. I need to copy these three files to the c:\fso_backup folder

http://blogs.technet.com/b/heyscriptingguy/archive/2011/10/08/easily-compare-two-folders-by-using-powershell.aspx
0
 
LVL 3

Expert Comment

by:suresh_boga
ID: 37819729
Dont move - first take backup then copy and then compare so that you will not  loose data
For  copying files from one location to another. Use Robocopy - and enable logging option. IF there are any difference then LOg file give you complete information.
0
 

Author Comment

by:principiamanagement
ID: 37820470
Hi Suresh,

Well we are not moving...We are only copying from one location to other..We use more advance tool than robocopy called EMCcopy ...

Anuroop, I am comparing the folders using below code...

Dim objShell 
Dim objExecObject 
Dim strText  
Dim command  
Dim Directory

Directory = Wscript.Arguments(0)

command = "cmd /c dir " & Directory & " /s"

Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec(command)
 

Do Until objExecObject.StdOut.AtEndOfStream

   strText = objExecObject.StdOut.ReadAll

Loop

aryText = Split(strText, VbNewLine)

Wscript.Stdout.Write Directory
Wscript.Stdout.Write aryText(ubound(aryText) -2 )
Wscript.Stdout.Write aryText(ubound(aryText) -1)

Open in new window


What is getting is same file number and folder numbers but the bytes are different...I am looking way where i can automatcally verify those...
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37820476
little Difference can be due to the allocation unit size of the HDD how they are format.
but will be good if you can verify the same.
0
 
LVL 52

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 37823552
Just as another fast way to do this, you could do a DIR /S command on both folders and then compare the last two lines, that list number and size of files and folders.  Would be pretty easy to do in a small BAT script, let me know if you want help with that approach.

~bp
0
 
LVL 8

Assisted Solution

by:bchallis
bchallis earned 250 total points
ID: 37854490
Here is a Powershell script that uses a pipeline cmdlet to process all the objects passed in to it to provide a running count of the files and folders found, as well as the total file size.

There are then some lines of code that use this cmdlet to process the directories of interest and determine if they were the same.  I too have found that there is some variation in the total size of all the files.

function Get-FolderDetails
{
    Begin
    {
        $details = New-Object PSObject -Property @{            
        FolderCount = 0
        Filecount = 0
        TotalFileSize = 0
    }
    }
    Process
    {
        if ($_.PSIsContainer)
        {
            $details.FolderCount++
        }
        else
        {
            $details.FileCount++
            $details.TotalFileSize += $_.Length
        }
    }
    End
    {
        Write-Output $details
    }
   
   
}

$original = (dir c:\data\development\powershell -Recurse | Get-FolderDetails)
backup = (dir C:\Backup\Powershell -Recurse | Get-FolderDetails)

if ($original.FolderCount -eq $backup.FolderCount -and $original.FileCount -eq $backup.FileCount -and $original.TotalFileSize -eq $backup.TotalFileSize)
{
    Write-Host "Folder structures match"
}
else
{
    Write-Warning "Folder structures don't match"
    $original
    $backup
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
This article will help you understand what HashTables are and how to use them in PowerShell.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now