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% ECHO ON rename %1 %barcodedigits%.jpg
Hi, I’m trying to use the script but it fails in two ways.
1. First it asks to specify which file is to be processed. Take a look:
http://imageshack.com/i/poF8McDMp
I tried to modify the line
zbarimg -D -q %1
to
zbarimg -D -q %1 *.tif
2. After this it succesfully finds barcode but fails to rename the file. It seems that %1 paramteter is not indicating properly the original filename that is supposed to be renamed.
http://imageshack.com/i/plbChoFfp
Dear David, From your screenshots it appears that you are calling
zbar.bat
You have not included the file name. If your file is called myImage.tif then the correct statement would be,
zbar.bat myImage.tif
In a batch file %1 means “the first parameter after the batch file name”. You can see another example of how to use the program on the first line of the batch file.
James
Hi James, thank you for the reply. Your advice worked. However the bat script works only for a single file. Is there a way to process many files in a folder? If I call zbar.bat on another file it erroneously calculate the “digits only” parameter (from the previous file). See screen below. I tried to call “zbar.bat *.tiff” as well but I get simillar error that file name is repeated or incorrect. My goal is to rename many files in a folder with barcodes numbers in each file as I have many dozens of files to rename every day.
http://imageshack.com/i/powSLIj2p
After further research I’ve found a way to process many files in a folder. I removed the “digits only” function from the script so the rename command is “rename %1 %fullbarcode%.tif” and added “–raw” option to zbar command to get rid of barcode type value. After this modifications to run the batch script on many files in a folder (assuming the batch file is named “zbar.bat” and we are processing “.tif” files) we simply call the following command in cmd window: for /f “tokens=*” %a in (‘dir /b *.tif’) do zbar.bat %a
I hope it helps anybody who stumbles upon the same problem. Anyway, thank you for sharing the script – it is really helpful and makes my daily toil at work less tedious 🙂
Nice work. This will be very useful for anyone who needs to do more than one file at a time.
Hi David can you please leave a screenshot for the multiple files renaming
I am interested as well if anyone has the full code. I am newbie
The sample code:
File:zbar.bat and Type Files: JPG
ECHO off
FOR /F “tokens=* USEBACKQ” %%F IN (`zbarimg -D -q %1`) DO (
SET fullbarcode=%%F
SET barcodedigits=%fullbarcode:~8,12%
)
ECHO Full barcode: %fullbarcode%
ECHO Digits only: %barcodedigits%
ECHO ON
rename %1 %barcodedigits%.jpg
And sequence en CMD:
for /f “tokens=*” %a in (‘dir /b *.jpg’) do zbar.bat %a
I tried this exactly and keep getting an error of:
*” was unexpected at this time.
Could you please provide some screenshots or more detail on how to rename all the JPG’s in a folder?
Sorry I published this 6 years ago and I don’t have the testing environment any more. If you need more help on the FOR /F command you can find it here. https://ss64.com/nt/for_f.html. Please also read the other comments on this article you may find they help. “.bat” is quite stable so I will be very surprised if it not a simple issue. Good luck!