1

I have the following string

set str=aaaaa,bbbbb,ccccc,dddd

I need to replace everything from the first , (comma) till the end of string. but I don't know how the string starts.

I can't use the * wildcard as it is not in the start of string, so something like that -

set str=%str:,*=% >>>> Will not work

Any ideas ? it must be in a batch script

3
  • Is this "aaaaa,bbbbb,ccccc,dddd" a string or set? Commented Jan 14, 2013 at 10:57
  • Aree you asking how to replace ",bbbbb,ccccc,dddd" this portion in you string "aaaaa,bbbbb,ccccc,dddd"? and you don't know starting of string!!! Commented Jan 14, 2013 at 11:00
  • Yes. I wat str to be "aaaaa" and I don't know that it start like that. All I know it ends with a , (comma) and I cant do it in a FOR loop Commented Jan 14, 2013 at 12:03

2 Answers 2

3

There is a very simple and fast solution that avoids a FOR loop: use search and replace to inject a concatenated REM command at each comma :-)

@echo off
set str=aaaaa,bbbbb,ccccc,dddd
set "str=%str:,="&rem %
echo str=%str%

Explanation

Here is the the important line of code, before any expansion takes place:

set "str=%str:,="&rem %

And here is what the line is transformed into after variable expansion:

set "str=aaaaa"&rem bbbbb"&rem ccccc"&rem dddd

It becomes a SET statement, followed by a REM statement. The & operator allows you to combine multiple commands on one line. There is only one REM statement because everything after the first REM is considered part of the remark.

Sign up to request clarification or add additional context in comments.

3 Comments

Haha, I didn't know about that, but it's a million times better than mine :) Mine was fun to make though, but I think I knew there must have been a better way, oh well.
@Eli - Explanation added :)
Now I know even better why I hate batch :-)
1

To remove everything after the first comma you can use

@echo off
setlocal enabledelayedexpansion
set str=aaaaa,bbbbb,ccccc,dddd
for /f "delims=," %%a in ("%str%") do (
set str=%%a
echo !str!
)
pause >nul

To replace, simply strip it and append the new string on the end

    set str=%%a,new,string

Update

This will do the same, but without a for loop

@echo off
setlocal enabledelayedexpansion
set str=aaaaa,bbbbb,ccccc,dddd
set strip=%str%
set length=0
set start=0
set new=
:loop
if defined strip (
set strip=!strip:~1!
set /a length+=1
goto :loop
)
:comma
if %length% gtr 0 (
call :add !start!
if "!char!"=="," (
goto :break
) else (
set new=!new!!char!
set /a length-=1
set /a start+=1
goto comma
)
)

:break
echo %new%
pause >nul

:add
set char=!str:~%1,1!

8 Comments

I need it without a FOR LOOP
@Eli I have updated my answer, considerably more difficult, but it doesn't use any for loops.
I can use other delimier like space of semicolun, if it would make it easier.
No worries, not that I can think of unfortunately. I doesn't matter what delimeter you use, because you would still have to go through the entire string to find it anyways, glad it's working though :)
Just out of curiosity, why couldn't you use a for loop?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.