[Bash] Rename files sequentially and padding 2
I’ve updated my last script in order to be able to manage different file extensions provided as parameters.
I’ve updated my last script in order to be able to manage different file extensions provided as parameters.
Maybe I’ve just reinvented the hot water. But I needed it and it took me less time to write it on my own than searching for the solution.
The task was to take all files in a directory and copy them into an output directory renaming them sequentially starting from a provided number. So files will be something like 0000.jpg, 0001.jpg, …, 0035.jpg, etc.
Less words as usual
#!/bin/bash
# copy all files in the directory to an output one renaming them
# sequentially.
#
# GNU LESSER GENERAL PUBLIC LICENSE
# Version 2.1, February 1999
#
# Copyright (C) 1991, 1999 Free Software Foundation, Inc.
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#
OUT_DIR=out
PAD_LENGTH=5
if [ -z "$1" ] || [ -z "$2" ] ; then
echo "./move.sh <number-to-start-from> <extension>"
echo " "
echo "./move.sh 12 JPG"
else
# testing output directory exist. if not create it.
if [ ! -d ${OUT_DIR} ] ; then
mkdir ${OUT_DIR}
fi
COUNTER=$1
EXT=$2
for file in *.${EXT} ; do
OUTFILE=$(printf "%0${PAD_LENGTH}i\n" "${COUNTER}")
cp -v $file ${OUT_DIR}/${OUTFILE}.${EXT};
let COUNTER++;
done
fi
Here is the source code (pdf) for easy read/copy/paste.
Configuring a modem in Windows XP for those like me used to use Debian can be a bit non trivial.
I got the Error 680: no dial tone. I know that it’s easy to solve this problem, just disable the “wait for dialtone” and put one comma before the number (wait for a second before dial). The non trivial part is to find where I can tell it. So, here it is:
Start->Settings->Control Panel->System and click on the Hardware tab and then on Device Manager.
Then select the modem from the list of devices. Right click and properties. Now on the Modem tab uncheck the Wait for dialtone
Formatting a number in the proper way is very important for the final user. And if you are in Locale different from the US one, then you can encounter some users that complain against it.
For example I’m Italian and we use as decimal separator a comma (,) and as thousands separator a dot (.). The opposite of the US. However the DecimalFormat accept only dots and commas in the US way. You can use the DecimalFormatSymbols in order to “translate” the format.
DecimalFormat df = new DecimalFormat("#,##0.00");
df.setDecimalFormatSymbos(new DecimalFormatSymbols(Locale.ITALY));
System.out.println(df.format(new BigDecimal(123456,78))); //will output 123.456,78
Another little and easy tip I often use. The task is to redirect the Throwable.printStackTrace() over a String object. In this way you can manage the string including it, for example, in an XML stream.
Less words, here it is
// "t" is an instance of Throwable //... StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); String error = sw.toString(); pw.close(); sw.close(); //...
TreeMap is a Map that keep the element in the natural order of the key. The order can be changed providing to the contructor of the TreeMap a Comparable. So you can implement your custom one for more specific tasks. However if you only need a reverse order, you can easily use the beautiful Collections class with his reverseOrder().
Here is a quick example
Map reverseOrderedMap = new TreeMap(Collections.reverseOrder());
Here it is. Easy! ![]()