OSX:檢查子資料夾,將目錄結構展平到新資料夾中,然後移動到桌面

OSX:檢查子資料夾,將目錄結構展平到新資料夾中,然後移動到桌面

我正在清理一個巨大的漫畫庫(主要是但不僅僅是圖像檔案)。每部漫畫都有一個包含子資料夾的主資料夾,每卷一個子資料夾。

但有時,卷宗資料夾僅包含圖像文件,有時它們包含子資料夾。

我想要一個針對所選漫畫資料夾的腳本:

  • 檢查哪些卷宗資料夾有子資料夾
  • 使用子資料夾展平卷資料夾的目錄結構。我不想將所有圖像放入父資料夾並刪除子資料夾,而是希望它在桌面上建立一個新資料夾,並將所有子資料夾的映像複製到其中。
  • 將桌面上建立的資料夾重新命名為其原始父資料夾

前 :

漫畫資料夾

volume1_folder

    subfolder1 / image1, image2, image3
    subfolder2 / image4, image5, image6
    subfolder3 / image7, image8, image9

volume2_folder

    image1, image2, image3, image4, image5, image6, image7, image8, image9

volume3_folder

    subfolder1 / image1, image2, image3
    subfolder2 / image4, image5, image6
    subfolder3 / image7, image8, image9

後:

桌面:

volume1_folder

    image1, image2, image3, image4, image5, image6, image7, image8, image9

volume3_folder

    image1, image2, image3, image4, image5, image6, image7, image8, image9

原始資料夾應保持不變這樣我就可以檢查是否有放錯位置的文件等。

現在,我使用 AppleScript 來扁平化我選擇的每個磁碟區資料夾的目錄結構。它會在其中建立一個新資料夾,以其父資料夾命名,其中包含所有複製的映像。

它不做什麼:

  • 檢查哪些卷宗資料夾有子資料夾(我必須單獨選擇每個卷宗資料夾並每次啟動腳本)
  • 將建立的具有扁平化目錄結構的資料夾移至桌面

我確實嘗試自己實施更改,但我可以將新資料夾移至桌面或以其父資料夾後重命名。當我放置兩個指令(無論先放置哪條指令)時,我會收到一條錯誤訊息,指出執行第一條指令後找不到該資料夾。這是腳本:

tell application "Finder"
    set theTopFolder to (selection as alias)
    set theFiles to a reference to every file of (entire contents of folder theTopFolder)
    set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
    duplicate theFiles to theNewFolder
    set name of theNewFolder to name of theTopFolder
    
    
    --move theNewFolder to "Macintosh HD:Users:xx:Desktop:"
end tell

答案1

tell application "Finder"
    repeat with f in (get folders of (POSIX file "/Users/username/manga_folder" as alias))
        make new folder at desktop with properties {name:(get name of f)}
        duplicate files of entire contents of f to result
    end repeat
end tell

您也可以使用以下 shell 命令:

cd ~/manga_folder;for d in *;do mkdir ~/Desktop/"$d";find "$d" -type f|while read f;do cp "$f" ~/Desktop/"$d";done;done

或者如果你有 GNU cp:

cd ~/manga_folder;for d in *;do mkdir ~/Desktop/"$d";find "$d" -type f -exec gcp -t ~/Desktop/"$d" {} +;done

相關內容