我有几个 .zip 文件的文件夹,我想执行以下操作:
- 将随附的文件或文件夹提取到与 .zip 相同的位置
- 将生成的文件或文件夹重命名为 .zip 文件的名称
- 删除 .zip 文件
.zip 文件始终包含单个文件或单个文件夹,因此无需担心重命名错误文件夹。
我对 Applescript 只有一点经验,对 shell 脚本几乎没有。任何人都可以提供帮助或提出任何建议吗?
谢谢!
我有几个 .zip 文件的文件夹,我想执行以下操作:
.zip 文件始终包含单个文件或单个文件夹,因此无需担心重命名错误文件夹。
我对 Applescript 只有一点经验,对 shell 脚本几乎没有。任何人都可以提供帮助或提出任何建议吗?
谢谢!
解压缩文件不是您问题的难点。困难的部分是重命名,因为我们不知道解压缩了哪些文件/文件夹。所以这里的策略是在解压前获取文件夹中所有项目的列表,解压后获取文件夹中所有项目的另一个列表,然后比较这两个列表。第二个列表中没有第一个列表中的项目是解压缩的项目......所以如果它们的名称与 zip 文件不同,我们可以重命名它们。
建议:要学习applescript,去这里做名为“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
这是另一种方法:
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
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
If the archives have AppleDouble files, unzip creates a __MACOSX folder instead of converting them back to extended attributes or other metadata. unar would preserve the information in AppleDouble files.
These would always create a folder with the same name as the archive:
for f in ~/Desktop/**/*.zip; do unzip "$f" -d "${f%.zip}"; done
find ~/Desktop -name \*.zip | parallel unzip {} -d {.}
If archive.zip
was normally extracted to archive/
, the commands above would extract it to archive/archive/
.