0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【個人メモ】ファイル差分有無を判定するためのPowerShellコマンドを書いてみた

0
Posted at

個人メモ用

#固定値設定
$FoldaName_A = "フォルダA" #元ファイルが格納されたフォルダ名
$FoldaName_B = "フォルダB" #処理後ファイルが格納されたフォルダ名

#実行日時を取得
$date_time = Get-Date -Format "yyyyMMddHHmm"

#出力ファイル名を設定""
$result_filename = $("ResultDiff_" + $date_time + ".csv")

# フォルダAとフォルダBのパスを指定
$parent_directory = "C:\Users\markv\Documents\PowerShell等\親フォルダ"
$child_directorys = dir $parent_directory -Directory

#子フォルダについてそれぞれ処理を実行
foreach ($child_directory in $child_directorys)
{
    $FullPass_folderA = $($parent_directory + "\" + $child_directory.Name + "\" + $FoldaName_A)
    $FullPass_folderB = $($parent_directory + "\" + $child_directory.Name + "\" + $FoldaName_B)
    
    if ($(Test-Path $FullPass_folderA) -and $(Test-Path $FullPass_folderB))
    {
        # フォルダAとフォルダBに含まれる同名のファイルを再帰的に取得
        $filesA = dir $FullPass_folderA -Recurse -File
        $filesB = dir $FullPass_folderB -Recurse -File

        # 差分判定  結果のリストを初期化
        $List_result = @()
        $header_row = "No." + "`t" + "ファイル名(処理前)" + "`t" + "ファイル名(処理後)" + "`t" + "差分有無(True:差分なし、False:差分あり)"
        $List_result += $header_row
        # ファイル数カウント用変数を初期化
        $count_file = 0

        # 総当たりで同名ファイルの差分有無を判定
        foreach ($fileA in $filesA) {
            foreach ($fileB in $filesB) {
                if ($fileB.Name -eq $fileA.Name)
                {
                    $count_file += 1
                    fc.exe $fileA.FullName $fileB.FullName > $null
                    $result = $?    #差分なしの場合:True、差分ありの場合:False
                    $comparisonResult = [string]$count_file + "`t" + $fileA.FullName + "`t" + $fileB.FullName + "`t" + $result
                    $List_result += $comparisonResult
                }
            }
        }
        # 差分判定結果のリストを出力
        $result_filename = $("ResultDiff_" + $date_time + "_" + $child_directory.Name + ".csv")
        New-Item $result_filename > $null
        echo $List_result > $result_filename
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?