Added snippet check_folder_for_extension.sh

This commit is contained in:
Stephan
2023-11-20 22:56:09 +01:00
commit 8ba5ad0203

51
check_folder_for_extension.sh Executable file
View File

@@ -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