How to Store Command Output in a Variable in Batch Script
A fundamental task in any scripting language is to run a command, capture its output, and store that output in a variable for later use. You might need to get the current username, read a specific line from a file, or capture the status of a system utility. In batch scripting, there is no direct assignment operator for this; you cannot simply write SET MyVar = command.
Instead, the standard and most powerful method for capturing command output is to use the FOR /F command. This guide will teach you how to use FOR /F to capture single-line and multi-line output, and how to handle the common pitfalls you'll encounter along the way.
The Challenge: No Direct Output-to-Variable Assignment
Many scripting languages allow for a simple syntax like my_var = $(whoami). Batch scripting does not have this. An attempt to do this with redirection will fail: SET MyVar=<whoami.exe does not work. The command processor requires a specific structure to loop over the output that a command produces.
The Core Method: The FOR /F Command
The FOR /F loop is the designated workhorse for parsing text, whether from a file, a string, or, in this case, the output of a command.
The Syntax: FOR /F "options" %%V IN ('command-to-execute') DO SET "MyVariable=%%V"
FOR /F: Initiates the loop designed for parsing."options": Optional parsing parameters, like"delims="to capture the entire line.%%V: The loop variable that will hold the captured output.('command-to-execute'): This is the crucial part. The command you want to run must be enclosed in single quotes.DO SET "MyVariable=%%V": This is the action to perform. It takes the value captured in%%Vand assigns it to a standard variable.
Basic Example: Capturing a Single Line of Output
Let's start with a simple command, whoami, which prints the current user's name on a single line.
@ECHO OFF
SET "CurrentUser="
ECHO Capturing the output of the 'whoami' command...
FOR /F "delims=" %%A IN ('whoami') DO (
SET "CurrentUser=%%A"
)
ECHO.
ECHO The current user is: "%CurrentUser%"
The delims= option tells FOR /F not to split the line by spaces, ensuring the entire output line is captured.
Output:
Capturing the output of the 'whoami' command...
The current user is: "my-pc\adminuser"
How the FOR /F Method Works
When the command processor encounters this FOR /F loop, it:
- Executes the command inside the single quotes (
whoami) in a new, hidden command prompt. - Captures the text output that the command produces.
- The
FOR /Floop then begins to iterate through that captured output, line by line. - In our example,
whoamionly produces one line, so the loop runs exactly once. - The
DOblock is executed, where theSETcommand assigns the content of that single line to theCurrentUservariable.
Handling Multi-Line Output
This is a critical concept. By default, if a command produces multiple lines of output, the FOR /F loop will iterate multiple times, and the SET command will overwrite the variable on each iteration. This means your variable will only hold the value of the last line of output.
The "Last Line Wins" Behavior
@ECHO OFF
SET "LastFile="
FOR /F "delims=" %%F IN ('DIR /B *.txt') DO (
SET "LastFile=%%F"
)
ECHO The last text file found was: %LastFile%
The Solution for Capturing All Lines
To concatenate all lines of output into a single variable, you must use Delayed Expansion.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "FileList="
FOR /F "delims=" %%F IN ('DIR /B *.txt') DO (
SET "FileList=!FileList! %%F"
)
ECHO The complete list of files is:%FileList%
How it works: !FileList! accesses the current value of the variable inside the loop, allowing you to build the string across multiple iterations.