Skip to content

How to Implement a Queue (FIFO) Data Structure in Batch Script

Learn how to create a FIFO queue in Windows Batch scripting using environment variables and head/tail pointers for ordered task processing, job scheduling, and log management.

In computer science, a Queue is a data structure that follows the FIFO (First-In, First-Out) principle. Imagine a line of people waiting for a bus: the first person to join the line is the first one to get on the bus. Queues are essential for managing background tasks, processing logs in order, or building a "To-Do" list for a multi-stage automation script.

In this guide, we will demonstrate how to build a Queue in Batch using two pointers: "Head" and "Tail."

The Strategy: Head and Tail Pointers

To implement a queue:

  1. Initialize head=1 (where we take items from) and tail=0 (where we add items to).
  2. ENQUEUE (Add): Increment tail and store the value at QUEUE_%tail%.
  3. DEQUEUE (Remove): Retrieve the value at QUEUE_%head% and then increment head.

Implementation Script

@echo off
setlocal enabledelayedexpansion

set "head=1"
set "tail=0"

echo --- QUEUE DEMONSTRATION (FIFO^) ---

:: 1. ENQUEUE elements (Add to back)
call :enqueue "Fix Permissions"
call :enqueue "Cleanup Logs"
call :enqueue "Restart Service"

echo.
call :size
call :peek
echo.

:: 2. DEQUEUE elements (Remove from front)
:process_loop
if !head! GTR !tail! (
    echo [VACANT] All tasks in the queue are complete.
    goto :end
)
call :dequeue
echo [STARTING] !result!
goto :process_loop

:end
echo.
echo --- QUEUE COMPLETE ---

endlocal
pause
exit /b 0

:: ============================================
:: QUEUE FUNCTIONS
:: ============================================

:enqueue
:: Usage: call :enqueue "value"
set /a "tail+=1"
set "QUEUE_!tail!=%~1"
echo Adding: %~1
exit /b

:dequeue
:: Usage: call :dequeue  (result stored in !result!)
if !head! GTR !tail! (
    echo [ERROR] Queue underflow - cannot dequeue from an empty queue.
    set "result="
    exit /b 1
)
set "result=!QUEUE_%head%!"
set "QUEUE_!head!="
set /a "head+=1"
exit /b

:peek
:: Usage: call :peek  (displays front element without removing it)
if !head! GTR !tail! (
    echo [PEEK] Queue is empty.
    exit /b 1
)
echo Next in line: !QUEUE_%head%!
exit /b

:size
:: Usage: call :size  (displays current queue length)
set /a "count=tail - head + 1"
if !count! LSS 0 set "count=0"
echo Queue Size: !count!
exit /b

Why Use a Queue in Batch?

  1. Job Schedulers: If your script finds 50 files that need processing, you can "Enqueue" their paths. The script then "Dequeues" them one-by-one, ensuring that every file is handled in the order it was discovered.
  2. Breadcrumb Traces: Recording the steps a script took in order, so you can output a "Timeline" of events for an audit log.
  3. Buffer Management: Temporarily storing user inputs or system events while you perform another task, ensuring no data is lost during the "Waiting" period.

Visualizing the Process

  1. Initial: [ ] (head=1, tail=0)
  2. Add "A": [ A ] (head=1, tail=1)
  3. Add "B": [ A, B ] (head=1, tail=2)
  4. Remove: Result "A", Queue [ B ] (head=2, tail=2)

Best Practices

  1. Empty Check: The queue is empty when head > tail. Always check this condition before attempting to dequeue.
  2. Memory Management: Unlike a real queue in languages like C++, Batch doesn't "Delete" the old variables automatically. The :dequeue function in this guide explicitly unsets each consumed variable. After high-volume processing, you can also clear the entire namespace by looping through remaining entries.
  3. Maximum Size: Batch variables are limited by system memory. While you can store thousands of items, very large queues can slow down the set command lookups.

Conclusion

Implementing a queue data structure transforms your scripts from simple command-lists into orderly "Task Managers". By mastering the FIFO principle and the dual-pointer (Head/Tail) methodology, you can ensure that your automation processes data fairly and predictably. This level of organizational logic is essential for building robust, professional-grade processing pipelines in any Windows environment.

Navigation