在不同文件夹中查找具有不同文件名的重复文件,同步它们的文件名

时间:2021-06-14 00:12:28

标签: python shell applescript

我在文件夹 A 中有一些重要文件,然后我将它们备份到其他驱动器上的文件夹 B 中。

FOLDER A:   Filename01. Filename02. Filename03. ...

FOLDER B:   Filename01. Filename02. Filename03. ...

然后我将文件夹 B 中的 Filename02 重命名为 Filename02new。

FOLDER A:   Filename01. Filename02. Filename03. ...

FOLDER B:   Filename01. Filename02new. Filename03. ...

现在我想同步这两个文件夹。将文件夹 A 中的 Filename02 更改为 Filename02new。 如果这两个文件夹中有很多相同文件名不同的文件,是否有快速、自动的方法或任何软件可以完成这项工作 - 在不同文件夹中找到不同文件名的重复项,然后同步它们的文件名?

2 个答案:

答案 0 :(得分:0)

set folderA to choose folder -- alias
set folderB to choose folder -- alias

tell application "Finder"
    repeat with i from 1 to count (items of folderA)
        set aItem to item i of folderA
        set aSize to size of aItem
        set posix_Path_1 to quoted form of (POSIX path of (aItem as text))
        repeat with j from 1 to count (items of folderB)
            set bItem to item j of folderB
            set bSize to size of bItem
            
            if aSize = bSize then -- if size is  same, than items can be duplicates
                set posix_Path_2 to quoted form of (POSIX path of (bItem as text))
                -- compare items further
                set shell_Command_String to "cmp -s" & posix_Path_1 & space & posix_Path_2
                set exitStatus to 0 -- setting initial status of shell operation
                try
                    set exitStatus to do shell script shell_Command_String
                    return exitStatus
                end try
                if exitStatus = 0 then -- if exitStatus remains= 0, that indicates "files is same"
                    set name of aItem to name of bItem
                    exit repeat
                end if
            end if
            
        end repeat
    end repeat
end tell

答案 1 :(得分:0)

我无法编辑 Robert 的代码,所以我在这里发布了编辑过的代码:

set folderA to choose folder 
set folderB to choose folder 

tell application "Finder"
    set numberA to count files of entire contents of folderA
    set numberB to count files of entire contents of folderB
    
    repeat with i from 1 to numberA
        set aItem to item i of folderA
        set aSize to size of aItem
        set posix_Path_1 to quoted form of (POSIX path of (aItem as text))
        repeat with j from 1 to numberB
            set bItem to item j of folderB
            set bSize to size of bItem
            
            if aSize = bSize then
                set posix_Path_2 to quoted form of (POSIX path of (bItem as text))
                set shell_Command_String to "cmp " & posix_Path_1 & space & posix_Path_2
                set exitStatus to 0
                try
                    set exitStatus to do shell script shell_Command_String
                end try
                if exitStatus ≠ 0 then
                    set name of aItem to name of bItem
                    exit repeat
                end if
            end if
            
        end repeat
    end repeat
end tell
相关问题