String Operations

Maintained on
Table of Contents

When writing batch files, you often need to process strings, for example to clean up file names or format log output.

Batch files have no dedicated string functions, but the special syntax of variable expansion lets you perform replacement, substring extraction, and concatenation.

Replacing Strings

To replace part of a string stored in a variable, use the following syntax.

Syntax
set newvar=%variable:old=new%

Example

replace.cmd
@echo off
setlocal

set TEXT=Hello World
echo Original: %TEXT%

set TEXT=%TEXT:World=Japan%
echo Replaced: %TEXT%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>replace.cmd
Original: Hello World
Replaced: Hello Japan
C:\users\user>

Converting Path Separators

Convert
set PATH_STR=C:\Users\user\Documents
set PATH_STR=%PATH_STR:\=/%
echo %PATH_STR%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %PATH_STR%
C:/Users/user/Documents
C:\users\user>

Deleting Characters (Replacing with an Empty String)

Remove
set TEXT=  Hello
set TEXT=%TEXT: =%
echo [%TEXT%]
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo [%TEXT%]
[Hello]
C:\users\user>
Replacement Is Case-Insensitive

String replacement ignores case. %TEXT:hello=Hi% replaces Hello, HELLO, and hello alike.

Use * to delete everything up to and including a given string.

Delete
set TEXT=key=value
set VALUE=%TEXT:*==%
echo %VALUE%
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo %VALUE%
value
C:\users\user>
Characters You Cannot Replace

The string to be replaced cannot begin with =. Strings containing % or ! may also not be handled correctly by ordinary replacement.

Extracting Substrings

To extract part of a variable, use the following syntax.

Syntax
%variable:~start,length%
ParameterDescription
startZero-based index
lengthNumber of characters to extract (to the end if omitted)
Negative valueCounts from the end of the string

Example

substring.cmd
@echo off
setlocal

set TEXT=ABCDEFGHIJ

rem First 3 characters
echo %TEXT:~0,3%

rem 3 characters starting at the 4th (index 3 because it is zero-based)
echo %TEXT:~3,3%

rem Skip the first 2 characters
echo %TEXT:~2%

rem Last 3 characters
echo %TEXT:~-3%

rem Everything except the last 3 characters
echo %TEXT:~0,-3%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>substring.cmd
ABC
DEF
CDEFGHIJ
HIJ
ABCDEFG
C:\users\user>

Zero Padding

This example pads numbers to three digits.

zeropad.cmd
@echo off
setlocal enabledelayedexpansion

for /L %%i in (1,1,5) do (
    set NUM=00%%i
    set NUM=!NUM:~-3!
    echo !NUM!
)

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>zeropad.cmd
001
002
003
004
005
C:\users\user>
Tip

Zero padding is used frequently for sequential file names and log formatting. Prefix the number with zeros using set NUM=00%%i, then take the last three characters with !NUM:~-3!.

Concatenating Strings

In batch files, you can concatenate strings simply by writing variables next to each other.

concat.cmd
@echo off
setlocal

set FIRST=Hello
set SECOND=World
set RESULT=%FIRST% %SECOND%
echo %RESULT%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>concat.cmd
Hello World
C:\users\user>

Building a String in a Loop

accumulate.cmd
@echo off
setlocal enabledelayedexpansion

set RESULT=
for %%f in (*.txt) do (
    set RESULT=!RESULT! %%f
)

echo Files found:%RESULT%
endlocal

Checking Whether a String Contains Another String

Batch files have no dedicated “contains” syntax. Instead, use replacement: if removing the target text changes the string, the string contains it.

contains.cmd
@echo off
setlocal

set "TEXT=report_2026.txt"

if not "%TEXT:2026=%"=="%TEXT%" (
    echo Contains 2026.
) else (
    echo Does not contain 2026.
)

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>contains.cmd
Contains 2026.
C:\users\user>
Limitations of this check

Like replacement, this check is case-insensitive. It can also cause a syntax error if the variable’s value contains a double quote ("). For a case-sensitive check, use findstr /c:"text".

Getting the Length of a String

Batch files have no built-in way to get the length of a string. Instead, you can extract one character at a time and count until the result is empty.

strlen.cmd
@echo off
setlocal enabledelayedexpansion

set TEXT=Hello World
set LEN=0

:LOOP
if not "!TEXT:~%LEN%,1!"=="" (
    set /a LEN+=1
    goto LOOP
)

echo String: %TEXT%
echo Length: %LEN%

endlocal
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>strlen.cmd
String: Hello World
Length: 11
C:\users\user>
Using a Variable for the Position

You cannot write %TEXT:~%LEN%,1% when the start position or length is a variable. As in the example above, enable enabledelayedexpansion and write !TEXT:~%LEN%,1!. See the delayed expansion guide for details.

Things to Keep in Mind

  • Assign values with set "VAR=value". In set TEXT=abc a trailing space becomes part of the value. Wrapping the assignment in quotes, as in set "TEXT=abc", excludes any spaces outside the quotes.
  • A start position beyond the string length returns an empty result. When counting from the end, as in %TEXT:~-3%, a string shorter than 3 characters is returned in full.
  • There is no case-conversion feature. If you need one, consider combining for with replacement, or use PowerShell.
  • Replacement and substring extraction give unexpected results on undefined variables. Check first with if defined VARNAME.
  • The command line is limited to 8,191 characters. A string longer than that cannot be set with set or expanded with %VAR%.

Working with File Paths

In batch files, the for variable modifiers let you extract each part of a file path.

path_parts.cmd
@echo off
set FILE=C:\Users\user\Documents\report.txt

for %%F in ("%FILE%") do (
    echo Full path:      %%~fF
    echo Drive:          %%~dF
    echo Folder:         %%~pF
    echo File name:      %%~nF
    echo Extension:      %%~xF
    echo Name + ext:     %%~nxF
)
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>path_parts.cmd
Full path: C:\Users\user\Documents\report.txt
Drive: C:
Folder: \Users\user\Documents\
File name: report
Extension: .txt
Name + ext: report.txt
C:\users\user>
ModifierDescription
~fFull path
~dDrive letter
~pPath (folder part)
~nFile name without extension
~xExtension
~nxFile name with extension

Summary

OperationSyntax
Replace a string%variable:old=new%
Extract a substring%variable:~start,length%
Extract from the end%variable:~-length%
Concatenate stringsset RESULT=%A%%B%
Delete everything before a string%variable:*string=%

Practice Quiz

Practice Problem

Which syntax extracts the first 5 characters of the string stored in the variable TEXT?

Choose one answer.

  1. %TEXT:~0,5%
  2. %TEXT:~1,5%
  3. %TEXT:~5%
#Command Prompt #Batch File #String #set #Command