-
Notifications
You must be signed in to change notification settings - Fork 3
/
find_files2json.sh
47 lines (40 loc) · 1000 Bytes
/
find_files2json.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Make sure we have our parameters
if [[ "${1}" == "" ]]; then
echo "Missing filename to search for"
exit 1
fi
if [[ "${2}" == "" ]]; then
echo "Missing folder to search through"
exit 2
fi
if [[ "${3}" == "" ]]; then
echo "Missing destination file for JSON"
exit 3
fi
SEARCH_NAME="${1}"
SEARCH_FOLDER="${2}"
JSON_FILE="${3}"
echo "File name to look for: ${SEARCH_NAME}"
echo "Searching in folder: ${SEARCH_FOLDER}"
# shellcheck disable=SC2206
clips=(${SEARCH_FOLDER}/*)
echo "${clips[@]}"
found_files=0
echo "{\"FILE_LIST\": [" >>"${JSON_FILE}"
sep=""
for entry in "${clips[@]}"; do
possible="${entry}/${SEARCH_NAME}"
echo "Checking possible ${possible}"
if [ -f "${possible}" ]; then
echo "${sep}{\"FILE\": \"${possible}\"," >>"${JSON_FILE}"
echo "\"DIR\": \"${entry}/\"}" >>"${JSON_FILE}"
sep=","
((found_files++))
fi
done
echo "]}" >>"${JSON_FILE}"
if [ "$found_files" -eq "0" ]; then
rm "${JSON_FILE}"
fi
echo "Found ${found_files} files"