Variable manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ankycooper9
Posts: 2
Joined: 26 Mar 2012 03:27

Variable manipulation

#1 Post by ankycooper9 » 26 Mar 2012 03:33

Let say I have 2 variables

Code: Select all

set var1=hello:\how\areyou.i'mgr8
set var2=hello:\how\are

now I have to remove contents of var2 from var1
(The desired result in the above example would be var1=you.i'mgr8 )

I have ftied the following but does not work

Code: Select all

set var1=!var1:!var2!=!


Please help

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Variable manipulation

#2 Post by dbenham » 26 Mar 2012 04:43

Code: Select all

set var1=!var1:%var2%=!

If you are in a situation where you cannot use immediate expansion, then:

Code: Select all

@echo off
setlocal enableDelayedExpansion
(
  set var1=hello:\how\areyou.i'mgr8
  set var2=hello:\how\are
  for /f "delims=" %%S in ("!var2!") do set var1=!var1:%%S=!
)


Dave Benham

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: Variable manipulation

#3 Post by !k » 26 Mar 2012 07:14

w/o DelayedExpansion

Code: Select all

call set var1=%%var1:%var2%=%%

ankycooper9
Posts: 2
Joined: 26 Mar 2012 03:27

Re: Variable manipulation

#4 Post by ankycooper9 » 26 Mar 2012 09:24

Thanks dbenham it woked!!

Post Reply