Batch processing files through an interactive script

I am newish to the site and to unix.

I have a functioning interactive script running on Mac that sorts and processes files located in an unsorted folder on my desktop.

As it currently stands, the user types jpg into the command line, the script executes and iterates through the unsorted folder where it takes files of type jpg and make a new directory on the desktop and moves them.

Its working terrific, but I want to develop the script further so that I can batch process without having to type single terminal commands one at a time.

i.e. I can type a series of arguments into the terminal jpg gif docx and the script will run and make new desktop directories for jpg gif docx and move all of these file types into such.

The only caveat is, The remaining miscellaneous files in the unsorted folder (.wav png and a whole litany of other extensions) need to have a miscellaneous folder created in the desktop and moved into such as soon as I run the batch.

What is the leanest way of achieving such.

#!/bin/bash
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

That seems to be a bash script, so be careful when running it with another shell.
How about reading in your space separated list of arguments and then using a for loop to loop through all of them? After the loop, run a similar logic again for the rest of the (unaddressed) extensions.

appreciate the prompt response, how do you mean by reading in your separated list of arguments. my apologies if the question is elementary ( I am first year software student)

This is not homework/coursework, is it? For such, special rules apply, c.f forum rules.

no i am experimenting with the language independently

Try, then,

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"
	  	if echo mv  -v unsorted/*."$extension" "$destination"
	  	  then	echo "Good News, Your files have been successfully processed"
	  	fi
	  done   <<< "${all_extensions// /$'\n'}"
	if echo mv  -v unsorted/* "foldermisc"
	  then	echo "Good News, the rest of Your files have been successfully processed"
	fi
fi

and report back on any difficulties or problems. It needs a recent bash , and the while loop is an alternative to a for construct. The echo es are a safety measure; remove them if happy with the output.

1 Like

Rudi, I managed to get it 3 quarters working which is a fantastic start. The folders are created for each of the three files stipulated in the terminal (jpg gif docx) and upon execution of the script, the new new folders are made and the files moved into their. Only thing I need to figure out is the miscellaneous folder is not created and the resulting extensions bumped into it.

Do once (outside script, as it will stay constant)

mkdir "$foldermisc"

thanks, i used mkdir -p foldermisc and it worked great.