Web Demos

BARCODE READER SDK DEMO

Explore the flexibe barcode reading settings to optimize for your specific usage scenario.

WEB TWAIN SDK DEMO

Try the most popular web scanner features: scan images, import local images and PDF files, edit, save to local, upload to database, and etc.

BARCODE READER JAVASCRIPT DEMO

Transform any camera-equipped devices into real-time, browser-based barcode and QR code scanners.

MRZ SCANNER WEB DEMO

Detects the machine-readable zone of a passport, scans the text, and parses into human-readable data.

APP STORE DEMOS

BARCODE READER SDK FOR IOS

BARCODE READER SDK FOR ANDROID

VIEW MORE DEMOS >

How to Rename a Batch of Files with Barcodes

Dec 15, 2017
How to Rename a Batch of Files with Barcodes

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.

Let’s consider a situation: You have thousands of images that you would like to rename. 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.

For example, image 1 is called “image001.tiff” and the barcode value on that image is “0002154”. The image file should be renamed as “0002154.tiff”. When all files are finished renaming, you can easily find them by the barcode value.

You’d like a piece of software that 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. 

Case study

Read a related case study: German Red Cross Uses Dynamsoft Barcode Reader SDK to Quickly Automate Barcode-to-PDF Procedure.

Rename a batch of files with barcodes in C#

If you are not using C#, please contact support@dynamsoft.com for source code in other programming languages, such as JavaScript, Python, and more.

We will demonstrate how to create a .NET application for automatic file renaming based on barcodes using Dynamsoft Barcode Reader SDK.

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.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com