1

フォルダにいくつかのファイルがあります。たとえば。

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

スクリプトを使用して、同じ名前の .jpg ファイルが存在しない場合 (上記のリストの 2.nef)、.nef ファイルを削除したいと考えています。フォルダに何千ものファイルがあります。このロジックをシェル スクリプトで開発することができません。これは 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

[do シェル スクリプトを使用してより高速なバージョンを含めるように編集] より高速なバージョン:

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句」を使用した高速バージョンではありません:

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 に答える
1

大きなフォルダーで who 句を使用すると時間がかかり、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
于 2013-10-19T23:56:11.983 に答える
0

それぞれ約 1700 個のファイルのフォルダーを持つ 3 つのバージョンをテストしました。システム イベントのバージョン: 61s。

最初の部分で do shell を使用した高速バージョン: 31s.

別の do シェルを使用してファイルを削除する以下の方法: 19 (またはそれ以下 -- 9 秒と 7 秒で実行しました)。[編集: 以下のメモとクリーンアップされたコードを参照]

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 に答える