【问题标题】:Create an Applescript or Shell Script to Unzip and Rename Files?创建 Applescript 或 Shell 脚本来解压缩和重命名文件?
【发布时间】:2012-11-19 01:07:06
【问题描述】:

我有几个 .zip 文件文件夹,我想执行以下操作:

  • 将随附的文件或文件夹解压缩到与 .zip 相同的位置
  • 将生成的文件或文件夹重命名为 .zip 文件的名称
  • 删除 .zip 文件

.zip 文件始终包含单个文件或单个文件夹,因此无需担心重命名错误文件夹。

我只对 Applescript 有一点经验,对 shell 脚本几乎没有。任何人都可以提供帮助或提出任何建议吗?

谢谢!

【问题讨论】:

    标签: macos shell applescript rename unzip


    【解决方案1】:

    解压缩文件并不是您问题的难点。困难的部分是重命名,因为我们不知道解压缩了哪些文件/文件夹。所以这里的策略是在解压前获取文件夹中所有项目的列表,解压后获取文件夹中所有项目的另一个列表,然后比较这两个列表。第二个列表中没有第一个列表的项目是解压缩后的项目......所以如果它们的名称与 zip 文件不同,我们可以重命名它们。

    建议:要学习 applescript,请转到 here 并完成名为“AppleScript Tutorial for Beginners”的教程。我就是这样学习的。它们做起来很简单,你会学到很多东西。祝你好运。

    -- get the folder containing the zip files
    set folderOfZips to (choose folder) as text
    
    tell application "Finder"
        -- get the zip files from the chosen folder
        set zipFiles to files of folder folderOfZips whose name extension is "zip"
    
        repeat with aZip in zipFiles
            -- we use this when renaming the unzipped files
            set zipName to text 1 thru -5 of (get name of aZip)
    
            -- get a list of all the items in the folder before unzipping
            set origItems to name of items of folder folderOfZips
    
            -- unzip the item
            my unzipItem(aZip as text, folderOfZips)
    
            -- get a list of all the items in the folder after unzipping
            set nowItems to name of items of folder folderOfZips
    
            -- compare the 2 lists of items, before and after unzipping
            repeat with i from 1 to count of nowItems
                set thisItem to item i of nowItems
    
                -- if thisItem is not in origItems then it means this is one of the unzipped files, so we rename it
                if thisItem is not in origItems then
                    set thisItemPath to folderOfZips & thisItem
                    set ext to name extension of item thisItemPath
                    if ext is "" then
                        set newname to zipName
                    else
                        set newname to zipName & "." & ext
                    end if
                    if newname is not thisItem then
                        try
                            set name of item thisItemPath to newname
                        end try
                    end if
                end if
            end repeat
            move aZip to trash
        end repeat
    end tell
    
    tell me
        activate
        display dialog "Finished!" buttons {"OK"} default button 1 with icon note
    end tell
    
    on unzipItem(zipPath, destinationFolder)
        do shell script "/usr/bin/ditto -xk " & quoted form of POSIX path of zipPath & space & quoted form of POSIX path of destinationFolder
    end unzipItem
    

    【讨论】:

    • 这需要一些非常小的调整,非常感谢!我一直在努力做到这一点。 :)
    • 我很高兴它成功了。请将此答案标记为正确答案(通过检查),以便每个人都知道您得到了帮助。祝你好运。
    【解决方案2】:

    这是另一种方法:

    set mainFolder to (path to desktop as text) & "My Folder"
    tell application "Finder" to set myZips to (every file of folder mainFolder whose name extension is "zip") as alias list
    repeat with aZip in myZips
        set zipName to itemName(aZip)
        tell application "System Events"
            set newItem to open aZip
            set newItem's name to zipName
            delay 1
            delete aZip
        end tell
    end repeat
    
    on itemName(anItem)
        tell application "System Events" to set {name:fileName, name extension:nameExtension} to anItem
        set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
    end itemName
    

    【讨论】:

      【解决方案3】:
      temp=$(mktemp -d -t zip)
      trap "rm -r $temp" EXIT
      cd ~/Desktop
      
      find . -name \*.zip | while IFS= read -r f; do
        if [[ $(zip -sf "$f" | awk 'END{print $2}') -eq 1 ]]; then
          unzip "$f" -x __MACOSX/* -d $temp
          for file in $temp/*; do
            ext=${file##*.}
            mv "$file" "${f%.zip}.$ext"
          done
        elif [[ $(zip -sf "$f" | sed '1d;$d' | awk -F/ '!a[$1]++' | wc -l) -eq 1 ]]; then
          unzip "$f" -x __MACOSX/* -d $temp
          mkdir -p "${f%.zip}"
          mv $temp/*/* "${f%.zip}"
          rm -r $temp/*
        else
          unzip "$f" -x __MACOSX/* -d "${f%.zip}"
        fi
      done
      
      • 如果存档仅包含一个文件,则将文件重命名为与存档具有相同的基本名称
      • 如果所有成员都在一个顶级文件夹中,则该文件夹将重命名为与存档具有相同的基本名称
      • 否则会创建一个新的包含文件夹

      如果档案有 AppleDouble 文件,解压缩会创建一个 __MACOSX 文件夹,而不是将它们转换回扩展属性或其他元数据。 unar 将保留 AppleDouble 文件中的信息。

      这些总是会创建一个与存档同名的文件夹:

      for f in ~/Desktop/**/*.zip; do unzip "$f" -d "${f%.zip}"; done
      find ~/Desktop -name \*.zip | parallel unzip {} -d {.}
      

      如果archive.zip 通常被提取到archive/,上面的命令会将其提取到archive/archive/。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        相关资源
        最近更新 更多