- 1 year ago
How to split string in batch-file
In a batch file, you can split a string into multiple parts using the for
loop and the delimiter character. Here's an example of how to split a string in a batch file:
batch@echo off
set string=Hello,World,How,Are,You
REM Enable delayed expansion
setlocal enabledelayedexpansion
REM Split the string using the comma (,) delimiter
for %%A in ("%string:,=" "%") do (
echo %%A
)
REM Disable delayed expansion
endlocal
In the example above, the string
variable holds the string you want to split. The string is "Hello,World,How,Are,You" in this example.
The for
loop iterates over the parts of the string, splitting it using the comma (,) as the delimiter. Inside the loop, %%A
represents each individual part of the split string.
Within the loop, you can perform any desired operations with the split parts. In this example, echo %%A
is used to display each part of the split string.
Note that the setlocal enabledelayedexpansion
command enables delayed expansion, which allows you to access and manipulate the variables within the loop.
After the loop, the endlocal
command is used to disable delayed expansion.
Running the batch file will split the string using the comma (,) delimiter and display each part separately.
Adjust the delimiter and the actions within the loop as needed for your specific requirements.