Skip to content

Instantly share code, notes, and snippets.

@loloof64
Last active December 4, 2016 21:24
Show Gist options
  • Select an option

  • Save loloof64/f9acbb55664db1f55aacfe621c5f7cd9 to your computer and use it in GitHub Desktop.

Select an option

Save loloof64/f9acbb55664db1f55aacfe621c5f7cd9 to your computer and use it in GitHub Desktop.
Copying android pictures from a downloaded resource folder into an Android Studio resources folders
#!/bin/bash
FOLDER_DOES_NOT_EXIST=1
FOLDER_LACKS_SOME_SUBFOLDERS=2
PROMPT_EMPTY=3
# Getting the source folder
echo -n "What is the android resource pack root folder (the one that has the pictures to be copied) ? ($PWD) "
read srcFolder
srcFolder="${srcFolder/#\~/$HOME}" # Substituting the beginning ~ with $HOME value
if [ -z $srcFolder ]
then
srcFolder=$PWD
fi
if [ ! -d $srcFolder ]
then
echo "The folder $srcFolder does not exist !"
exit $FOLDER_DOES_NOT_EXIST
fi
echo "Using source folder $srcFolder."
echo -n "What is the base name (XXX) of drawbles subfolders inside source folder, e.g XXX-hdpi ? (mipmap) ? "
read drawablesBaseNameForSource
if [ -z $drawablesBaseNameForSource ]
then
drawablesBaseNameForSource="mipmap"
fi
# Checking that the source folder has the five drawable-{h,m,xh,xxh,xxxh}dpi subfolders.
IFS=' ';declare -a bases=(h m xh xxh xxxh)
for current_base in ${bases[@]}
do
sub_folder_name="${drawablesBaseNameForSource}-${current_base}dpi"
sub_folder_path="$srcFolder/$sub_folder_name"
if [ ! -d $sub_folder_path ]
then
echo "No folder $sub_folder_name in given source folder ($srcFolder) !"
exit $FOLDER_LACKS_SOME_SUBFOLDERS
fi
done
# Getting the destination folder and the resource folders name base
echo -n "What is the destination folder, base of all drawables folders ? "
read destBaseFolder
destBaseFolder="${destBaseFolder/#\~/$HOME}"
if [ -z $destBaseFolder ]
then
echo "Destination folder name can't be empty !"
exit $PROMPT_EMPTY
fi
if [ ! -d $destBaseFolder ]
then
echo "Destination folder does not exist !"
exit $FOLDER_DOES_NOT_EXIST
fi
# checks that dest base folder name does not end with /
substIndex=$((${#destBaseFolder}-1))
lastChar="${destBaseFolder:$substIndex:1}"
if [ '/' = $lastChar ]
then
destBaseFolder=${destBaseFolder::-1}
fi
echo -n "What is the base name (XXX) of drawbles subfolders inside destination folder, e.g XXX-hdpi ? (mipmap) ? "
read drawablesBaseNameForDest
if [ -z $drawablesBaseNameForDest ]
then
drawablesBaseNameForDest="mipmap"
fi
# Processing files
for current_base in ${bases[@]}
do
current_src_folder="${srcFolder}/$drawablesBaseNameForSource-${current_base}dpi"
current_dest_folder="${destBaseFolder}/${drawablesBaseNameForDest}-${current_base}dpi"
mkdir -p $current_dest_folder;
for subElement in "$current_src_folder"/*
do
if [ -f $subElement ]
then
cp -v $subElement $current_dest_folder
if [ $? -ne 0 ]
then
echo "Error copying file $subElement !"
fi
fi
done
done
echo "Done."
@loloof64

loloof64 commented Sep 9, 2016

Copy link
Copy Markdown
Author

Please don't be too critic : just a poor script written in just 3/4 hours though I am a bash scripting beginner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment