1

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

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

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

谢谢,阿伦

4

5 回答 5

3

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

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

删除echo以实际删除文件。

于 2013-10-20T00:08:44.223 回答
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
于 2013-10-19T18:15:40.457 回答
1

在我看来,在大文件夹上使用 who 子句很慢,并且在重复循环中多次搜索大文件夹以查找 jpg 文件很慢。所以我建议你也不做。

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

该脚本在 2 秒内在一个包含 1500 个文件的文件夹上运行。文件夹中有 1000 个 nef,其中 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
于 2013-10-19T23:56:11.983 回答
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 'whose clause' 的快速版本:

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

请注意,这将在其中留下任何孤立的 jpg,但不会留下孤立的 .nef 文件

于 2013-10-19T17:38:45.113 回答
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
于 2013-10-19T22:21:30.267 回答