commit 8ba5ad020384cba1687cf524a9d354292311f186 Author: Stephan Date: Mon Nov 20 22:56:09 2023 +0100 Added snippet check_folder_for_extension.sh diff --git a/check_folder_for_extension.sh b/check_folder_for_extension.sh new file mode 100755 index 0000000..068484e --- /dev/null +++ b/check_folder_for_extension.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +## Checks given folder for files with certain extensions. Subfolders are checked recursively. +## $1 Folder to check. +## $2 space separated list of extensions to check for. e.g.: "*.jpg *.JPG *.mp4 *.MP4". +## returns Count of files found with the desired extension. + +__check_folder() { + local _FOLDER=$1; + local _EXTENSIONS=$2; + local _FILE_C=0; + if [ -d "$_FOLDER" ]; then # is a folder + local _ITEM=""; + local _FC=${#FILESNPATHS[@]}; + pushd "$_FOLDER" 1>/dev/null; + for _ITEM in ${_EXTENSIONS}; do # lookup all files with the desired extensions + if [[ -f "$_ITEM" ]]; then # is a file + FILESNPATHS[$_FC]="$(pwd)/$_ITEM"; + ((_FC+=1)); + ((_FILE_C+=1)); + fi + done + # check subfolders if exist + local _SUBFOLDERS="$(/usr/bin/ls . 2>/dev/null)"; + local _CNT=0; + for _SUBITEM in ${_SUBFOLDERS}; do + if [[ -d "$_SUBITEM" ]]; then + __check_folder "$_SUBITEM" "$_EXTENSIONS"; + _CNT=$? + ((_FILE_C+=_CNT)); + fi + done + popd 1>/dev/null; + fi + return $_FILE_C +} + + +__test() { + MYFOLDER="./test"; + __check_folder $MYFOLDER "*.jpg *.JPG *.mp4 *.MP4" + FILE_COUNT=$? + echo "Found $FILE_COUNT files in folder $MYFOLDER, length of files array is ${#FILESNPATHS[*]}."; + for ((FILE_C=0;FILE_C<${#FILESNPATHS[*]};FILE_C++)); do + echo "$FILE_C ${FILESNPATHS[$FILE_C]}"; + done +} + +declare -A FILESNPAHTS; + +__test