Search code examples
batch-filecmdbarcode-scannerbatch-rename

Rename file as per barcode using windows cmd or batch script


I have some scan images which having barcode i had found cmd barcode scanner tool

bardecode -f C:\input\file1.png -t code128 >> C:\output\output.txt

this will read the image file and save barcode number in output.txt file.

I want to make a process so this command run on each file in a folder and rename that file with its barcode first 3 or 4 digits.

i try for loop, it runs the command on all png files in folder and print output in file.

for %%i in (C:\input\*.png) do bardecode -f %%i -t code128 >> C:\output\output.txt

but i want to use output to rename that file.


Solution

  • So, if I understood well, with no layout to test, I image the loop to do this.

    If possible, please, provide barcode.exe output layout, without this, there no much references to perform test.

    Please note that I need to know the layout of output.txt to improve my code:

    @echo off && cd /d "%~dp0" & setlocal enableextensions enabledelayedexpansion
    
    for /f "tokens=* delims= " %%i in ('dir /b /a-d C:\input\*.png') do ( 
    
         for /f "tokens=*" %%I in ('"bardecode.exe" -f "!_name_!" -t code128') do (
    
             set _full_name=%%~fi
             set _name_only=%%~ni
             set _bar_code_to_rename=%%I
             set _bar_code_to_rename=!_bar_code_to_rename:~0,4!-!_name_only!
             ren "!_full_name!" "!_bar_code_to_rename!.png"
    
            )    
    
        )