Align Right - Align text to the right i.e. to improve readability of number columns
Description:
Add leading spaces to a string to make sure the output lines up. I.e. for variables no longer than 8 characters add 8 spaces at the front and then show only the last 8 characters of the variable.
Script:
1. 2. 3. 4. 5. 6.
set x=3000 set y=2 set x= %x% set y= %y% echo.X=%x:~-8% echo.Y=%y:~-8%
Left String - Extract characters from the beginning of a string
Description:
Similar to the Left function in VB a batch script can return a specified number of characters from the left side of a string by specifying a substring for an expansion given a position of 0 and a length using :~ while expanding a variable content. The example shows how to return the first 4 characters of a string.
Script:
1. 2. 3. 4.
set str=politic echo.%str% set str=%str:~0,4% echo.%str%
Map and Lookup - Use Key-Value pair list to lookup and translate values
Description:
This example shows an approach to map a name of a month into it`s corresponding two digit number. The key-value pairs are listed in the map variable separated by semicolon. Key and value itself are separated by one dash character. Same can be used to tranlate a day-of-the-week short string into a day-of-the-week long string by changing the map content only.
Similar to the Mid function in VB a batch script can return a specified number of characters from any position inside a string by specifying a substring for an expansion given a position and length using :~ while expanding a variable content. The example here shows how to extract the parts of a date.
Remove - Remove a substring using string substitution
Description:
The string substitution feature can also be used to remove a substring from another string. The example shown here removes all occurrences of "the " from the string variable str.
Script:
1. 2. 3. 4.
set str=the cat in the hat echo.%str% set str=%str:the =% echo.%str%
Replace - Replace a substring using string substitution
Description:
To replace a substring with another string use the string substitution feature. The example shown here replaces all occurrences "teh" misspellings with "the" in the string variable str.
Script:
1. 2. 3. 4.
set str=teh cat in teh hat echo.%str% set str=%str:teh=the% echo.%str%
Right String - Extract characters from the end of a string
Description:
Similar to the Right function in VB a batch script can return a specified number of characters from the right side of a string by specifying a substring for an expansion given a negative position using :~ while expanding a variable content. The example shows how to return the last 4 characters of a string.
Script:
1. 2. 3. 4.
set str=politic echo.%str% set str=%str:~-4% echo.%str%
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:
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
Trim Right - Trim spaces from the end of a string via "FOR" command
Description:
Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use a FOR loop to trim up to 31 spaces from the end of a string. It assumes that Delayed Expansion is enabled.
Script:
1. 2. 3. 4.
set str=15 Trailing Spaces to truncate &rem echo."%str%" for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1! echo."%str%"
Script Output:
Script Output
"15 Trailing Spaces to truncate "
"15 Trailing Spaces to truncate"
Trim Right - Trim spaces from the end of a string via substitution
Description:
Trimming spaces at the end of a variable seems a little tricky. The following example shows how to use the string substitution feature to trim up to 31 spaces from the end of a string. It assumes that the string to be trimmed never contains two hash "##" characters in a row.
Script:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
set str=15 Trailing Spaces to truncate &rem echo."%str%" set str=%str%## set str=%str: ##=##% set str=%str: ##=##% set str=%str: ##=##% set str=%str: ##=##% set str=%str: ##=##% set str=%str:##=% echo."%str%"
Script Output:
Script Output
"15 Trailing Spaces to truncate "
"15 Trailing Spaces to truncate"