2016-11-26 61 views
-1

我有一個正在運行的Mac正在運行的交互式腳本,它對位於我的桌面上未排序文件夾中的文件進行排序和處理。通過交互式腳本批量處理文件

目前,用戶在命令行輸入jpg,腳本將執行並遍歷未經分類的文件夾,在該文件夾中獲取這些文件類型並在桌面上創建一個新目錄並移動它們。

它的工作很棒,但我想進一步開發腳本,以便我可以批量處理,而不必一次輸入一個終端命令。

即我可以輸入一系列的參數到終端jpggifdocx和腳本運行做出新的桌面目錄爲jpggifdocx,所有這些類型的文件移動到這樣的。

唯一需要注意的是,未分類文件夾中的剩餘雜項文件(.wav png和其他擴展名的整數)需要在桌面中創建一個miscellaneous文件夾,並且在運行批處理後立即移動。

什麼是實現這種最尖端的方式。

read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" extension 
if cd /Users/christopherdorman/desktop; then 
    destination="folder$extension" 
    # ensure the destination folder exists 
    mkdir -p "$destination" 
    if mv -v unsorted/*."$extension" "$destination"; then 
     echo "Good News, Your files have been successfully processed" 
    fi 
fi 

回答

1

像這樣的東西應該工作:

read -a extensions -p "give me extensions seperated by spaces: " # read extensions and put them in array $extensions 
for ext in ${extensions[@]}; do #for each extension stored in the array extensions 
echo -e "- Working with extension $ext" 
destination="/Users/christopherdorman/desktop/folder$ext" 
mkdir -p "$destination" 
mv -v unsorted/*.$ext "$destination" 
done 

miscellaneous="/Users/christopherdorman/desktop/miscellaneous"  
mv -v unsorted/*.* "$miscellaneous"; 
# since previously you moved the required extensions to particular desktop folders 
# move what ever is left under unsorted folder to the desktop miscellaneous folder 
+0

,這並不正常運行。文件夾被創建,但沒有文件移動到它們中 –

+0

移動命令沒有被測試。我剛剛複製了你的移動命令。如果它不起作用,它一開始就不起作用。嘗試調整移動部分。我會盡量在稍後測試它。 –

+1

在我的代碼,如果你改變這樣的移動命令應該工作:mv -v unsorted /*.$ ext「$ destination」(從$ ext刪除引號) –

0
#!/bin/bash 
read -p "Good Morning, Please enter your file type name for sorting [ENTER]:" all_extensions 
if cd /Users/christopherdorman/desktop 
    then while read extension 
     do destination="folder$extension" 
     mkdir -p "$destination" 
     mv -v unsorted/*."$extension" "$destination" 
     done <<< "${all_extensions// /$'\n'}" 
     mkdir -p foldermisc 
     if mv -v unsorted/* "foldermisc" 
     then echo "Good News, the rest of Your files have been successfully processed" 
     fi 
fi 
相關問題