Page 1 of 1

Inseting underscore in between the test for a variable

Posted: 08 Sep 2011 03:07
by kumar_kondapalli
Hi All,

I have the below requirement ie To insert Underscores instead of spaces for a set of words present in a variable. For eg: if the variable contains " The Pebbles & Bamm-Bamm Show" , then I need an output "The_Pebbles_&_Bamm-Bamm_Show". The number of words vary each time. They are different too.

Please let me know if this is possible using Bat filee.

Thanks,
Kumar

Re: Inseting underscore in between the test for a variable

Posted: 08 Sep 2011 05:29
by Batcher

Code: Select all

@echo off
set "str=The Pebbles & Bamm-Bamm Show"
set str="%str: =_%"
echo,%str%
pause

Re: Inseting underscore in between the test for a variable

Posted: 09 Sep 2011 00:14
by kumar_kondapalli
Hi Batcher,

Thanks a lot. One more help from you

Is it possible to put the output 'STR' in to a txt file??

Ie what i want is after generating the Output with Underscore symbol . i want the output result to be stored in a txt file.

Please help me in this ...

Thanks,
kumar

Re: Inseting underscore in between the test for a variable

Posted: 09 Sep 2011 02:49
by kumar_kondapalli
Hey i got the answer for this ..

I have to use >>c:\somelogfile.txt. But if i run the bat file one more time the same value is added to the txt file. Can i prevent that happeing.

@echo off
set "str=The Pebbles & Bamm-Bamm Show"
set str="%str: =_%"
echo,%str% >>c:\somelogfile.txt

pause

Re: Inseting underscore in between the test for a variable

Posted: 09 Sep 2011 17:46
by Ocalabob
Greetings kumar_kondapalli,

Change the line:
echo,%str% >>c:\somelogfile.txt
to
echo,%str%>c:\somelogfile.txt

That answers your question but does it solve the problem?

Best Wishes!

Re: Inseting underscore in between the test for a variable

Posted: 10 Sep 2011 00:01
by dbenham
@Ocalabob - You certainly eliminate duplicate values, but I think kumar wants to preserve the entire logfile, not overwrite it.

If we state the problem in English, the answer pretty much falls out as long as you know your "DOS" commands:
- create the string
- check if the string already exists in the logfile
- if it does not exist then add it

The string creation has been solved.
FINDSTR is used to check if the string already exists in the file. We aren't interested in the output so we direct it to NUL. We only want the error status - It returns an error if the string is not found.
The || is used to conditionally execute the append operation if an error occurred

untested code

Code: Select all

@echo off
set log="c:\somelogfile.txt"
set "str=The Pebbles & Bamm-Bamm Show"
set str="%str: =_%"
findstr /x /c:"%str%" %log% >nul || echo(%str%>>%log%


Dave Benham