Batch processing files through an interactive script


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Batch processing files through an interactive script
# 1  
Old 11-26-2016
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.

Code:
#!/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


Last edited by Braveheart; 11-26-2016 at 11:51 AM..
# 2  
Old 11-26-2016
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.
# 3  
Old 11-26-2016
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)
# 4  
Old 11-26-2016
This is not homework/coursework, is it? For such, special rules apply, c.f forum rules.
# 5  
Old 11-26-2016
Quote:
Originally Posted by RudiC
This is not homework/coursework, is it? For such, special rules apply, c.f forum rules.
no i am experimenting with the language independently
# 6  
Old 11-26-2016
Try, then,
Code:
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 echoes are a safety measure; remove them if happy with the output.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-26-2016
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.

Last edited by Braveheart; 11-26-2016 at 01:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to use gpg to encrypt data in batch/non-interactive mode?

Hello, I have to encrypt data using a script in batch mode without being prompted. I can successfully encrypt data but it prompts me to enter (y/N) as shown below. I tried batch flags without no success. I need to automate this without any prompts. Appreciate your inputs. Thanks, C. $... (2 Replies)
Discussion started by: calredd
2 Replies

2. Shell Programming and Scripting

Sftp batch processing commands

Hello, I have a UNIX script to sftp batch processing. Here is my sftp command. ftp -b toopc userid@sftp.hostname.com In the file toopc I have the following commands: mget *.csv bye This brings in all files with an extension of .csv However, I need to only bring in files that ... (6 Replies)
Discussion started by: schlinzj
6 Replies

3. Shell Programming and Scripting

Need to exclude .NFSxxx files in clear old files batch script

I am new to Shell Scripting and need some help. The following batch job has been failing for me due to the .nfsxxx files in use. I need to know how to modify the following script to exclude the .nfsxxx files so this batch job will not fail on me. I have done lots of googling and keep coming back... (2 Replies)
Discussion started by: kimberlyg2007
2 Replies

4. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

5. Shell Programming and Scripting

How to batch-processing numerous shell scripts?

How to batch-processing numerous shell scripts? how to record the result of all the scripts as a report? then, I can analysis the process result. I want to process numerous shell scripts in my working directory: the directory name is consistent with shell scripts name, that is to say,... (2 Replies)
Discussion started by: qcmao
2 Replies

6. Shell Programming and Scripting

script for renaming a batch of files

hi i have a folder full of files. some of the names are quite off because the dimensions were the same and i had to put a 'b' after the initial number so that it didnt overwrite. what i want is a script in unix to overwrite the filwe name leaving some of the title intact, e.g. below are some... (3 Replies)
Discussion started by: shabs1985
3 Replies

7. Shell Programming and Scripting

Processing different jobs as a batch process

Hi All, I want to process consecutive jobs in a sequence but when I execute 1 job ,the control does not return to the command prompt to continue with the next job. Can anyone help me here? Thanks (3 Replies)
Discussion started by: Taranjeet Singh
3 Replies

8. Homework & Coursework Questions

How to write script that behaves both in interactive and non interactive mode

Q. Write a script that behaves both in interactive and non interactive mode. When no arguments are supplied it picks up each C program from the directory and prints first 10 lines. It then prompts for deletion of the file. If user supplies arguments with the script , then it works on those files... (8 Replies)
Discussion started by: rits
8 Replies

9. Shell Programming and Scripting

bash - batch processing folder of files by name

Hello Everyone!!! I need some help with a shellscript to batch process a folder of files with the imagemagick convert -append/+append command. The folder contains some hundred or thousand of small images in .png format which I would like to join together in order of their filenames. The... (3 Replies)
Discussion started by: imtombi
3 Replies

10. UNIX for Dummies Questions & Answers

Regarding interactive ID of batch job

Hi all, We are running a batch job using Unix script. This batch job is running daily to get data from a Mainframe database and to load our tables. We are using an interactive ID for the batch job. If we removed that interactive ID, the batch job fails. What's my question is.. Is the... (1 Reply)
Discussion started by: pradeep.edara
1 Replies
Login or Register to Ask a Question