Skip to main content

How to Create Multi-Page Output with Pagination in Batch Script

When a Batch script generates a large amount of information, such as a long list of log files, hundreds of active processes, or a massive database dump, the text often scrolls past the top of the terminal buffer before the user can read it. To make these scripts user-friendly, you need to implement Pagination (showing a specific number of items per "page" and waiting for a keypress to continue).

In this guide, we will demonstrate how to use a counter variable to pause output every N lines, creating a classic "Press any key for the next page" experience.

Method 1: The Native MORE Command (Simple)

Windows has a built-in command precisely for this purpose. The more command displays text one screenful at a time.

Implementation

If you have a large text file, you can simply pipe it into more:

type "VeryLongLogFile.txt" | more

Constraints:

The more command is based on the window size, not a specific number of items. If the user resizes their terminal, the "page" length changes. For developers who want exact control (e.g., exactly 10 items per page), we need a custom loop.

Method 2: Custom Loop Pagination

To control precisely how many items appear on each page, we use a FOR loop combined with a counter and the SET /A (Arithmetic) command.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: Configuration
set "pageSize=10"
set "lineCount=0"

echo LISTING SYSTEM FILES (Paginated: %pageSize% per page^)
echo --------------------------------------------------

:: Process a list of files as our example data
for %%F in (C:\Windows\System32\*.dll) do (
set /a "lineCount+=1"

:: Output the item
echo !lineCount!. %%~nxF

:: Check if we have reached the end of the page
set /a "remainder=lineCount %% pageSize"
if !remainder! equ 0 (
echo.
echo --- [Page Complete: Press any key for next !pageSize! items] ---
pause >nul
echo.
)
)

echo.
echo End of list. Total: !lineCount! items.

endlocal
pause

Key Logic:

  1. set /a "lineCount+=1": Increments the total count for every item found.
  2. lineCount %% pageSize: This uses the Modulo operator. It checks the remainder of the division. If lineCount is 10, 20, or 30, the remainder is 0. Inside set /a, variable names can be used directly without % or ! delimiters.
  3. pause >nul: Stops the script and waits for a keypress without displaying the default "Press any key to continue..." message, allowing us to use a custom message.

Method 3: Pagination with "Quit" Option

Sometimes a user doesn't want to see all 500 items. You can add a prompt to either see the next page or exit the loop entirely.

@echo off
setlocal enabledelayedexpansion

set "counter=0"
set "pageSize=5"

echo PROCESS AUDIT (!pageSize! per page)
echo --------------------------------------------------

for /F "tokens=1 delims=," %%P in ('tasklist /NH /FO CSV 2^>nul') do (
set "proc=%%~P"
if defined proc (
set /a "counter+=1"
echo !counter!. !proc!

set /a "mod=counter %% pageSize"
if !mod! equ 0 (
echo.
set "choice="
set /p "choice=Next page? (Y/N): "
if /i "!choice!"=="N" goto :end_audit
)
)
)

:end_audit
echo.
echo Audit finished. Displayed !counter! items.

endlocal
pause
info

Note on user input: If the user presses Enter without typing anything, set /p leaves the variable unchanged (empty, since we cleared it with set "choice="). The script treats any input other than N, including blank input, as "continue." This is intentional so that pressing Enter quickly advances to the next page.

Comparisons: more vs. Custom Loop

  • more Command: Best for raw text files or streaming output where you don't care about the exact count. It handles window resizing automatically.
  • Custom Loop: Best for formatted data, dashboards, or when you need an interactive "Quit" option. It gives you total control over the UI of the pagination.

Best Practices

  1. Clear Screen (Optional): If you want each page to feel like a "new screen," add cls inside the if block before starting the next page.
  2. Breadcrumbs: Always show the current item count (e.g., Items 10-20 of 150) so the user knows how far into the data they are.
  3. Handle Small Datasets: If the total items are less than your pageSize, the if block will never trigger. Ensure your "End of list" message is always present so the user isn't confused.

Conclusion

Pagination is the difference between a "dump of data" and a functional administration tool. By using a simple modulo check in your loops, you empower your users to digest large datasets at their own pace, preventing information overload and making your Batch scripts feel much more robust and professional.