VBS文件夹比较删除同名同路径文件

'遍历一个文件夹下的所有文件夹
Function FilesTreeClean(NeedCleanPath, ComparePath)
    WScript.Echo "清理目录: " & NeedCleanPath
    WScript.Echo "保留目录: " & ComparePath
    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFso.GetFolder(NeedCleanPath)
    Set oSubFolders = oFolder.SubFolders
    
    Set oFiles = oFolder.Files
    For Each oFile In oFiles
        'WScript.Echo oFile.Path
        curFileName = oFso.GetFileName(oFile.Path)
        If(oFso.FileExists(ComparePath & "\" & curFileName)) Then '检测文件是否存在
            WScript.Echo "删除重复文件 " & oFile.Path
            oFile.Delete
        End If
    Next
    
    For Each oSubFolder In oSubFolders
        subFolderName = "\" & oFso.GetFileName(oSubFolder.Path)
        WScript.Echo "=>" & subFolderName
        'oSubFolder.Delete
        call FilesTreeClean(oSubFolder.Path, ComparePath & subFolderName)'递归
    Next
    
    Set oFolder = Nothing
    Set oSubFolders = Nothing
    Set oFso = Nothing
End Function

Set args = WScript.Arguments
If args.Count = 2 Then
    NeedCleanPath = WScript.Arguments(0)
    ComparePath = WScript.Arguments(1)
    call FilesTreeClean(NeedCleanPath, ComparePath)
Else
    MsgBox "用法: CSCRIPT DuplicateCleaner.vbs 待清理的文件夹路径 待保留的文件夹路径", 0, "温馨提示"
End If

你可能感兴趣的:(VBS文件夹比较删除同名同路径文件)