Tuesday, November 25, 2014

fileconverter.sh


This is a script to convert files in batch to given format, kind of like pandoc.


#!/bin/sh -u 
echo -e "$0 is loaded."
echo -e "You need to put all the required files together in a directory, press Y if yes, N if no"
read choice
if [ "$choice" = "y" -o "$choice" == "Y" ]
then 
 echo -e "Example: If the files are in a directory called 'apple', /folder1/folder2/apple is location into the directory apple."
 echo -e "Enter the location into the directory as absolute path, . for current directory: "
 read path
 if [ -n "$path" ]
 then 
  current = pwd  
  cd $path
  echo "Example: If the file to be converted is of type document, it has extension .doc, in that case enter *.doc"
  echo "Example: If you have multiple file types, seperate them with spaces. If you have *.doc and *.html enter"
  echo "*.doc *.html"
  echo "Enter the file types to be converted as illustrated above : "
  read from
  declare -a var
  mkdir converted_files
  var=($from)
  count=0
  for i in "${var[@]}"
  do
   echo $i
   let count=count+1
  done
  echo "Total $count files were found."
  echo "Example: If the file is to be converted into type document, it should have extension .doc, in that case enter .doc"
  echo "Enter the file type into which these files have to be converted."
  read to
  for i in "${var[@]}"
  do
   echo "$i is being converted"
   newt[$count]="`echo $i | cut -d '.' -f1`"
   v="`echo $i | cut -d '.' -f2`"
   if [ "$v" = "$to" ]
   then
    newt[$count]="${newt[$count]}[converted]"
   fi
   echo "Extension removed : ${newt[$count]}" 
   newt[$count]="${newt[$count]}$to"
   echo "Created a $to file of name : ${newt[$count]}"
   echo -e "Converted into a $to file from a shell script designed by SANTOSH KUMAR DESAI. \n\n\n" > ${newt[$count]}
   cat $i >> ${newt[$count]}
   echo -e "\n\n\nEnd of the document. Conversion courtesy : SANTOSH KUMAR DESAI" >> ${newt[$count]}
   echo "Copied content into the $to file : ${newt[$count]}"
   mv ${newt[$count]} ./converted_files
   echo "Moved ${newt[$count]} to the folder converted_files."
  done
  cd ./converted_files
  numfiles = ls -l | wc -l
  echo -e "Total $numfiles files were copied."
  if [ ! $count -eq $numfiles ]
  then 
   echo "You must have files with same names or same names but different extensions. Please check them out and try again."
  fi
  echo -e "Converting all the files into $to files is complete.\n"
  echo -e "The files are in converted_files directory and it will be available in the same directory as other source files."
  echo -e "Thanks for using this shell script ---- Horopter."
 fi
fi 
echo "Exiting the script."
cd $current
exit 0

No comments:

Post a Comment