通过交互式脚本批量处理文件
问题描述:
我有一个正在运行的Mac
正在运行的交互式脚本,它对位于我的桌面上未排序文件夹中的文件进行排序和处理。通过交互式脚本批量处理文件
目前,用户在命令行输入jpg
,脚本将执行并遍历未经分类的文件夹,在该文件夹中获取这些文件类型并在桌面上创建一个新目录并移动它们。
它的工作很棒,但我想进一步开发脚本,以便我可以批量处理,而不必一次输入一个终端命令。
即我可以输入一系列的参数到终端jpg
gif
docx
和脚本运行做出新的桌面目录为jpg
gif
docx
,所有这些类型的文件移动到这样的。
唯一需要注意的是,未分类文件夹中的剩余杂项文件(.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
答
像这样的东西应该工作:
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
答
#!/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
,这并不正常运行。文件夹被创建,但没有文件移动到它们中 –
移动命令没有被测试。我刚刚复制了你的移动命令。如果它不起作用,它一开始就不起作用。尝试调整移动部分。我会尽量在稍后测试它。 –
在我的代码,如果你改变这样的移动命令应该工作:mv -v unsorted /*.$ ext“$ destination”(从$ ext删除引号) –