Removing & from string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gmh
Posts: 2
Joined: 15 Oct 2008 14:42

Removing & from string

#1 Post by gmh » 16 Oct 2008 14:56

I used FINDSTR in a FOR loop to pull out a specific string. The string contains an & in it. I am trying to remove it. I tried set q=%q:^&=% and it did not work. Any ideas?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 19 Oct 2008 12:13

gmh,

Use quotes around the assignment. Btw. it never hurts to use quotes.

Code: Select all

set "q=%q:&=%"

DosItHelp? :wink:

gmh
Posts: 2
Joined: 15 Oct 2008 14:42

Did not work...

#3 Post by gmh » 20 Oct 2008 09:12

He is an example of the actual string....

(doc NAME\ &\ NAME2\ NAME3)

If the & was not in the string I have other statements to remove the "(",")", and "\", but with the "&" it messes everything up.

greenfinch
Posts: 36
Joined: 17 Jul 2008 07:37

#4 Post by greenfinch » 23 Oct 2008 09:27

Quotes won't help (or hinder) here - you need setlocal enabledelayedexpansion:

Code: Select all

setlocal enabledelayedexpansion
set q=this^&that
echo %q%
set q=!q:^&=or!
echo %q%

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 24 Oct 2008 21:44

Quotes work fine on XP:

Code: Select all

@echo off
set "q=this&that"
echo."%q%"
set "q=%q:&=%"
echo. %q%


@echo off
set "q=(doc NAME\ &\ NAME2\ NAME3)"
echo."%q%"
set "q=%q:&=%"
set "q=%q:(=%"
set "q=%q:)=%"
set "q=%q:\=%"
echo. %q%

Output:

Code: Select all

"this&that"
 thisthat

"(doc NAME\ &\ NAME2\ NAME3)"
 doc NAME  NAME2 NAME3

:wink:

Post Reply