Batch String Manipulation

What Is String Manipulation?

String manipulation is modifying or extracting parts of text — extremely useful when writing batch scripts that need to parse file names, user input, or system values.

Basic Substring Extraction

Format:

%variable:~start,length%
      
Example:
set text=HelloWorld
echo %text:~0,5%   :: Hello
echo %text:~5%     :: World
      

Replace Text Inside a Variable

Format:

%variable:old=new%
      
Example:
set word=banana
echo %word:a=o%   :: bonono
      

Remove Characters

set msg=hello_world
echo %msg:_=%    :: helloworld
      

Convert to Uppercase / Lowercase

Batch doesn't have built-in uppercase/lowercase, but you can do tricks:

Uppercase example

for %%A in ("a"="A" "b"="B" "c"="C") do (
  set text=!text:%%~A!
)
      

Check If a String Contains Something

set username=Kaloyan
echo %username% | find "Kalo" >nul
if %errorlevel%==0 echo Found
      

Trim Spaces

for /f "tokens=* delims= " %%A in ("%text%") do set text=%%A
      

Split a String

Split by delimiter using for /f:

set line=apple,banana,orange

for /f "tokens=1-3 delims=," %%A in ("%line%") do (
  echo %%A
  echo %%B
  echo %%C
)
      

What's Next?

String manipulation is a core Batch skill — next you’ll dive deeper into functions, loops, and building more advanced CLI utilities.