You are on page 1of 7

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.
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

Split String - Split a String, Extract Substrings by Delimiters


TOP
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

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"
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…  

 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…   8414 topics 1 2 3 4 5 … 169 


Announcements Replies Views Last post

Survey Popup - RESULTS by ispy 


5 24166
by admin » 22 Aug 2017 22:26 20 Feb 2021 14:18

Topics Replies Views Last post

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


2 32763
by foxidrive » 08 Dec 2014 05:51 17 Sep 2016 05:48

Output the results of two commands in by Hackoo 


4 88
parallel 10 Oct 2021 08:41
by Blitz » 09 Oct 2021 16:15
set time date setting automatically every by aGerman 
1 32
day by running batch file 10 Oct 2021 04:31
by prasadk » 10 Oct 2021 01:19
JREPL.BAT v8.6 - regex text processor with by IanC 
support for text highlighting and alternate 497 508944 08 Oct 2021 10:08
character sets
 by dbenham » 14 Nov 2014 14:52
 1 … 30 31 32 33 34

Looping sqlcmd until condition is met by aGerman 


1 88
by exppost » 05 Oct 2021 15:01 06 Oct 2021 12:19

Sum of two decimal values by sark 


5 101
by sark » 05 Oct 2021 09:26 05 Oct 2021 15:37

Testing cmd.exe in Windows 11? by aGerman 


8 559
by atfon » 13 Sep 2021 11:51 05 Oct 2021 15:22

Funny Windows 11 Non-Glitch by aGerman 


7 176
by GeoffVass » 04 Oct 2021 17:39 05 Oct 2021 13:28

How to read a file in UTF-8 and remove a by aGerman 


2 127
BOM in batch script? 05 Oct 2021 01:54
by PiotrMP006 » 04 Oct 2021 07:11
user name by aGerman 
11 187 04 Oct 2021 10:41
by macoga » 03 Oct 2021 09:39

Remove empty folders log by drgt 


4 188 03 Oct 2021 10:30
by drgt » 02 Oct 2021 04:32

Using BAT to copy directories. by aGerman 


1 83 03 Oct 2021 08:42
by crazygamer24151 » 02 Oct 2021 17:15

How to get READONLY Status for Current by Eureka! 


disk drive (usb) - like diskpart/attr disk 15 464 02 Oct 2021 18:16
by GInfo » 28 Sep 2021 17:38  1 2

wmic os get BuildNumber by Jbcom41 


3 179
by Jbcom41 » 01 Oct 2021 08:08 02 Oct 2021 03:49

Efficient Code by Jbcom41 


7 378
by Jbcom41 » 26 Sep 2021 11:28 01 Oct 2021 03:54

display time in 12 hr by atfon 


25 12326
by kumar_kondapalli » 14 Jul 2011 01:40 29 Sep 2021 06:03
 1 2

split string into substrings based on by Aacini 


38 46125
delimiter 27 Sep 2021 21:47
by Sponge Belly » 02 May 2015 09:17
 1 2 3

Strange behavior of SET /P by OJBakker 


2 199 26 Sep 2021 05:38
by Alogon » 25 Sep 2021 23:01

VT.EXE - Print to a Virtual Terminal by aGerman 


emulator (like the Win 10 console or 0 291 25 Sep 2021 14:56
Windows Terminal), read response
sequences.
 by aGerman » 25 Sep 2021 14:56
Compare two lists of elements, remove by atfon 
6 362
same and recycle for next run 25 Sep 2021 08:29
by darioit » 22 Sep 2021 12:23
Best way to pull out specifc lines from text by ShadowThief 
file? JREPL? 6 351 24 Sep 2021 06:25
by SIMMS7400 » 23 Sep 2021 03:47
Array construction with empty value by SIMMS7400 
9 468
by SIMMS7400 » 18 Sep 2021 08:04 24 Sep 2021 04:12

Efficient Array Management Operations in by IcarusLives 


3 284
Batch files 23 Sep 2021 16:05
by Aacini » 22 Sep 2021 22:08
Terminal Escape Sequence and next line by atfon 
2 265
position 22 Sep 2021 13:54
by atfon » 22 Sep 2021 13:21
run command in a loop by aGerman 
1 245
by dosW10 » 22 Sep 2021 01:02 22 Sep 2021 02:16

Can we capture the response of a terminal? by T3RRY 


3 324
by aGerman » 21 Sep 2021 10:31 21 Sep 2021 11:34

Drag and Drop batch file correction by penpen 


1 259
by bloodstar » 17 Sep 2021 05:57 18 Sep 2021 18:58

Question with Array setup by SIMMS7400 


12 657
by SIMMS7400 » 12 Sep 2021 16:02 18 Sep 2021 07:57

Enabling Special Characters in Windows 10 by atfon 


18 1691
by atfon » 28 Jul 2021 11:40  1 2 17 Sep 2021 14:45

[How-To] Get or set the console font size by aGerman 


and font name (PowerShell hybrid) 3 805 17 Sep 2021 14:03
by aGerman » 31 Jul 2021 17:22
Differences between Windows Terminal and by aGerman 
5 448
the console host 16 Sep 2021 09:54
by atfon » 15 Sep 2021 11:26
Hopefully a straightforward one? by Kaarthuul 
6 418
 by Kaarthuul » 14 Sep 2021 02:09 15 Sep 2021 02:18

Scratching my head with this Function by SIMMS7400 


3 370
behavoir.... 12 Sep 2021 14:44
by SIMMS7400 » 11 Sep 2021 08:04
Markdown to HTML converter by siberia-man 
1 313 12 Sep 2021 00:40
by siberia-man » 11 Sep 2021 15:37

Function arg validation and return structure by T3RRY 


0 339 07 Sep 2021 15:12
by T3RRY » 07 Sep 2021 15:12

How to return ascii value in 'bg _Kbd' by Aacini 


9 648
by back_slash » 04 Sep 2021 17:47 07 Sep 2021 02:03

proof of concept: alternate asynchronous by T3RRY 


7 4069
non-blocking input 06 Sep 2021 06:24
by nephi12 » 26 Jan 2017 13:00
How to make a custom mouse cursor in by back_slash 
16 1487
batch? 04 Sep 2021 17:49
 by back_slash » 20 Aug 2021 08:12
 1 2

How to reset a batch variable in Powershell? by T3RRY 


3 469
by SIMMS7400 » 31 Aug 2021 07:34 03 Sep 2021 11:31

Using "HTA input forms" in Batch files by Aacini 


18 32033
by Aacini » 29 Jul 2015 20:54  1 2
03 Sep 2021 11:09

js/vbs/html/hta and more hybrids and by siberia-man 


62 179926
chimeras in cmd/bat 03 Sep 2021 10:10
by siberia-man » 21 Apr 2014 03:43
 1 2 3 4 5

Powershell Performance Recommendations by atfon 


15 965
by atfon » 01 Sep 2021 12:02  1 2 03 Sep 2021 08:27

How to create a variable with arguments by Squashman 


starting at 2 positions ? 3 482 01 Sep 2021 14:33
by PiotrMP006 » 01 Sep 2021 04:58
Paint & Animation Studio by T3RRY 
11 4622
 by T3RRY » 20 Jun 2020 10:02 01 Sep 2021 04:38

File List (additional text on seperate line) by sark 


2 439
by sark » 31 Aug 2021 03:28 31 Aug 2021 15:51

Ignoring Arguments by atfon 


12 853
by atfon » 26 Aug 2021 13:28 30 Aug 2021 12:15

Get every combination possible by AR Coding 


2 521
by AR Coding » 26 Aug 2021 22:15 29 Aug 2021 17:39

Delete files in folder B which exist in folder by Squashman 


5 545
A 28 A 2021 22 43
5 545
A 28 Aug 2021 22:43
by Yanta » 28 Aug 2021 05:02

You might also like