How to Split a String by a Delimiter in Batch Script
Parsing structured data is a core task in scripting. A common format for this data is a single string where values are separated by a specific character, known as a delimiter (e.g., apple,banana,cherry or 192.168.1.10). To use this data, you need to split the string into its individual components. While Windows Batch has no direct SPLIT() function, it has a powerful and versatile command, FOR /F, that is perfectly designed for this task.
This guide will teach you how to use the FOR /F command with its delims and tokens options to split any string. You will learn how to extract specific parts and how to loop through all the parts of a string with an unknown number of elements.
The Core Command: FOR /F
The FOR /F command is the primary text-parsing tool in batch scripting. It is designed to read and tokenize strings line by line. When we give it a single string to process, it treats it as a one-line file.
The key options for splitting are:
delims=...: This option specifies the delimiter character(s). For example,delims=,tells it to split the string wherever it sees a comma.tokens=...: This option specifies which of the resulting pieces (tokens) you want to capture.tokens=1,2,3: Captures the first three tokens. The first goes into the main loop variable (e.g.,%%A), the second into the next alphabetical variable (%%B), and the third into%%C.tokens=*: A special wildcard that captures all tokens into the first variable.tokens=2*: A combination that captures the second token into%%Aand all remaining tokens into%%B.
Basic Example: Splitting a Comma-Separated String
Let's parse a simple data string containing a user's ID, username, and role.
@ECHO OFF
SET "UserData=101,JohnDoe,Administrator"
ECHO Original String: "%UserData%"
ECHO.
REM --- Use FOR /F to split the string ---
FOR /F "tokens=1,2,3 delims=," %%A IN ("%UserData%") DO (
SET "UserID=%%A"
SET "Username=%%B"
SET "UserRole=%%C"
)
ECHO User ID: %UserID%
ECHO Username: %Username%
ECHO Role: %UserRole%
Output:
Original String: "101,JohnDoe,Administrator"
User ID: 101
Username: JohnDoe
Role: Administrator
Let's see how the the command FOR /F "tokens=1,2,3 delims=," %%A IN ("%UserData%") performs the split in the script above:
IN ("%UserData%"): It takes our string"101,JohnDoe,Administrator"as its input.delims=,: It uses the comma as its knife to slice the string. This results in three pieces (tokens):101,JohnDoe, andAdministrator.tokens=1,2,3: It specifies that we want to capture the first three tokens.%%A: TheFORcommand assigns the first token (101) to the main loop variable,%%A.- It then automatically assigns the subsequent tokens to the next letters of the alphabet:
%%BgetsJohnDoeand%%CgetsAdministrator.
Iterating Through All Parts of a String
What if you don't know how many parts your string will have? You can use a clever nested loop to process every token, one by one.
@ECHO OFF
SET "FruitList=apple banana cherry date"
ECHO --- Iterating through all fruits ---
REM The outer FOR /F with "tokens=*" captures the whole list.
FOR /F "tokens=*" %%L IN ("%FruitList%") DO (
REM The inner FOR loop iterates through each word in the list.
FOR %%F IN (%%L) DO (
ECHO Fruit: %%F
)
)
Output:
--- Iterating through all fruits ---
Fruit: apple
Fruit: banana
Fruit: cherry
Fruit: date
The default delimiter for FOR is a space, so we don't need delims= here.
Common Pitfalls and How to Solve Them
Problem: The String Contains Empty Fields
This is the most significant limitation of FOR /F. By default, it skips over empty tokens.
For example, if you try to parse "field1,,field3" with tokens=1,2,3 delims=,, %%A will get field1, but %%B will get field3 because the empty token between the two commas is ignored.
Solution: There is no simple, pure-batch solution for this. It requires very complex, multi-stage parsing. For any data that might contain empty fields, it is highly recommended to use PowerShell, which can handle this correctly.
powershell -Command "('field1,,field3' -split ',')[1]"
This PowerShell command would correctly identify that the second element is empty.
Problem: Handling Multiple Delimiters
You can specify more than one delimiter character in the delims= option. FOR /F will treat any character in the set as a delimiter.
Let's split a path-like string that uses both \ and /.
@ECHO OFF
SET "Path=C:/Users/Admin/Documents"
FOR /F "tokens=1,2,3,4 delims=/\ " %%A IN ("%Path%") DO (
ECHO Part 1: %%A, Part 2: %%B, Part 3: %%C, Part 4: %%D
)
Practical Example: Parsing a Version Number
This script takes a version string like 10.2.3-beta and splits it into its major, minor, and patch components.
@ECHO OFF
SET "Version=10.2.3-beta"
ECHO Full Version: %Version%
REM --- Split by the dot delimiter ---
FOR /F "tokens=1,2,3 delims=." %%A IN ("%Version%") DO (
SET "Major=%%A"
SET "Minor=%%B"
SET "PatchAndLabel=%%C"
)
REM --- The third token might still have a label, so split it by the hyphen ---
FOR /F "tokens=1 delims=-" %%P IN ("%PatchAndLabel%") DO (
SET "Patch=%%P"
)
ECHO Major: %Major%
ECHO Minor: %Minor%
ECHO Patch: %Patch%
Output:
Full Version: 10.2.3-beta
Major: 10
Minor: 2
Patch: 3
Conclusion
The FOR /F command is the indispensable "Swiss Army knife" of text parsing in Windows Batch. Its delims and tokens options give you complete control for splitting strings into their component parts.
- Use
delims=to specify the character(s) to split by. - Use
tokens=to select which of the resulting pieces you want to capture. - Remember that
FOR /Fcannot reliably handle empty tokens, and for such cases, PowerShell is the better tool.
By mastering this one command, you can handle a huge variety of data parsing and manipulation tasks directly in your batch scripts.