Advertisement
pintcat

unzoom v1.3

Jan 24th, 2016 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. DEF_SOURCE=/media/H6_SD/FOLDER01/
  4. DEF_TARGET=/media/ramdisk/
  5. DEF_FTYPE=.WAV
  6. DEF_IFS=$IFS
  7. SOURCE=$DEF_SOURCE
  8. TARGET=$DEF_TARGET
  9. FTYPE=$DEF_FTYPE
  10. TMP_FLIST=
  11. LIST=empty
  12. typeset -i TMP_COUNT=0
  13. typeset -i FAIL=0
  14.  
  15. function ERROR() # displays error message and explains how to operate this script
  16. {
  17.     printf "\033[0;31mError! $1\033[0;32m\n\n"
  18.     cat <<EOF
  19.  unzoom seeks the folder list created by the ZOOM H6 recorder, selects the audio files
  20.  by year, month, day, hour and/or file number and - if desired - copies them to a custom
  21.  target folder.
  22.  
  23.  Usage: unzoom.bash [-estymdhn]
  24.  Options: -e <extension> ..... Set file type (default is $DEF_FTYPE)
  25.           -s <path> .......... Set source directory (default is $DEF_SOURCE)
  26.           -t <path> .......... Set target directory (default is $DEF_TARGET)
  27.           -y <n1,n2,nX-nY> ... Filter by year (must be written exactly as in the file list)
  28.           -m <n1,n2,nX-nY> ... Filter by month
  29.           -d <n1,n2,nX-nY> ... Filter by day
  30.           -h <n1,n2,nX-nY> ... Filter by hour
  31.           -n <n1,n2,nX-nY> ... Filter by file number
  32.  
  33.  If you wish to change file type or source directory, the -e and -s options must be the first
  34.  of all options. When setting a file extension, consider that everything is case sensitive.
  35.  Each filter option must be followed by either a single number, several numbers separated by
  36.  comma or a range of numbers specified by the first and the last number divided by a "-".
  37.  And remember: If you intend to filter by year, the value must be written exactly as given in
  38.  the file list (i.e. if year ist 2010 then write 2010 and not just 10 instead). You can
  39.  combine all the filter options at discretion. Using no filter will of course bring up the
  40.  entire file list.
  41.  Beware: When copying files, all existing files with same names in the target folder will be
  42.  overwritten without request!
  43. EOF
  44.     printf "\033[0m\n"
  45.     exit 1
  46. }
  47.  
  48. function CHECK_MATCH() # checks if current file specs match with filter settings and calls BUILD_LIST() function
  49. {
  50.     typeset -i NUM=$(echo "obase=10;$1" | bc)
  51.     if [ $NUM -ge $NUM_LO ] && [ $NUM -le $NUM_HI ]; then BUILD_LIST; fi
  52. }
  53.  
  54. function BUILD_LIST() # builds file lists, skips existing lines and counts gathered entries
  55. {
  56.     NEW_LIST=$(printf "$NEW_LIST")"\n"$LINE
  57.     TMP_LINE=$(printf "/$FILE\t    time stamp:  $YEAR $MONTH $DAY $HOUR (year, month, day, hour)")
  58.     PREV_IFS=$IFS
  59.     IFS=$'\n'
  60.     for SEG in $(printf "$TMP_FLIST"); do
  61.         if [ "$TMP_LINE" = "$SEG" ]; then TMP_LINE=; fi
  62.     done
  63.     IFS=$PREV_IFS
  64.     if [ ! -z "$TMP_LINE" ]; then
  65.         TMP_FLIST=$(printf "$TMP_FLIST")"\n"$TMP_LINE
  66.         TMP_COUNT=$TMP_COUNT+1
  67.     fi
  68. }
  69.  
  70. function FILTER() # reads and filters the files according to the filter options set by the user
  71. {
  72.     if [ "$LIST" = empty ]; then
  73.         if [ ! -d "$SOURCE" ]; then ERROR "Source directory $SOURCE does not exist."; fi
  74.         for PROJECT in $(ls "$SOURCE"); do
  75.             if [ -d "$SOURCE$PROJECT" ]; then
  76.                 if ls "$SOURCE$PROJECT"/*$FTYPE >/dev/null 2>/dev/null; then
  77.                     if [ "$LIST" = empty ]; then LIST=; fi
  78.                     LIST=$(printf "$LIST")"\n"$(ls -g --time-style=long-iso "$SOURCE$PROJECT"/*$FTYPE)
  79.                 fi
  80.             fi
  81.         done
  82.     fi
  83.     IFS=","
  84.     for VAL in $1; do
  85.         typeset -i NUM_LO=${VAL%-*}
  86.         typeset -i NUM_HI=${VAL#*-}
  87.         IFS=$'\n'
  88.         for LINE in $(printf %b "$LIST"); do
  89.             if [ ! -z "$LINE" ]; then
  90.                 DATE=$(echo $LINE | awk {'print $5'})
  91.                 YEAR=${DATE%-*-*}
  92.                 MONTH=${DATE%-*}
  93.                 MONTH=${MONTH#*-}
  94.                 DAY=${DATE#*-*-}
  95.                 DAY=${DAY%\ *}
  96.                 TIME=$(echo $LINE | awk {'print $6'})
  97.                 HOUR=${TIME%:*}
  98.                 FILE=${LINE#*\ /}
  99.                 PNAME=${FILE%/*}
  100.                 FRONT=${FILE#$PNAME/ZOOM}
  101.                 REAR=${FRONT#????}
  102.                 if [ ! $1 = nothing ]; then
  103.                     if [ $2 = h ]; then CHECK_MATCH $HOUR; fi
  104.                     if [ $2 = d ]; then CHECK_MATCH $DAY; fi
  105.                     if [ $2 = m ]; then CHECK_MATCH $MONTH; fi
  106.                     if [ $2 = y ]; then CHECK_MATCH $YEAR; fi
  107.                     if [ $2 = n ]; then CHECK_MATCH ${FRONT%$REAR}; fi
  108.                 else BUILD_LIST
  109.                 fi
  110.             fi
  111.         done
  112.         IFS=","
  113.     done
  114.     IFS=$DEF_IFS
  115.     LIST=$(printf "$NEW_LIST")
  116.     NEW_LIST=
  117.     FLIST=$TMP_FLIST
  118.     TMP_FLIST=
  119.     COUNT=$TMP_COUNT
  120.     TMP_COUNT=0
  121. }
  122.  
  123. while getopts :e:s:t:h:d:m:y:n: OPT; do
  124.     case $OPT in
  125.     e)
  126.         if [ ! -z "$LINE" ]; then ERROR "-e must be specifyed BEFORE any filter option."; fi
  127.         FTYPE=$OPTARG
  128.         if [[ ! $FTYPE = .* ]]; then FTYPE=.$FTYPE; fi
  129.         ;;
  130.     s)
  131.         if [ ! -z "$LINE" ]; then ERROR "-s must be specifyed BEFORE any filter option."; fi
  132.         SOURCE="$OPTARG"
  133.         if [[ ! "$SOURCE" = */ ]]; then SOURCE="$SOURCE"/; fi
  134.         ;;
  135.     t)
  136.         if [ ! -d "$OPTARG" ]; then ERROR "Target directory $OPTARG does not exist."; fi
  137.         TARGET="$OPTARG"
  138.         ;;
  139.     h)
  140.         FILTER $OPTARG h
  141.         ;;
  142.     d)
  143.         FILTER $OPTARG d
  144.         ;;
  145.     m)
  146.         FILTER $OPTARG m
  147.         ;;
  148.     y)
  149.         FILTER $OPTARG y
  150.         ;;
  151.     n)
  152.         FILTER $OPTARG n
  153.         ;;
  154.     :)
  155.         ERROR $OPTARG" requires an argument."
  156.         ;;
  157.     ?)
  158.         ERROR "Unknown option: "$OPTARG
  159.         ;;
  160.     esac
  161. done
  162. if [ -z "$LINE" ]; then FILTER nothing; fi
  163. if [ -z "$FLIST" ]; then ERROR "$SOURCE doesn't contain any useful data."; fi
  164. printf "$FLIST\n\n"
  165. read -p $COUNT" files selected. Copy these files to $TARGET (y/N)? " YN
  166. case $YN in
  167.     y | Y | yes | Yes | YES)
  168.         IFS=$'\n'
  169.         typeset -i COUNT=0
  170.         echo
  171.         for PROCESS in $(printf "$FLIST"); do
  172.             FILE_PATH=${PROCESS%$'\t'"    time stamp:"*}
  173.             if cp -v --preserve=timestamps $FILE_PATH $TARGET; then
  174.                 COUNT=$COUNT+1
  175.             else
  176.                 FAIL=$FAIL+1
  177.             fi
  178.         done
  179.         IFS=$DEF_IFS
  180.         echo && echo "Done - "$COUNT" files copied, "$FAIL" failed."
  181.         ;;
  182.     ?)
  183.         ;;
  184. esac
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement