#1 17 Feb 2007 13:03

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Variable search and replace hell

Anyone got any bright ideas how one could overcome this little problem:

@echo off

set x=asd*asd

echo:This works just fine: %x:asd*=ASD%
pause
echo:This sort of does, but might not look quite as intended: %x:*asd=ASD%
pause
echo:But what if I just want to replace the "*"?
pause
echo:Sadly, this doesn't work at all: %x:^*=ASD%
pause
echo:And this is just HORRIBLE:
echo:%x:*=ASD%
echo:You won't even get as far as this line!

Lovely, huh?

~bxm


cmd | *sh | ruby | chef

Offline

#2 17 Feb 2007 13:23

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,077
Website

Re: Variable search and replace hell

for /f "tokens=1,2 delims=*" %%G in ("asd*asd") do echo %%G~%%H

Offline

#3 17 Feb 2007 15:23

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: Variable search and replace hell

Yeah I've worked around it with something like that before, but what if, say, I had variable numbers of consecutive "*", the number of which I wanted to preserve in the substitution?  That there "for" command would reduce them all to a singular.

Or an unknown/very large number of "*"?  OK, OK I'm pushing the bounds of likelihood now, but still...


cmd | *sh | ruby | chef

Offline

#4 17 Feb 2007 15:33

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,077
Website

Re: Variable search and replace hell

For those cases I think FindStr /L is the tool to look at

If you dont want to write a text file, you can just pipe text into FindStr.

Offline

#5 19 Feb 2007 12:28

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: Variable search and replace hell

Hmmm ... I'm not sure how a literal string search is going to help out searching and replacing.  Ugh, why can't Microsoft ship Windows with a half-arsed "sed" rip off?  After all, they've managed to include a half-arsed "egrep" rip off...


cmd | *sh | ruby | chef

Offline

#6 02 Mar 2007 19:48

Dan9999
Member
Registered: 16 Feb 2007
Posts: 7

Re: Variable search and replace hell

Try this.  Problem is that if you have quotes in there it won't work.  You have to remove them first.

If you want to remove quotes, I guess it's SET OrigString=%OrigString:"=%

Hope it works

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set OrigString=asd*asd*asd;\;
set ResultString=
set Searchchar=*
set ReplChar=+

SET /A counter=0
:tagSTART
set tmpChar=!OrigString:~%counter%,1!
if not defined tmpChar goto tagEND
if  "%tmpChar%" == "%Searchchar%" SET tmpChar=%ReplChar%
set ResultString=%ResultString%%tmpChar%
set /A counter=%counter% + 1
goto tagStart
:tagEND
echo %ResultString%

Offline

#7 02 Mar 2007 20:07

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,077
Website

Re: Variable search and replace hell

Very nice

I still cant believe Microsoft dont have a utility for this, fingers crossed for the vista resource kit next month.

Offline

#8 09 Mar 2007 19:24

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: Variable search and replace hell

Sorry, I never said thanks -- thanks!

Not tried your code as yet, but I like what you've done there.

As for quotes, I'd tend to switch them out for a character that's unlikely to be in the string (or determine one with some jiggery pokery) and put them back in at the end.

Also, I'm not a fan of using "goto" in any other context than "goto :EOF".  A hang over from the teachings of Mr Bird, my high school IT teacher, many years ago. I only use it when I have no other viable options, so I'd go something like this:

if not defined tmpChar goto :EOF
if  "%tmpChar%" == "%Searchchar%" SET tmpChar=%ReplChar%
set ResultString=%ResultString%%tmpChar%
set /A counter=%counter% + 1
call :tagStart

echo %ResultString%

But that's just me.

Last edited by bluesxman (09 Mar 2007 19:29)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by FluxBB