JSI Tip 8150. How do I remove the last N characters of a string?

I have scripted StripString.bat to remove the last N characters of a string.

The syntax for using StripString.bat is:

call StripString String newString \[Numchar\]


String      is the variable name that contains the string.
newString   is a call directed environment variable that will contain the new string, and
Numchar     is an optional parameter that contains N, the number of characters to remove. The default is 1.

@echo off
set string=123456789
call stripstring string newstring
@echo string=%string%, newstring=%newstring%
call stripstring string newstring 8
@echo string=%string%, newstring=%newstring%


string=123456789, newstring=12345678
string=123456789, newstring=1
StripString.bat contains:
@echo off
if \{%2\}

\{\} @echo Syntax: StripString String newVar \[NumChar\]&goto :EOF if not \{%3\}

\{\} ( call set %2=%%%1:~0,^-%3%% ) else ( call set %2=%%%1:~0,^-1%% )

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.