Page 1 of 1

Removing & from string

Posted: 16 Oct 2008 14:56
by gmh
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?

Posted: 19 Oct 2008 12:13
by DosItHelp
gmh,

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

Code: Select all

set "q=%q:&=%"

DosItHelp? :wink:

Did not work...

Posted: 20 Oct 2008 09:12
by gmh
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.

Posted: 23 Oct 2008 09:27
by greenfinch
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%

Posted: 24 Oct 2008 21:44
by DosItHelp
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: