Substring in Batch File


Have you ever wonder is there any substring function or alike language construct to get a portion of the string ? I always  do.. and here's some examples on how to do that.

Syntax / Construction


  • start : is the start position of the text ( index begin from 1 ).
  • length : length of the portion of text extracted.

Examples 1

Get "Hello" part from "Hello World" text, i.e. text portion from position 1 to 5 of the original text.

set mytext="Hello World"
echo %mytext:~1,5%

Result :

Examples 2

Get and display the modified date portion of every ".xml" files scanned.

SETLOCAL enabledelayedexpansion
FOR %%IN (*.xml) do (
  SET datetime=%%~tv
  SET fdate=!datetime:~0,10!
  echo !fdate!

Result :