Skip to content

Instantly share code, notes, and snippets.

@htlin222
Created June 12, 2023 15:45
Show Gist options
  • Save htlin222/77670fea2c6e06b92c69724dfb6f6190 to your computer and use it in GitHub Desktop.
Save htlin222/77670fea2c6e06b92c69724dfb6f6190 to your computer and use it in GitHub Desktop.
這段程式碼是用來將指定資料夾中的pptx, ppt, doc, docx,將其轉換為 PDF 格式並移動到新資料夾中。
#!/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
@htlin222
Copy link
Author

htlin222 commented Jun 12, 2023

這段程式碼是用來將指定資料夾中的特定檔案進行格式轉換和移動的腳本。

首先,我們定義了一個名為 folder_paths 的array,其中包含了三個資料夾的路徑。這些資料夾分別是 "/Users/mac/Documents/10_PPTX檔/"、"/Users/mac/Documents/10_DOC檔/" 和 "/Users/mac/Documents/10_DOCX檔/"。

接下來,程式進入迴圈,遍歷每個資料夾。對於每個資料夾路徑,程式會顯示正在處理的資料夾路徑。

然後,程式會遍歷資料夾中的每個檔案。使用通配符模式,程式會找到以 ".pptx" 或 ".ppt" 結尾的檔案。如果找到符合條件的檔案,程式會執行以下操作:

  1. 提取檔案名稱和副檔名。
  2. 檢查副檔名是否為 "pptx"、"ppt"、"doc" 或 "docx"。
  3. 如果副檔名符合條件,程式會顯示正在轉換的檔案名稱,然後使用 LibreOffice 命令將檔案轉換為 PDF 格式。
  4. 轉換完成後,程式會建立一個名為 "converted" 的資料夾(如果不存在),然後將原始檔案移動到該資料夾中。
  5. 最後,程式會顯示已移動檔案的資訊,包括檔案名稱和目標資料夾路徑。

當程式處理完一個資料夾後,會顯示該資料夾的處理完成訊息。

簡而言之,這段程式碼用於遍歷指定資料夾中的特定檔案,將它們轉換為 PDF 格式並將原始檔移動到converted資料夾中。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment