Watchfolders - Part 4

2 minute read

The filemove script

The final part of the watchfolder process is the filemove script. This doesn’t have to be limited to moving files, but can be any script that you want to call. For my purpose, moving files from one place to another is what was required. As the files got moved, there was a bit of scope creep that manifested in the users starting to ask for any features that would make their jobs easier. One item that popped up was that there was a need that the file moving into the destination folder had a hard set name. That meant if a new file was generated it overwrote the existing file, so the logic to test if the file existed and rename it if it did exist was added. There was an ask to have a log created so that the users could have a trace of what was happening to the files. This was added using some echo statements. Along with this option the notifications were added so that it would email a group of people when the file had been renamed.

#!/bin/bash
 
#Used to move files in watch directories to NAS mounts
#This is called by the inotify processes which pass the
#watchdir, outdir, notify, filename and monitor  parameters.  Multiple inotify processes
#can call this file.
 
 
#ISSUE processing files with a space in them.  Problem is due to the $IFS variable set to the space character.
#SOLUTION: Set the IFS to "\n\b" then set it back at the end of the script
 
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
 
 
#variables passed from calling /etc/init.d/watcherScript
watchDir=$1
outDir=$2
notify=$3
fileName=$4
monitor=$5
 
#Set the log directory
logDir=$outDir"LOGS"
#Create the LOG DIR if it does not exist
mkdir -p "$logDir"
 
for file in `ls $watchDir`; do
 
        if [ "$fileName" = "DoNotRename" ]
        then
                fileName=$file
        fi
 
 
        #Check if the file is empty (0k) if it is append entry to LOG file
        fileSize=$(wc -c $watchDir$file | cut -f 1 -d ' ')
 
        if [ $fileSize -gt 0 ]; then
        #File Has Data
                chmod 666 $watchDir$file
                if [ -e "$outDir$fileName" ]
                then
                        # "$fileName exists" Move the file and Notify
                        lastMod=`stat -c %Y $outDir$fileName`
                        timeStamp=`date -d @$lastMod +"%Y%m%d_%H%M%S"`
                        extension="${fileName##*.}"
                        mv $outDir$fileName $outDir$timeStamp.$extension
                        #Send the email
                        echo "File $fileName already found in $outDir.  File has been renamed to $outDir$timeStamp.$extension" | mailx -v -s "INFORMATION: $fileName already exists" -S smtp=smtp://smtpserver -S from="from@username" $notify
                        #Write to the Log
                        echo -e "`date +"%Y%m%d_%H%M%S"`\tWARNING:\tFile $fileName already found in $outDir.  File has been renamed to $outDir$timeStamp.$extension\n\r" >> $logDir/$monitor.log
                fi
 
                mv "$watchDir$file" "$outDir$fileName" &> /dev/null
                echo -e "`date +"%Y%m%d_%H%M%S"`\tFILEMOVE:\t$watchDir$file moved to $outDir$fileName\n\r" >> $logDir/$monitor.log
 
        else
        #File Size is Zero (0)
                #Make the log entry
                echo  -e "`date +"%Y%m%d_%H%M%S"`\tINFORMATION:\tEmpty file $file processed by $monitor\n\r" >> $logDir/$monitor.log
                #remove the file
                rm -f $watchDir$file
 
        fi
 
sleep 2s
done
 
 
IFS=$SAVEIFS

« Watchfolders Part 3