, ,

When processing images that contain barcodes such a scanned documents or photographs of products you may wish to extract the information from the barcode and rename the image.

This batch file does this using the excellent zbar package. If you need to generate barcode images you can use the excellent Avery Design and Print service or create them in Excel or Word using a free barcode font.

REM (c 2016)James Bayley, MIT Licence (so you can use it freely)
REM Example of use extract-barcode-rename-file.bat image-with-barcode.jpg
ECHO off
REM Barcode labels can be printed using Avery Online Design and Print or
REM from Word or Excel using a free barcode font https://www.barcodesinc.com/free-barcode-font/
REM zbarimg from http://zbar.sourceforge.net/download.html
REM capture command output http://stackoverflow.com/questions/6359820/how-to-set-commands-output-as-a-variable-in-a-batch-file
REM substring http://stackoverflow.com/questions/636381/what-is-the-best-way-to-do-a-substring-in-a-batch-file
FOR /F "tokens=* USEBACKQ" %%F IN (`zbarimg -D -q %1`) DO (
 SET fullbarcode=%%F
 REM for my codes the digits start at 8 and the number is 5 characters long.
 SET barcodedigits=%fullbarcode:~8,5%
ECHO Full barcode: %fullbarcode%
ECHO Digits only: %barcodedigits%
rename %1 %barcodedigits%.jpg