首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找不同文件夹中具有不同文件名的重复文件,同步其文件名

查找不同文件夹中具有不同文件名的重复文件,同步其文件名
EN

Stack Overflow用户
提问于 2021-06-14 08:12:29
回答 2查看 64关注 0票数 0

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

代码语言:javascript
运行
复制
FOLDER A:   Filename01. Filename02. Filename03. ...

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

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

代码语言:javascript
运行
复制
FOLDER A:   Filename01. Filename02. Filename03. ...

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

现在我想同步这两个文件夹。将文件夹A中的Filename02更改为Filename02new。如果在这两个文件夹中有许多相同的文件具有不同的文件名,有没有一种快速的方法,自动的方法,或者任何软件都可以完成这项工作-在不同的文件夹中找到具有不同文件名的副本,然后同步它们的文件名?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-16 20:01:03

代码语言:javascript
运行
复制
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
票数 0
EN

Stack Overflow用户

发布于 2021-06-17 22:45:37

我不能编辑Robert的代码,所以我在这里发布了编辑后的代码:

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67963552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档