Created
June 12, 2023 15:45
-
-
Save htlin222/77670fea2c6e06b92c69724dfb6f6190 to your computer and use it in GitHub Desktop.
這段程式碼是用來將指定資料夾中的pptx, ppt, doc, docx,將其轉換為 PDF 格式並移動到新資料夾中。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# title: watch_and_convert | |
# date created: "2023-06-12" | |
# | |
# prerequisite: install the LibreOffice first to use the bin soffice | |
# https://www.libreoffice.org/download/download-libreoffice/ | |
# remember to chmod a+x *.sh | |
# chose the folder you want | |
# List of folders to process | |
folder_paths=( | |
"/Users/mac/Documents/10_PPTX檔/" | |
"/Users/mac/Documents/10_DOC檔/" | |
"/Users/mac/Documents/10_DOCX檔/" | |
) | |
# Iterate over each folder | |
for folder_path in "${folder_paths[@]}"; do | |
echo "Processing folder: $folder_path" | |
for file in "$folder_path"/*.pptx "$folder_path"/*.ppt; do | |
if [ -f "$file" ]; then | |
filename=$(basename "$file") | |
extension="${filename##*.}" | |
if [ "$extension" == "pptx" ] || [ "$extension" == "ppt" ] || [ "$extension" == "doc" ] || [ "$extension" == "docx" ]; then | |
echo "Converting $filename" | |
command /Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf "$file" | |
echo "$filename converted" | |
mkdir -p "$folder_path/converted" | |
mv "$file" "$folder_path/converted/$filename" | |
echo "Moved $filename to $folder_path/converted directory" | |
fi | |
fi | |
done | |
echo "Folder: $folder_path processing complete" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
這段程式碼是用來將指定資料夾中的特定檔案進行格式轉換和移動的腳本。
首先,我們定義了一個名為
folder_paths
的array,其中包含了三個資料夾的路徑。這些資料夾分別是 "/Users/mac/Documents/10_PPTX檔/"、"/Users/mac/Documents/10_DOC檔/" 和 "/Users/mac/Documents/10_DOCX檔/"。接下來,程式進入迴圈,遍歷每個資料夾。對於每個資料夾路徑,程式會顯示正在處理的資料夾路徑。
然後,程式會遍歷資料夾中的每個檔案。使用通配符模式,程式會找到以 ".pptx" 或 ".ppt" 結尾的檔案。如果找到符合條件的檔案,程式會執行以下操作:
當程式處理完一個資料夾後,會顯示該資料夾的處理完成訊息。
簡而言之,這段程式碼用於遍歷指定資料夾中的特定檔案,將它們轉換為 PDF 格式並將原始檔移動到
converted
資料夾中。