如果文件夹中不存在具有相同名称和不同扩展名的其他文件,则用于删除文件的Mac脚本

时间:2013-10-19 17:05:53

标签: macos shell applescript

我文件夹中的文件很少。例如。

1.jpg,
1.nef,
2.nef,
3.jpg,
3.nef

使用脚本,我想删除.nef文件,如果没有相同名称的.jpg文件(在上面的列表中,2.nef)。 我在文件夹中有数千个文件。我无法在shell脚本中开发此逻辑。可以使用AppleScript完成。

谢谢, 阿伦

5 个答案:

答案 0 :(得分:2)

这将使CRGreens脚本更快一点......

set ff to choose folder

tell application "System Events"
    set everyNef to files of ff whose name extension = "nef"
    repeat with thisNef in my everyNef
        set thisJpg to my getBaseName(thisNef) & ".jpg"
        if not (exists file thisJpg of ff) then delete thisNef
    end repeat
end tell

-- This handler is not limited to .jpg
on getBaseName(aFile)
    tell application "System Events" to set {fileName, fileExt} to {name, name extension} of aFile
    return text 1 thru ((get offset of "." & fileExt in fileName) - 1) of fileName
end getBaseName

答案 1 :(得分:2)

你也可以在终端中运行这样的命令:

cd /path/to/directory; for f in *.nef; do [[ -e ${f%nef}jpg ]] || echo rm "$f"; done

删除echo以实际删除文件。

答案 2 :(得分:1)

[编辑包含使用do shell脚本的更快版本] 更快的版本:

set macStyleFF to choose folder
set ff to (POSIX path of macStyleFF)
tell application "Finder"
    --first get all the .nef files (names)
    set everyNef to do shell script "cd '" & ff & "';ls *.nef" --name of every file of ff whose name ends with ".nef"
    set everyNef to (every paragraph of everyNef)

    repeat with thisNef in everyNef
        --use this list to see if there is a jpg of same name
        if file ((text 1 thru -5 of thisNef) & ".jpg") of macStyleFF exists then
            --it has a companion
        else
            --if not, send it to the trash
            delete (file thisNef of macStyleFF)
        end if
    end repeat
end tell

使用Finder'的子句':

的版本不是那么快
set ff to choose folder
tell application "Finder"
    --first get all the .nef files (names)
    set everyNef to name of every file of ff whose name ends with ".nef"
    repeat with thisNef in everyNef
        --use this list to see if there is a jpg of same name
        if file ((text 1 thru -5 of thisNef) & ".jpg") of ff exists then
            --it has a companion
        else
            --if not, send it to the trash
            delete (file thisNef of ff)
        end if
    end repeat
end tell

请注意,这将留下任何孤立的jpgs,但不会留下孤儿.nef文件

答案 3 :(得分:1)

在我看来,在一个大文件夹上使用其claus很慢,并且在重复循环中寻找jpg文件时,它很慢地搜索大文件夹很多次。所以我建议你不要这样做。

我们可以获取文件的名称一次,然后使用该名称列表进行搜索。另外,我们可以将该列表放入脚本对象中,这样可以在处理大型列表时使脚本更快。

此脚本在包含1500个文件的文件夹上运行2秒钟。文件夹中有1000个nefs,其中500个没有jpg文件。

set ff to choose folder
set ffText to ff as text

script s
    property everyFileName : missing value
end script

set inTime to current date
tell application "System Events"
    set s's everyFileName to name of files of ff
end tell

repeat with aName in s's everyFileName
    if (text -3 thru -1 of aName) is "nef" then
        if (text 1 thru -4 of aName & "jpg") is not in s's everyFileName then
            tell application "System Events" to delete file (ffText & aName)
        end if
    end if
end repeat
set s's everyFileName to missing value

set totalTime to (current date) - inTime
return totalTime

答案 4 :(得分:0)

我测试了3个版本,每个版本都有一个大约1700个文件的文件夹。 系统事件版本:61s。

我在第一部分使用do shell的更快版本:31s。

下面的方法使用另一个do shell删除文件:19(或更少 - 它在9s和7s中完成)。 [编辑:请参阅下面的注释和清理代码]

set macStyleFF to choose folder
set ff to (POSIX path of macStyleFF)
set d to current date
tell application "Finder"
    --first get all the .nef files (names)
    set everyNef to do shell script "cd '" & ff & "';ls *.nef" --name of every file of ff whose name ends with ".nef"
    set everyNef to (every paragraph of everyNef)

    repeat with thisNef in everyNef
        --use this list to see if there is a jpg of same name
        if file ((text 1 thru -5 of thisNef) & ".jpg") of macStyleFF exists then
            --it has a companion
        else
            --if not, send it to the trash
            --delete (file thisNef of macStyleFF)
            do shell script "rm '" & ff & thisNef & "'"
        end if
    end repeat
end tell

[清理代码以提高效率(根据adayzone的建议)会生成下面的脚本,时间为5-6秒。]

set macStyleFF to choose folder
set ff to (POSIX path of macStyleFF)

set everyNef to do shell script "cd '" & ff & "';ls *.nef" --name of every file of ff whose name ends with ".nef"
set everyNef to (every paragraph of everyNef)

repeat with thisNef in everyNef
    tell application "Finder" to file ((text 1 thru -5 of thisNef) & ".jpg") of macStyleFF exists
--or ((do shell script "[ -f " & ff & ((text 1 thru -5 of thisNef) & ".jpg") & " ] && echo \"true\" || echo \"false\"") as boolean)
    --use this list to see if there is a jpg of same name
    if (result) then
        --it has a companion
    else
        --if not, send it to the trash
        --delete (file thisNef of macStyleFF)
        do shell script "rm '" & ff & thisNef & "'"
    end if
end repeat