How to Get Input from a Barcode Scanner in a Batch Script
Integrating a barcode scanner into a batch script can dramatically speed up data entry for tasks like inventory management, asset tracking, or point-of-sale systems. While it might seem like a complex hardware integration problem, the solution is surprisingly simple. Most modern USB barcode scanners are designed to behave in a very user-friendly way: they emulate a keyboard.
This guide will explain this "keyboard wedge" concept and teach you how to use the standard SET /P command to reliably capture input from a barcode scanner in your batch scripts, just as if a user were typing the numbers in manually.
The Core Concept: Keyboard Emulation
The vast majority of modern USB barcode scanners are configured as "keyboard wedge" or "HID" (Human Interface Device) devices. This means that when they scan a barcode, they do two things:
- They instantly "type" the numbers or characters from the barcode.
- They then automatically "press" the Enter key.
To the operating system and your batch script, there is no difference between a user typing 12345 and pressing Enter, and a scanner scanning a barcode for 12345. This makes capturing the input incredibly easy.
The Core Command: SET /P
Since the scanner is emulating a keyboard that types a string and then presses Enter, the perfect command to capture this is SET /P. This command displays a prompt and waits for the user (or the scanner) to provide a line of input, which is terminated by the Enter key.
Syntax: SET /P "VariableName=Prompt for user: "
Basic Example: Capturing a Single Barcode
This script will prompt for a scan, wait for the barcode scanner to provide input, and then display the data it received.
@ECHO OFF
TITLE Barcode Scanner Input
ECHO --- Please scan a barcode now ---
ECHO.
SET "BarcodeData="
SET /P "BarcodeData=Scan item: "
ECHO.
ECHO --- Data Received ---
ECHO Scanned data: %BarcodeData%
ECHO.
PAUSE
The script will pause and wait for input.
--- Please scan a barcode now ---
Scan item:
When you scan a barcode (e.g., for "9780141036144"), the scanner will instantly populate the line and press Enter.
Scan item: 9780141036144
--- Data Received ---
Scanned data: 9780141036144
Press any key to continue . . .
Creating a Continuous Scanning Loop
For inventory or check-in systems, you'll want the script to be ready for the next scan immediately after the last one. A simple GOTO loop is perfect for this.
@ECHO OFF
TITLE Continuous Barcode Scanner
:ScanLoop
CLS
ECHO --- Ready for next scan ---
ECHO (Scan a barcode or type 'exit' and press Enter to quit)
ECHO.
SET "BarcodeData="
SET /P "BarcodeData=Scan item: "
REM Check if the user wants to exit
IF /I "%BarcodeData%"=="exit" GOTO :EOF
REM Process the data (in this case, just display it)
ECHO Scanned: %BarcodeData%
REM Add the data to a log file
ECHO %DATE% %TIME% - %BarcodeData% >> scan_log.txt
ECHO Item logged. Pausing for 2 seconds...
TIMEOUT /T 2 > NUL
GOTO :ScanLoop
Common Pitfalls and How to Solve Them
The Scanner's "Enter" Suffix
The entire system relies on the scanner being configured to send an "Enter" (Carriage Return/Line Feed) suffix after it transmits the barcode data. This is the default setting for almost all scanners.
Solution: If your script is not reacting to a scan, the first thing to check is the scanner's configuration. Most scanners are configured by scanning special barcodes in the user manual. Ensure that the "CR/LF Suffix" or "Enter Key" option is enabled.
Handling Accidental Keyboard Input
Because the scanner is a keyboard, your script can't tell the difference if a user accidentally types something into the command prompt and presses Enter.
Solution: This is generally not a problem and is often a feature. It allows a user to manually type in a barcode number if a label is damaged and won't scan. If you need to prevent this, a more advanced language (like PowerShell or C#) that can interact with specific hardware devices would be required.
Clearing the Variable Before Each Scan
This is a critical best practice for SET /P. If the user (or scanner) provides empty input (e.g., by just pressing Enter), the variable is left unchanged.
Solution: As shown in the loop example, always clear your variable (SET "BarcodeData=") immediately before the SET /P prompt. This ensures you don't accidentally process the same data twice.
Practical Example: A Simple Inventory Counter
This script allows a user to scan items to count them. It stores the results in a temporary file and uses find to count how many times each item has been scanned.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "InventoryFile=%TEMP%\inventory_session.txt"
IF EXIST "%InventoryFile%" DEL "%InventoryFile%"
TITLE Inventory Counter
:ScanLoop
CLS
ECHO --- Inventory Scan ---
ECHO Scanned items will be logged to: %InventoryFile%
ECHO (Type 'report' to finish, 'exit' to quit)
ECHO.
SET "Barcode="
SET /P "Barcode=Scan item: "
IF /I "%Barcode%"=="exit" GOTO :End
IF /I "%Barcode%"=="report" GOTO :ShowReport
ECHO !Barcode! >> "%InventoryFile%"
ECHO Logged: !Barcode!
TIMEOUT /T 1 > NUL
GOTO :ScanLoop
:ShowReport
CLS
ECHO --- Final Inventory Report ---
FOR /F "delims=" %%I IN ('SORT "%InventoryFile%" ^| UNIQ -c') DO (
ECHO %%I
)
PAUSE
GOTO :ScanLoop
:End
DEL "%InventoryFile%"
This advanced example uses SORT and UNIQ (if available from Git for Windows or similar toolsets) to generate the final count.
Conclusion
Integrating a barcode scanner with a batch script is a surprisingly simple and powerful way to automate data entry.
Key takeaways:
- Most USB barcode scanners act as a keyboard emulator that types the data and presses Enter.
- The
SET /Pcommand is the perfect tool to capture this input. - For continuous scanning, use a
GOTOloop. - Always clear your variable (
SET "Var=") before eachSET /Pprompt to prevent errors from empty input.
By using this simple technique, you can create efficient and user-friendly scripts for a wide range of inventory and data-capture tasks.