我有几个.zip文件的文件夹,我想执行以下操作:
.zip文件始终包含单个文件或单个文件夹,因此无需担心重命名错误的文件夹。
我对Applescript只有一点经验,而且几乎没有使用shell脚本的经验。任何人都可以提供帮助或提出任何建议吗?
谢谢!
答案 0 :(得分:0)
解压缩文件并不是您问题的难点。困难的部分是重命名,因为我们不知道解压缩了哪些文件/文件夹。所以这里的策略是在解压缩之前获取文件夹中所有项目的列表,在解压缩后获取文件夹中所有项目的另一个列表,然后比较2个列表。第二个列表中不在第一个列表中的项目是解压缩的项目...因此,如果它们的名称与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
答案 1 :(得分:0)
这是另一种方法:
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
答案 2 :(得分:0)
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/
。