Batch – Delete Characters in a String

24,106

First of all, don’t use path as a variable name.  Windows Command Prompt (unlike most components of Unix, including the shells) treats variable names in a case-insensitive way, so path is the same as PATH, which is your execution search path, and if you assign something random to that, you’ll be in a world of hurt.

Use the %variable:str1=str2% form.  (I presume that you know that set /? documents these forms.)  This one is substitution:

C:\> set play=food

C:\> echo %play:foo=bar%
bard

C:\> set animal=cat

C:\> echo %animal:at=ow%
cow

This supports a very limited pattern-matching capability; %variable:*str1=str2% will find and delete everything up to and including the first occurrence of str1 and replace it with str2.  In particular, %variable:*\=% will replace everything up to and including the first \ with null.  E.g., if %pathname% is C:\Windows\Example\001 Example\005 Example, then %pathname:*\=% will evaluate to Windows\Example\001 Example\005 Example (without the C:\). If your pathname will always have four levels, you can do

for %%I in (%pathname%) do set tempname=%%~I
set tempname=%tempname:*\=%
set tempname=%tempname:*\=%
set tempname=%tempname:*\=%
set tempname=%tempname:*\=%
set basename=%tempname%
echo %basename%

The for statement is a bit of a kludge.  It’s a loop that is guaranteed to execute exactly once with index variable %%I set to the value of %pathname%1.  Then it assigns %%~I to tempname.  %%~I is the value of %%I (i.e., the value of %pathname%) except, if that string begins and ends with quote (") characters, they are removed.  If you’re sure that %pathname% doesn’t begin and end with quotes (e.g., because you’ve already done this, or something equivalent), you can skip this step and just start by saying set tempname=%pathname%.

If the pathname has an indeterminate number of levels, do

for %%I in (%pathname%) do set tempname1=%%~I
:loop
set tempname2=%tempname1:*\=%
if not %tempname1% == %tempname2% ( set tempname1=%tempname2% & goto loop )
set basename=%tempname1%
echo %basename%

which loops until there are no \s left.
______________
1 Actually, its behavior may be more complex if %pathname% is null (empty), or if it contains wildcard (pattern-matching) characters like ? and *. There may be other peculiar cases that I haven’t found.  (Please let me know if there’s a cleaner way to deal with quotes.)


Warning: I’ve observed %variable:str1=str2% to behave oddly if %variable% is null.

Share:
24,106

Related videos on Youtube

NvyxMarx
Author by

NvyxMarx

Updated on September 18, 2022

Comments

  • NvyxMarx
    NvyxMarx over 1 year

    For example, I have the following path: "C:\Windows\Example\001 Example\005 Example" I must extract the "005 Example" from this string. I've tried it with "set path=%path:~0,11%", but the path can also be "C:\Windows\Example\001 Example\005 Example_2"

    What I need is to cut the string after the last "\"

    How can I do this?

    • Scott - Слава Україні
      Scott - Слава Україні over 7 years
      Your question is a bit confusing.  How does %path:~0,11% help you get 005 Example?  It looks like it is designed to get "C:\Windows.
  • NvyxMarx
    NvyxMarx over 7 years
    Thanks verry Much. set tempname=%tempname:*\=% was what i searched for.