How to Rename a Batch of Files with Barcodes

batch file renaming using barcode

Last Updated on 2020-08-04

A barcode is an optical, machine-readable, representation of data. Barcodes are widely used in data capture and document management to improve business workflow and work efficiency.

For example, you may get challenges like this: You have thousands of images that you would like to rename in a folder. Each image is a scanned image file with a Code 39 on it. You’d like to rename the files based on the barcode value on each image.

Assume image 1 is called “image001.tiff” and the barcode value on that image is “Dynamsoft0002154”. You’d like the image file name to be “Dynamsoft0002154.tiff” so that when all files are finished renaming in a folder, you can easily find them by the barcode value.

You’d like a solution where a piece of software can loop through a folder, open the image, read the barcode and rename and save the image with that new name before moving on to the next. Read a related case study: German Location of World-Famous Charity Uses Dynamsoft Barcode Reader SDK to Automate Barcode-to-PDF Procedure.

In this post, we will demonstrate how to create a .NET application for automatic file renaming based on barcodes using Dynamsoft Barcode Reader SDK. You can create an application to loop files through a folder, open each image, read the barcode, and save the image with a new name based on the barcode value.

How to rename a batch of files with barcodes in C#

You can follow the steps below to create a C# WinForms application to do file renaming.

  1. Install Dynamsoft Barcode Reader SDK. You can download the 30-day free trial here.
  2. Create a C# Windows Forms Application and add “Dynamsoft.BarcodeReader.dll” to References. Learn more details on using Dynamsoft Barcode Reader .NET API in WinForms App >
  3. Call .NET barcode reading APIs to decode barcodes from images and rename files.
private void DoRename(string strInputFolder, string strOutputFolder)
{
    int iFileCount = 0;
    int iSuccCount = 0;
    DateTime dtStart = DateTime.Now;
    string[] files = Directory.GetFiles(strInputFolder); //get the files in the folder
    if (files != null)
    {
        foreach (string strFile in files)
        {
            if (IsImageFile(strFile))
            {
                try
                {
                iFileCount++;
                int iDirectSeparator = strFile.LastIndexOf(Path.DirectorySeparatorChar);
                string strFileName = strFile.Substring(iDirectSeparator + 1);
                tbLog.AppendText(string.Format("\r\nProcessing file {0}\r\n", strFileName));
                Bitmap bmp = (Bitmap)Bitmap.FromFile(strFile);
                barcodeReader.ReaderOptions.BarcodeFormats = BarcodeFormat.CODE_39; //specify the barcode type
                BarcodeResult[] barcodes = barcodeReader.DecodeBitmap(bmp);//decode the barcodes on the image
                bmp.Dispose();

                if (barcodes == null || barcodes.Length <= 0 )
                {
                    tbLog.AppendText("There is no barcode on the first page\r\n");
                }
                else
                {
                    tbLog.AppendText(string.Format("Page: {0}\r\n", barcodes[0].PageNumber));
                    tbLog.AppendText(string.Format("Barcode Value: {0}\r\n", barcodes[0].BarcodeText));

                    //output file name
                    int iDot = strFileName.LastIndexOf('.');
                    string strOutputFileName = barcodes[0].BarcodeText + strFileName.Substring(iDot); //use the barcode value to rename the file
                    string strOutputFile = null;
                    if (strOutputFolder.EndsWith(Path.DirectorySeparatorChar.ToString()))
                        strOutputFile = strOutputFolder + strOutputFileName;
                    else
                        strOutputFile = strOutputFolder + Path.DirectorySeparatorChar + strOutputFileName;
                    if (barcodes[0].BarcodeText.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
                    {
                        tbLog.AppendText(string.Format("{0} contains character(s) that are not allowed in file names, skip renaming\r\n", barcodes[0].BarcodeText));
                    }
                    else
                    {
                        if (File.Exists(strOutputFile))
                        {
                            tbLog.AppendText(string.Format("{0} exists, skip renaming {1}\r\n", strOutputFileName, strFileName));
                        }
                        else
                        {
                            System.IO.File.Move(strFile, strOutputFile);                            
                            iSuccCount++;                            
                            tbLog.AppendText(string.Format("Renamed to {0}\r\n", strOutputFileName));
                        }
                    }
                }
                catch (Exception exp)
                {
                    tbLog.AppendText(exp.Message + "\r\n");
                }
                tbLog.Refresh();
            }
        }             
    }

    tbLog.AppendText("Completed\r\n");
    tbLog.AppendText(string.Format("Files Total: {0} file(s), Success: {1} file(s)\r\n", iFileCount, iSuccCount));
    tbLog.AppendText(string.Format("Total cost time: {0}ms", (int)(DateTime.Now - dtStart).TotalMilliseconds));
}

If you want to  keep the old files after renaming, you can change Move to Copy:

System.IO.File.Copy(strFile, strOutputFile); 

You can download the complete sample code from our code gallery.

Let us know if you have any questions on how to use barcodes to rename files.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com