同じ構造のフォルダを比較するPowershellスクリプトを書いてみました。
 フォルダ内のサブフォルダ、ファイルのパス、更新日時、サイズを基準にして比較しています。
 ソースコードは以下の通りです。
    | #==============================================#処理:同じ構造のフォルダを比較する
 #実行条件:2つのフォルダ名が同じであること
 #==============================================
 $dir1 = "u:\test"
 $dir2 = "c:\temp\test"
 if(!(Test-Path $dir1) -or !(Test-Path $dir2)){Write-Output "フォルダが存在しません。"
 Exit 1
 }
 $dir1name = (Get-Item $dir1).Name$parent1 = (Get-Item $dir1).Parent.Name
 $dir2name = (Get-Item $dir2).Name
 $parent2 = (Get-Item $dir2).Parent.Name
 if($dir1name -ne $dir2name){
 Write-Output "フォルダ名が異なるため、比較できません。"
 Exit 1
 }
 #連想配列$content = @{}
 $dir1only = @()
 $dir2only = @()
 $dir1con = Get-ChildItem $dir1 -Recurse | % {$nametime = $_.fullname + " " + $_.lastwritetime.ToString() + " " + $_.Length; Write-Output $nametime}$dir2con = Get-ChildItem $dir2 -Recurse | % {$nametime = $_.fullname + " " + $_.lastwritetime.ToString() + " " + $_.Length; Write-Output $nametime}
 foreach($con in $dir1con){$item = $con.Substring($parent1.Length)
 $content[$item] = -1
 }
 foreach($con in $dir2con){$item = $con.Substring($parent2.Length)
 if(++$content[$item] -eq 1){
 $dir2only += $con
 }
 }
 foreach($con in $dir1con){$item = $con.Substring($parent1.Length)
 if($content[$item] -eq -1){
 $dir1only += $con
 }
 }
 Write-Output "差分を出力します。"Write-Output "-----------$dir1-----------"
 Write-Output $dir1only
 Write-Output "-----------$dir2-----------"
 Write-Output $dir2only
 | 
  
 ソースコードをコピーし、$dir1、$dir2に該当の値を変更して.ps1に保存して実行する。
 機能が十分ではないため、複雑な比較はできません。
この記事がお役にたちましたらシェアをお願いします:)
 
0 件のコメント:
コメントを投稿