Skip to main content

How to Append Text to Every Line in a File in Batch Script

Appending text (adding a suffix) to every line is a standard data-processing task. You might want to add a file extension to a list of names, add a closing bracket, or append a specific status (like ,PENDING) to every row in a database export. This allows you to prepare a list for its next destination without manually editing every entry.

In this guide, we will demonstrate how to append text using a for /f loop.

Method 1: The Standard Loop (FOR /F)

This method reads each line and outputs it with the desired suffix added to the end.

@echo off
setlocal disabledelayedexpansion

set "Source=UserList.txt"
set "Dest=Processed_UserList.txt"
set "Suffix= - [VERIFIED]"

:: Verify source file exists
if not exist "%Source%" (
echo [ERROR] Source file "%Source%" not found.
pause
exit /b 1
)

echo Appending "%Suffix%" to every line in "%Source%"...

(
for /f "usebackq delims=" %%A in ("%Source%") do (
set "line=%%A"
setlocal enabledelayedexpansion
echo(!line!!Suffix!
endlocal
)
) > "%Dest%"

echo [SUCCESS] Suffixed file saved to "%Dest%".
pause
exit /b 0
warning

The for /f loop skips blank lines by design and also skips lines beginning with ; (the default eol character). Blank lines in the original file will be removed from the output, and lines starting with ; will be silently dropped. For files where every line must be preserved, use the PowerShell method (Method 2).

tip

The script uses the delayed expansion toggle pattern: each line is set with delayed expansion disabled (set "line=%%A") to preserve literal ! characters in the file content. The line and suffix are then combined with delayed expansion enabled (echo(!line!!Suffix!) to safely handle &, |, >, <, and other special characters in both the content and the suffix. The echo( syntax also prevents ECHO is off. messages for empty values.

If your file content or your suffix contains special characters, or if blank lines must be preserved, PowerShell handles the append operation more reliably.

@echo off
setlocal

set "Source=data.txt"
set "Dest=appended_data.txt"
set "Suffix=;STATUS_OK"

:: Verify source file exists
if not exist "%Source%" (
echo [ERROR] Source file "%Source%" not found.
pause
exit /b 1
)

echo Appending "%Suffix%" to every line in "%Source%"...

:: ForEach-Object concatenates each line with the suffix
:: Blank lines receive the suffix, preserving line count
powershell -NoProfile -Command ^
"$suffix = '%Suffix%'; " ^
"Get-Content -Path '%Source%' | " ^
"ForEach-Object { $_ + $suffix } | " ^
"Set-Content -Path '%Dest%' -Encoding UTF8"

if %errorlevel% equ 0 (
echo [SUCCESS] Suffixed file saved to "%Dest%".
) else (
echo [ERROR] Append operation failed.
pause
exit /b 1
)
pause
exit /b 0
info

The PowerShell method preserves blank lines and handles all special characters without escaping. Blank lines in the source will appear as lines containing only the suffix text in the output. If you want blank lines to remain truly blank (no suffix), add a condition: ForEach-Object { if ($_.Length -gt 0) { $_ + $suffix } else { $_ } }.

Why Append Text?

  1. Format Conversion: If you have a list of raw filenames and need to turn them into a CSV, you can append a comma to every line.
  2. Tagging: Adding a status or a timestamp to the end of a line in a log report.
  3. Command Building: Adding a closing quote or a redirection symbol (e.g., > nul) to every line of a command manifest.

Best Practices

  1. Verify Source File: Always check that the input file exists before processing. A missing file will cause the for /f loop to silently produce empty output, and the redirection will create an empty destination file.
  2. Blank Lines: The Batch for /f method drops blank lines. If your file uses blank lines as section separators, those separators will disappear from the output. Use Method 2 to preserve them.
  3. Special Characters in Suffix: If the suffix contains special Batch characters (&, |, <, >, ^), the delayed expansion pattern in Method 1 handles them safely through !Suffix!. Never use echo %%A%Suffix% with percent expansion, as special characters in either the line content or the suffix will be interpreted as command operators.
  4. Special Characters in Content: Lines containing &, |, >, <, or ! require the delayed expansion toggle pattern. The original echo %%A%Suffix% approach would break on any of these characters.
  5. Trailing Spaces in Suffix: If your suffix starts with a space (e.g., DONE), ensure it is properly enclosed in the set command's quotes (set "Suffix= DONE"). The quotes in the set "var=value" syntax protect leading and trailing spaces from being trimmed.
  6. Performance: For files larger than 10 MB, the Batch loop can be noticeably slow. The PowerShell method is optimized for streaming large text files efficiently.
  7. Encoding: Include -Encoding UTF8 in the PowerShell Set-Content command to prevent non-ASCII characters from being corrupted by the default ANSI encoding in PowerShell 5.1.

Conclusion

Appending text to every line is a versatile transformation that bridges the gap between raw data and formatted output.

Whether you are generating a deployment manifest or finalizing a CSV export, the ability to programmatically add suffixes to your datasets ensures that your files are always perfectly tailored for their specific role.

By mastering the for /f loop and PowerShell bridges, you turn bulk text editing from a manual chore into a reliable, automated process.