Split String

Split a String, Extract Substrings by Delimiters.

Description: Use the FOR command to split a string into parts. The example shows how to split a date variable into its parts.
Script:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
echo.-- Split off the first date token, i.e. day of the week
for /f %%a in ("%date%") do set d=%%a
echo.Date   : %date%
echo.d      : %d%
echo.

echo.-- Split the date into weekday, month, day, and year, using slash and space as delimiters
for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set wday=%%a&set month=%%b&set day=%%c&set year=%%d
echo.Weekday: %wday%
echo.Month  : %month%
echo.Day    : %day%
echo.Year   : %year%
Script Output:
 DOS Script Output
-- Split off the first date token, i.e. day of the week
Date   : Thu 12/02/2005
d      : Thu

-- Split the date into weekday, month, day, and year, using slash and space as delimiters
Weekday: Thu
Month  : 12
Day    : 02
Year   : 2005