-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpg-this-dir.sh
executable file
·101 lines (91 loc) · 2.55 KB
/
jpg-this-dir.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh
customDie() {
cat <<END
ERROR:
$@
END
exit 1
}
usage() {
cat > ~/jpg-this-dir.txt <<END
You must specify a directory or file.
- If directory: This script will convert all png files in the directory
to jpg, then the png files will be moved to a directory called "png"
- If file: the above steps will be taken on the directory that contains
the file.
END
cat ~/jpg-this-dir.txt
}
if [ -z "$1" ]; then
usage
exit 1
fi
parentdir=""
if [ ! -d "$1" ]; then
if [ ! -f "$1" ]; then
usage
exit 1
else
parentDir="$(dirname "$1")"
fi
else
parentDir="$1"
fi
if [ ! -d "$parentDir/png" ]; then
mkdir "$parentDir/png" || customDie "Cannot create png directory"
fi
if [ ! -d "$parentDir/tga" ]; then
mkdir "$parentDir/tga" || customDie "Cannot create tga directory"
fi
# see https://superuser.com/questions/71028/batch-converting-png-to-jpg-in-linux
# ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.*}.jpg"' # this is good too
convert_count=0
convert_failed_count=0
mv_count=0
mv_failed_count=0
echo "Converting (moving only successfully converted files)..."
echo "#!/bin/sh" > ~/jpg-this-dir.txt
echo "# `date`" > ~/jpg-this-dir.txt
cd "$parentDir" || customDie "cd $parentDir # FAILED"
convertOne() {
failed=false
i=$1
ext="$2"
convert "$i" "${i%.*}.jpg" || failed=true
if [ "@$failed" = "@true" ]; then
echo "convert \"$i\" \"${i%.*}.jpg\"" >> ~/jpg-this-dir.txt
echo "# ^ failed" >> ~/jpg-this-dir.txt
convert_failed_count=$((convert_failed_count+1))
else
echo "# convert \"$i\" \"${i%.*}.jpg\"" >> ~/jpg-this-dir.txt
echo "# # ${i%.*}.jpg successful"
convert_count=$((convert_count+1))
failed=false
mv -f "$parentDir/$i" "$parentDir/$ext/" || failed=true
if [ "@$failed" = "@true" ]; then
echo "mv -f \"$parentDir/$i\" \"$parentDir/$ext/\"" >> ~/jpg-this-dir.txt
echo "# ^ failed" >> ~/jpg-this-dir.txt
mv_failed_count=$((mv_failed_count+1))
else
echo "# mv -f \"$parentDir/$i\" \"$parentDir/$ext/\"" >> ~/jpg-this-dir.txt
echo "# # successful"
mv_count=$((mv_count+1))
fi
# echo "Moving $i..."
fi
}
for i in *.png
do
convertOne $i "png"
done
for i in *.tga
do
convertOne $i "tga"
done
echo "# Converted $convert_count ($convert_failed_count failed)." >> ~/jpg-this-dir.txt
echo "# Moved $mv_count ($mv_failed_count failed)." >> ~/jpg-this-dir.txt
# open ~/jpg-this-dir.txt
cat ~/jpg-this-dir.txt
echo "Done."
echo
echo