Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
244 views

DOS - String Manipulation

The document discusses various string manipulation techniques in DOS batch scripts, including aligning text right, extracting parts of a string, removing substrings, mapping keys to values, and more. Specific examples show how to extract the first or last few characters of a string, remove spaces or other substrings, and look up values from a semicolon-delimited map of key-value pairs.

Uploaded by

MITSom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

DOS - String Manipulation

The document discusses various string manipulation techniques in DOS batch scripts, including aligning text right, extracting parts of a string, removing substrings, mapping keys to values, and more. Specific examples show how to extract the first or last few characters of a string, remove spaces or other substrings, and look up values from a semicolon-delimited map of key-value pairs.

Uploaded by

MITSom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

6/26/2020 DOS - String Manipulation

DOS - String Manipulation


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. for Windows10,Windows8,Windows7,VISTA,
Left String Extract characters from the beginning of a string. XP,NT,Server 2000,Server 2003,Server 2008
Map and Lookup Use Key-Value pair list to lookup and translate values.
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.
Home
Replace Replace a substring using string substitution.
Source Script
Right String Extract characters from the end of a string. Batch Files
Split String Split a String, Extract Substrings by Delimiters. Functions
String Function Library
Concatenation Add one string to another string. Script Snippets
Interfacing
Trim Left Trim spaces from the beginning of a string via "FOR"
command.
Trim Quotes Remove surrounding quotes via FOR command. Tips and Tricks
Copy Tips
Trim Right Trim spaces from the end of a string via "FOR" command. Menu in Batch
Trim Right Trim spaces from the end of a string via substitution. String Manipulation
String Operations
TOP Date and Time
Align Right - Align text to the right i.e. to improve 2008-01-01 Arithmetic
readability of number columns FTP Batch Script
Command Index
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 Tutorials
and then show only the last 8 characters of the variable. My First Batch
Script: 1. set x=3000 Solid Framework
Functions
2. set y=2 Persistency
3. set x= %x%
4. set y= %y% Forum
5. echo.X=%x:~-8%
6. echo.Y=%y:~-8% General
Terms of Use
Script Enablers
Output:
Script Output
About Us
X= 3000 Contact Us
Y= 2
Others
TOP Wa
Left String - Extract characters from the beginning 2008-01-01
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. 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.

https://www.dostips.com/DtTipsStringManipulation.php 1/8
6/26/2020 DOS - String Manipulation
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

TOP
Mid String - Extract a Substring by Position 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
Script Output
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
Script Output
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

https://www.dostips.com/DtTipsStringManipulation.php 2/8
6/26/2020 DOS - String Manipulation
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

TOP
Split String - Split a String, Extract Substrings by Delimiters 2008-01-01

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. echo.-- Split off the first date token, i.e. day of the week
2. for /f %%a in ("%date%") do set d=%%a
3. echo.Date : %date%
4. echo.d : %d%
5. echo.
6.
7. echo.-- Split the date into weekday, month, day, and year, using slash and space as delimiters
8. for /f "tokens=1,2,3,4 delims=/ " %%a in ("%date%") do set wday=%%a&set month=%%b&set day=%%c&set year=%%d
9. echo.Weekday: %wday%
10. echo.Month : %month%
11. echo.Day : %day%
12. 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

TOP
String Concatenation - Add one string to another string 2008-02-26

https://www.dostips.com/DtTipsStringManipulation.php 3/8
6/26/2020 DOS - String Manipulation
Description: This example shows how to add two strings in DOS.
Script: 1. set "str1=Hello"
2. set "str2=World"
3.
4. set "str3=%str1%%str2%"
5. set "str4=%str1% %str2%"
6. set "str1=%str1% DOS %str2%"
7.
8. echo.%str3%
9. echo.%str4%
10. echo.%str1%

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"

TOP
Trim Right - Trim spaces from the end of a string via substitution 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%##

https://www.dostips.com/DtTipsStringManipulation.php 4/8
6/26/2020 DOS - String Manipulation
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"

https://www.dostips.com/DtTipsStringManipulation.php 5/8
6/26/2020 DOS - String Manipulation

DosTips.com
A Forum all about DOS Batch Search…  

 Quick links  FAQ  Register  Login


 DosTips.com ‹ Board index ‹ DosTips - Dos Batch ‹ DOS Batch Forum

Unanswered topics Active topics

DOS Batch Forum


Moderator: DosItHelp

New Topic  Search this forum…   8006 topics 1 2 3 4 5 … 161 


Announcements Replies Views Last post

Survey Popup - RESULTS by Samir 


4 19752
by admin » 22 Aug 2017 22:26 22 May 2020 17:41

Topics Replies Views Last post

How to get help for a batch script - by aGerman 


quickly! 2 28057
17 Sep 2016 05:48
by foxidrive » 08 Dec 2014 05:51
Intentional Spam by Emiliano Fierro 
5 33
by Emiliano Fierro » 25 Jun 2020 25 Jun 2020 20:28
17:54
VT code Batch Paint by T3RRY 
7 160
by T3RRY » 20 Jun 2020 10:02 25 Jun 2020 15:08

caret removes the first command by lockedscope 


after opening parenthesis if its 4 106
25 Jun 2020 14:34
concat'd to the first token of
command without space
by lockedscope » 21 Jun 2020 06:32
JREPL - Combine Data From Two by houstontech 
0 8
Files By Matching Strings 25 Jun 2020 10:48
by houstontech » 25 Jun 2020 10:48
JREPL.BAT v8.5 - regex text by Ridim 
461 340909
processor with support for text 24 Jun 2020 07:24
highlighting and alternate
character sets
 by dbenham » 14 Nov 2014
14:52
 1 … 27 28 29 30 31

Expression Lab - interactive by misol101 


9 201
expression display 23 Jun 2020 09:18
 by misol101 » 20 Jun 2020 09:54
Pass multiple lines(through stdin) by jeb 
4 99
from batch to powershell 22 Jun 2020 00:38
by lockedscope » 18 Jun 2020 11:30
How batch parsing phases work? by jeb 
4 187
by lockedscope » 15 Jun 2020 07:37 22 Jun 2020 00:30

Repetitively read x lines from txt by pieh-ejdsch 


1 97
file and pass to called program 21 Jun 2020 12:21
by Bryan » 17 Jun 2020 11:39
How escape a pipe ? by einstein1969 
3 121
by einstein1969 » 18 Jun 2020 15:34 19 Jun 2020 13:29

Batch MasterMind by T3RRY 


12 4004
by dbenham » 31 Dec 2014 20:37 19 Jun 2020 11:55

JREPL to check 12 columns for a by SIMMS7400 


2 116
https://www.dostips.com/DtTipsStringManipulation.php 6/8
6/26/2020 DOS - String Manipulation
2 116
certain string? 19 Jun 2020 04:57
by SIMMS7400 » 17 Jun 2020 04:18
Is it possible to create a variable by T3RRY 
3 79
that will change a data path in a 18 Jun 2020 13:17
command?
by the_importer » 18 Jun 2020 09:15
Renaming multiple folders in a by the_importer 
0 56
command prompt, is it possible? 18 Jun 2020 12:57
by the_importer » 18 Jun 2020 12:57
Listc - a modern Windows file by Bryan 
17 8480
explorer/viewer in the console 18 Jun 2020 02:36
window
by misol101 » 02 Sep 2017 05:18
 1 2

jscript issues with downloading a by penpen 


5 340
zip from the Web 17 Jun 2020 12:01
by SIMMS7400 » 04 Jun 2020 04:41
getting a forfiles result that shows by Norma 
file locations 2 125
16 Jun 2020 06:23
by Norma » 15 Jun 2020 19:12
How delayed expansion works? by Aacini 
3 227
by lockedscope » 13 Jun 2020 06:05 15 Jun 2020 16:33

CmdBkg - use bitmap as by misol101 


background to console window 45 21114
15 Jun 2020 08:06
by misol101 » 12 Sep 2016 12:56
 1 2 3 4

Dynamic loop code over multiple by lockedscope 


7 308
lines causes extra quotes 15 Jun 2020 07:47
by lockedscope » 12 Jun 2020 13:13
Executing dynamic code blocks by penpen 
1 127
by lockedscope » 12 Jun 2020 10:47 15 Jun 2020 03:37

Batch delete first 67 character all by penpen 


text file in folder 2 208
15 Jun 2020 02:37
by senonolo » 09 Jun 2020 03:27
Recursively Delete File Types From by pieh-ejdsch 
A Named Folder Only 2 196
14 Jun 2020 05:07
 by pmc181 » 10 Jun 2020 14:20
Is there a way to split a string to by brentonv 
new lines in file by delimiter 4 290
13 Jun 2020 06:06
by brentonv » 11 Jun 2020 00:26
Batch hex edit offset from by Aacini 
8 362
filename 12 Jun 2020 19:14
by Samson92 » 10 Jun 2020 08:45
Atan? by misol101 
5 309
by misol101 » 09 Jun 2020 12:23 12 Jun 2020 01:40

Cmdwiz - 52 operation cmd helper by misol101 


39 22034
tool (now with Unicode) 10 Jun 2020 14:13
 by misol101 » 10 Sep 2016 19:46
 1 2 3

Read arrow keys and show color by VooDooGuito 


16 13254
text in an efficient way 10 Jun 2020 12:56
 by Aacini » 30 Jan 2016 20:56
 1 2

Find will NOT find EXACT input! by Squashman 


3 237
by PAB » 09 Jun 2020 13:25 09 Jun 2020 15:20

Cmdgfx - draw 3d and graphic by Aacini 


primitives (polygons,circles etc) in 119 72460
09 Jun 2020 14:57
cmd window (now with 24-bit RGB
support!)
https://www.dostips.com/DtTipsStringManipulation.php 7/8
6/26/2020 DOS - String Manipulation

by misol101 » 15 May 2016 15:19


 1 … 4 5 6 7 8

Change position of environment by T3RRY 


variables output! 3 246
09 Jun 2020 12:50
by PAB » 09 Jun 2020 04:53
Ouput Multiple Lines and Input into by mojobadshah 
0 187
Duplicate Path 08 Jun 2020 23:05
by mojobadshah » 08 Jun 2020 23:05
Game Of Life by T3RRY 
19 866
by T3RRY » 02 Jun 2020 13:37 08 Jun 2020 08:19
 1 2

Risk Remover Batch Antivirus ( by kornkaobat 


0 188
Scanner Component ) 08 Jun 2020 07:47
by kornkaobat » 08 Jun 2020 07:47
Batch substring code not working by kornkaobat 
6 425
by kornkaobat » 04 Jun 2020 03:55 08 Jun 2020 00:28

Extract UNIQUE rows ONLY from by PAB 


.txt file. 3 305
07 Jun 2020 15:02
by PAB » 07 Jun 2020 10:08
Redirect output to file without by Shohreh 
using > sign? 10 430
07 Jun 2020 06:52
by Shohreh » 06 Jun 2020 07:56
Queue Script by penpen 
1 226
by casper2201 » 05 Jun 2020 06:11 06 Jun 2020 13:18

Batch wont display anything on my by npocmaka_ 


5 313
new PC??? 05 Jun 2020 12:26
by IcarusLives » 05 Jun 2020 08:25
for loop -> Pipe - not working by SIMMS7400 
2 277
by SIMMS7400 » 03 Jun 2020 09:31 04 Jun 2020 04:28

Telnet Scripting Tool v.1.0 by by penpen 


Albert Yale 11 611
03 Jun 2020 14:56

https://www.dostips.com/DtTipsStringManipulation.php 8/8

You might also like