Inseting underscore in between the test for a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Inseting underscore in between the test for a variable

#1 Post by kumar_kondapalli » 08 Sep 2011 03:07

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

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: Inseting underscore in between the test for a variable

#2 Post by Batcher » 08 Sep 2011 05:29

Code: Select all

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

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: Inseting underscore in between the test for a variable

#3 Post by kumar_kondapalli » 09 Sep 2011 00:14

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

kumar_kondapalli
Posts: 32
Joined: 08 Jul 2011 03:38

Re: Inseting underscore in between the test for a variable

#4 Post by kumar_kondapalli » 09 Sep 2011 02:49

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

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Inseting underscore in between the test for a variable

#5 Post by Ocalabob » 09 Sep 2011 17:46

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!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Inseting underscore in between the test for a variable

#6 Post by dbenham » 10 Sep 2011 00:01

@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

Post Reply