Professional Documents
Culture Documents
Basic string manipulation in batch like you are used to from other programming languages.
Align Right Align text to the right i.e. to improve readability of number columns.
Left String Extract characters from the beginning of a string.
for Windows10,Windows8,Windows7,VISTA,
Map and Lookup Use Key-Value pair list to lookup and translate values. XP,NT,Server 2000,Server 2003,Server 2008
Mid String Extract a Substring by Position.
Remove Remove a substring using string substitution.
Remove both Ends Remove the first and the last character of a string.
Remove Spaces Remove all spaces in a string via substitution.
Replace Replace a substring using string substitution. Home
Right String Extract characters from the end of a string.
Split String Split a String, Extract Substrings by Delimiters. Source Script
String Concatenation Add one string to another string. Batch Files
Functions
Trim Left Trim spaces from the beginning of a string via "FOR" command. Function Library
Trim Quotes Remove surrounding quotes via FOR command. Script Snippets
Trim Right Interfacing
Trim spaces from the end of a string via "FOR" command.
Trim Right Trim spaces from the end of a string via substitution.
Tips and Tricks
Copy Tips
TOP Menu in Batch
Align Right - Align text to the right i.e. to improve 2008-01-01
String Manipulation
readability of number columns String Operations
Date and Time
Description: Add leading spaces to a string to make sure the output lines up. I.e. for Arithmetic
variables no longer than 8 characters add 8 spaces at the front and then show FTP Batch Script
only the last 8 characters of the variable. Command Index
Script: 1. set x=3000
2. set y=2 Tutorials
3. set x= %x% My First Batch
Solid Framework
4. set y= %y% Functions
5. echo.X=%x:~-8% Persistency
6. echo.Y=%y:~-8%
Script
Forum
Output: Script Output
X= 3000 General
Y= 2 Terms of Use
Enablers
About Us
TOP Contact Us
Left String - Extract characters from the beginning of a 2008-01-01
string Others
Wa
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. set str=politic
2. echo.%str%
3. set str=%str:~0,4%
4. echo.%str%
Script
Output: Script Output
politic
poli
TOP
Map and Lookup - Use Key-Value pair list to lookup and translate values 2008-01-01
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.
Script: 1. REM ---- Example 1: Translate name of month into two digit number ----
2. SET v=Mai
3.
4. SET map=Jan-01;Feb-02;Mar-03;Apr-04;Mai-05;Jun-06;Jul-07;Aug-08;Sep-09;Oct-10;Nov-11;Dec-12
5. CALL SET v=%%map:*%v%-=%%
6. SET v=%v:;=&rem.%
7.
8. ECHO.%v%
9.
10.
11. REM ---- Example 2: Translate abbreviation into full string ----
12. SET v=sun
13.
14. set map=mon-Monday;tue-Tuesday;wed-Wednesday;thu-Thursday;fri-Friday;sat-Saturday;sun-Sunday
15. CALL SET v=%%map:*%v%-=%%
16. SET v=%v:;=&rem.%
17.
18. ECHO.%v%
Script
Output: Script Output
05
Sunday
Mid String - Extract a Substring by Position TOP
2008-01-01
Description: 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.
Script: 1. echo.Date : %date%
2. echo.Weekday: %date:~0,3%
3. echo.Month : %date:~4,2%
4. echo.Day : %date:~7,2%
5. echo.Year : %date:~10,4%
Script
Output: Script Output
Date : Sat 03/11/2006
Weekday: Sat
Month : 03
Day : 11
Year : 2006
TOP
Remove - Remove a substring using string substitution 2008-01-01
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. set str=the cat in the hat
2. echo.%str%
3. set str=%str:the =%
4. echo.%str%
Script
Output: Script Output
the cat in the hat
cat in hat
TOP
Remove both Ends - Remove the first and the last character of a string 2008-01-01
Description: Using :~1,-1 within a variable expansion will remove the first and last character of the string.
Script: 1. set str=politic
2. echo.%str%
3. set str=%str:~1,-1%
4. echo.%str%
Script Output:
Script Output
politic
oliti
TOP
Remove Spaces - Remove all spaces in a string via substitution 2008-01-01
Description: This script snippet can be used to remove all spaces from a string.
Script: 1. set str= word &rem
2. echo."%str%"
3. set str=%str: =%
4. echo."%str%"
Script Output:
Script Output
" word "
"word"
TOP
Replace - Replace a substring using string substitution 2008-01-01
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. set str=teh cat in teh hat
2. echo.%str%
3. set str=%str:teh=the%
4. echo.%str%
Script
Output: Script Output
teh cat in teh hat
the cat in the hat
TOP
Right String - Extract characters from the end of a string 2008-01-01
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. set str=politic
2. echo.%str%
3. set str=%str:~-4%
4. echo.%str%
Script
Output: Script Output
politic
itic
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
TOP
String Concatenation - Add one string to another string 2008-02-26
Script Output:
Script Output
HelloWorld
Hello World
Hello DOS World
TOP
Trim Left - Trim spaces from the beginning of a string via "FOR" command 2008-04-28
Description: Use the FOR command to trim spaces at the beginning of a variable. In this example the variable to be trimmed is str.
Script: 1. set str= 15 Leading spaces to truncate
2. echo."%str%"
3. for /f "tokens=* delims= " %%a in ("%str%") do set str=%%a
4. echo."%str%"
Script Output:
Script Output
" 15 Leading spaces to truncate"
"15 Leading spaces to truncate"
TOP
Trim Quotes - Remove surrounding quotes via FOR command 2008-01-01
Description: The FOR command can be used to safely remove quotes surrounding a string. If the string does not have quotes then it will
remain unchanged.
Script: 1. set str="cmd politic"
2. echo.%str%
3. for /f "useback tokens=*" %%a in ('%str%') do set str=%%~a
4. echo.%str%
Script
Output: Script Output
"cmd politic"
cmd politic
TOP
Trim Right - Trim spaces from the end of a string via "FOR" command 2008-01-01
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. set str=15 Trailing Spaces to truncate &rem
2. echo."%str%"
3. for /l %%a in (1,1,31) do if "!str:~-1!"==" " set str=!str:~0,-1!
4. 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 TOP
2008-01-01
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. set str=15 Trailing Spaces to truncate &rem
2. echo."%str%"
3. set str=%str%##
4. set str=%str: ##=##%
5. set str=%str: ##=##%
6. set str=%str: ##=##%
7. set str=%str: ##=##%
8. set str=%str: ##=##%
9. set str=%str:##=%
10. echo."%str%"
Script
Output: Script Output
"15 Trailing Spaces to truncate "
"15 Trailing Spaces to truncate"
Ad:
DosTips.com
A Forum all about DOS Batch Search…
Announcements Replies Views Last post