PowerShell: Compare File Counts of 2 Directories

$folder1='C:\Temp'
$folder2='C:\Temp2'
$username=$null
$password=$null

function compareFileCounts{
  param($folder1,$folder2,$mountAsUser,$mountAsPassword)
  function mountPathAsDrive($path,$driveLetter,$mountAsUser,$mountAsPassword){

    function convertPathToUnc($path,$computername=$env:computername,$credentials=$null){
      $pathIsUnc=$path -match '^\\\\'
      $pathIsReachable=if($credentials){test-path $path -credentials $credentials}else{test-path $path}
      if($pathIsUnc -and $pathIsReachable){
          return $path
      }elseif(!$pathIsUnc){
          $convertedPath=.{
                      $x=$path -replace "^([a-zA-Z])\:","\\$computername\`$1`$";
                      if($x -match '\\$'){return $x.Substring(0,$x.length-1)}else{return $x}
                      }
          $validUnc=if($convertedPath){test-path $convertedPath -ErrorAction SilentlyContinue}else{$false}
          if($validUnc){
              write-host "$path has been converted to $convertedPath, and it is reachable"
              return $convertedPath
          }else{
              write-warning "$path has been converted to $convertedPath, but it is NOT unreachable"
              return $null
          }            
      }else{
          write-warning "Path is invalid"
          return $null
      }
    }

    $pathIsUnc=$path -match '^\\\\'
    $credentials = if($mountAsUser -and $mountAsPassword){
          $password = "$mountAsPassword" | ConvertTo-SecureString -AsPlainText -Force
          New-Object System.Management.Automation.PSCredential ("$mountAsUser", $password )
      }else{
          $null
      }
    $driveLetter=if($driveLetter){
      $driveLetter
    }else{
      $unavailableDriveLetters=(Get-Volume).DriveLetter|sort
      $availableDriveLetters=.{(65..90|%{[char]$_})|?{$_ -notin $unavailableDriveLetters}}
      [char]($availableDriveLetters[0])
    }

    if($pathIsUnc){
      if($credentials){
          $null=net use "$driveLetter`:" "$path" /user:$mountAsUser "'$mountAsPassword'"
      }else{
          $null=net use "$driveLetter`:" "$path"
      }      
    }else{
        $pathAsUnc=convertPathToUnc $path
        if($credentials){
          $null=net use "$driveLetter`:" "$pathAsUnc" /user:$mountAsUser "'$mountAsPassword'"
        }else{
          $null=net use "$driveLetter`:" "$pathAsUnc"
        }
    }

    $reachable=if($credentials){test-path "$driveLetter`:\" -credentials $credentials}else{test-path "$driveLetter`:\"}
    return $(if($reachable){"$driveLetter`:\"}else{$false})
  }

  $unavailableDriveLetters=(Get-Volume).DriveLetter|sort
  $availableDriveLetters=.{(65..90|%{[char]$_})|?{$_ -notin $unavailableDriveLetters}}
  $firstDriveLetter=[char]($availableDriveLetters[0])
  $secondDriveLetter=[char]($availableDriveLetters[1])

  $path1=mountPathAsDrive $folder1 $firstDriveLetter $mountAsUser $mountAsPassword
  $path2=mountPathAsDrive $folder2 $secondDriveLetter $mountAsUser $mountAsPassword

  if($path1 -and $path2){
    $timer1=[System.Diagnostics.Stopwatch]::StartNew()
    $filesInFolder1 = Invoke-Expression -Command:"cmd.exe /C dir '$path1' /S /B /W /A:-D" -EA Ignore
    $filesCount1=$filesInFolder1.Count
    $elapsed1=[math]::round($timer1.Elapsed.TotalMinutes,2)
    $timer1.Stop()
    write-host "$folder1 (mounted as $path1) file count: $filesCount1 ($elapsed1 minutes)"
    $timer2=[System.Diagnostics.Stopwatch]::StartNew()
    $filesInFolder2 = Invoke-Expression -Command:"cmd.exe /C dir '$path2' /S /B /W /A:-D" -EA Ignore
    $filesCount2=$filesInFolder2.Count
    $elapsed2=[math]::round($timer2.Elapsed.TotalMinutes,2)
    $timer2.Stop()
    write-host "$folder2 (mounted as $path2) file count: $filesCount2 ($elapsed2 minutes)"
  }elseif(!$path1 -and !$path2){
    write-warning "Both $path1 and $path2 are NOT accessible."
  }elseif(!$path1){
    write-warning "$path1 is NOT accessible."
  }elseif(!$path2){
    write-warning "$path2 is NOT accessible."
  }
  $null=net use "$($path1[0]):" /delete
  $null=net use "$($path2[0]):" /delete
  write-host "Total duration: $($elapsed1+$elapsed2) minutes"
  return $($filesCount1 -eq $filesCount2)
}

compareFileCounts $folder1 $folder2 $username $password
# old version

$folder1='\\FILESERVER1\TEST'
$folder2='\\BACKUP1\TEST'

function compareFileCounts($folder1,$folder2){
    $filesInFolder1 = Invoke-Expression -Command:"cmd.exe /C dir '$folder1' /S /B /W /A:-D" -EA Ignore
    $fileCount1=$filesInFolder1.Count
    write-host "$folder1 file count: $fileCount1"
    $filesInFolder2 = Invoke-Expression -Command:"cmd.exe /C dir '$folder2' /S /B /W /A:-D" -EA Ignore
    $fileCount2=$filesInFolder2.Count
    write-host "$folder2 file count: $fileCount2"
    return $($fileCount1 -eq $fileCount2)    
}

compareFileCounts $folder1 $folder2

Leave a Reply

Your email address will not be published. Required fields are marked *