LoginSignup
1
2

More than 5 years have passed since last update.

フォルダの差分を比較するためのVBScript

Last updated at Posted at 2018-10-22

フォルダの差分を比較するvbScriptのクラスを作成しました。
Winマージをプログラムで書いたような感じです。
使い方:第一引数と第二引数にフォルダパスを入れて実行


Dim DiffObj
Set DiffObj = new Diff
If DiffObj.IsDiff("./test","./test2") Then
    MsgBox("差分有")

Else
    MsgBox("差分無")

End If

Class Diff
    Public Function IsDiff(path1,path2) 

        If IsDiffFolder(path1,path2)=true then
            IsDiff=true
            Exit Function
        End If 

        If IsDiffFolder(path2,path1) =true then
            IsDiff=true
            Exit Function
        End If 
        IsDiff=false
    End Function

    '差分があればtrueを返す
    Private Function IsDiffFolder(path1,path2)
        Set fso   = CreateObject("Scripting.FileSystemObject")

        Set fsoFolder = fso.GetFolder(path1)
        For Each fsoFile In fsoFolder.Files
            If IsDiffFile(path1 & "\" & fsoFile.Name,path2 & "\" & fsoFile.Name) =true then
                '差分がある場合どのファイルで差分があるかをログファイルに書き出す例
                'LogWriterObj.write( path1 & "\" & fsoFile.Name & "で差分あり")
                IsDiffFolder=true
                Exit Function
            End If
        Next

        For each subfolder in fsoFolder.subfolders          
            If IsDiff(path1 & "\" & subfolder.name,path2 & "\" & subfolder.name)=true then
                IsDiffFolder=true
                Exit Function
            End If
        Next
    End Function

    '差分があればtrueを返す
    Private Function IsDiffFile(file1Path,file2Path)

        Set objFSO = CreateObject("Scripting.FileSystemObject") 

        Set objFile1 = objFSO.OpenTextFile(file1Path, 1) 
        text1 = objFile1.ReadAll 

        Set objFile1 = objFSO.OpenTextFile(file2Path, 1) 
        text2 = objFile1.ReadAll 


        objFile1.Close 
        If InStr(text1,text2) = 0 OR InStr(text2,text1) = 0 Then 
            IsDiffFile=true
        Else
            IsDiffFile=false
        End If 
    End Function
End Class


'''

またPrivateメソッドですが、IsDiffFileはファイルの差分を比較するメソッドです。

1
2
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
1
2