Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ABS_PATH=.\
  4.  
  5. # Colourise the output
  6. RED='\033[0;31m' # Red
  7. GRE='\033[0;32m' # Green
  8. YEL='\033[1;33m' # Yellow
  9. NCL='\033[0m' # No Color
  10.  
  11. function file_specification() {
  12. FILE_NAME="$(basename "${entry}")"
  13. DIR="$(dirname "${entry}")"
  14. NAME="${FILE_NAME%.*}"
  15. EXT="${FILE_NAME##*.}"
  16. SIZE="$(du -sh "${entry}" | cut -f1)"
  17.  
  18. printf "%*s${GRE}%s${NCL}\n" $((indent+4)) '' "${entry}"
  19. printf "%*s\tFile name:\t${YEL}%s${NCL}\n" $((indent+4)) '' "$FILE_NAME"
  20. printf "%*s\tDirectory:\t${YEL}%s${NCL}\n" $((indent+4)) '' "$DIR"
  21. printf "%*s\tName only:\t${YEL}%s${NCL}\n" $((indent+4)) '' "$NAME"
  22. printf "%*s\tExtension:\t${YEL}%s${NCL}\n" $((indent+4)) '' "$EXT"
  23. printf "%*s\tFile size:\t${YEL}%s${NCL}\n" $((indent+4)) '' "$SIZE"
  24. }
  25.  
  26. function walk() {
  27. local indent="${2:-0}"
  28. printf "\n%*s${RED}%s${NCL}\n\n" "$indent" '' "$1"
  29. # If the entry is a file do some operations
  30. for entry in "$1"/*; do [[ -f "$entry" ]] && file_specification; # done
  31. # If the entry is a directory call walk() == create recursion
  32. for entry in "$1"/*; do [[ -d "$entry" ]] && walk "$entry" $((indent+4)); # done
  33. }
  34.  
  35. # If the path is empty use the current, otherwise convert relative to absolute; Exec walk()
  36. function view_dir () {
  37. [[ -z "${1}" ]] && ABS_PATH="${PWD}" || cd "${1}" && ABS_PATH="${PWD}"
  38. walk "${ABS_PATH}"
  39. echo
  40. }
  41.  
  42. # Search for key files and rename them based on "Lastname, Firstname" from the directory path and save
  43. # in the directory called.
  44.  
  45. TIMESTAMP=`date '+%Y%m%d_%H-%M-%S'`
  46. mkdir ${TIMESTAMP}
  47. mkdir "${TIMESTAMP}/Keyword"
  48.  
  49. echo "Copying source files"
  50. for f in ./**/**/Keyword*.doc; do
  51. cp -v "${f}" "${TIMESTAMP}/Keyword/$(echo "${f}" | grep -o '^\.\/[a-zA-Z0-9\-\s]*,\s[a-zA-Z0-9]*')_Keyword.doc"
  52. done
  53. for f in ./**/**/Keyword*.docx; do
  54. cp -v "${f}" "${TIMESTAMP}/Keyword/$(echo "${f}" | grep -o '^\.\/[a-zA-Z0-9\-\s]*,\s[a-zA-Z0-9]*')_Keyword.docx"
  55. done
  56.  
  57. echo "Listing contents"
  58. ls -al "${TIMESTAMP}"/Keyword
  59.  
  60. sleep 1 # Make sure to get a new timestamp
  61.  
  62. TIMESTAMP_1=`date '+%Y%m%d_%H_-%M-%S'`
  63. mkdir -p ${TIMESTAMP_1}/Originals
  64. mkdir -p ${TIMESTAMP_1}/Word
  65. mkdir -p ${TIMESTAMP_1}/Zip
  66. mkdir -p ${TIMESTAMP_1}/Xml
  67. mkdir -p ${TIMESTAMP_1}/Txt
  68.  
  69. echo "convert doc to docx"
  70. for f in *.doc; do
  71. textutil -convert docx "${f}"
  72. done
  73. for f in *.docx; do
  74. textutil -convert txt "${f}"
  75. done
  76.  
  77. cp -v *.doc "${TIMESTAMP_1}/Originals/"
  78. cp -v *.docx "${TIMESTAMP_1}/Word/"
  79. cp -v *.docx "${TIMESTAMP_1}/Xml/"
  80. cp -v *.txt "${TIMESTAMP_1}/Txt"
  81.  
  82. cd "${TIMESTAMP_1}/Xml/"
  83. for f in *.docx; do
  84. new_file="$(echo "${f}" | grep -o '^.*[^.docx]')"
  85. cp -v "${f}" "${new_file}.zip"
  86. unzip "${f}" -d "${new_file}"
  87. done
  88. mv -v *.zip ../Zip
  89. cd ../../
  90.  
  91. rm -v *.docx *.doc
  92.  
  93. # Now we have the converted textfiles, clean and prepare for SQLITE3 insertion
  94. for f in *.txt; do
  95. cat -s "{f}" > "{f}_.txt"
  96. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement