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
Inseting underscore in between the test for a variable
Moderator: DosItHelp
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: Inseting underscore in between the test for a variable
Code: Select all
@echo off
set "str=The Pebbles & Bamm-Bamm Show"
set str="%str: =_%"
echo,%str%
pause
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: Inseting underscore in between the test for a variable
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
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
-
- Posts: 32
- Joined: 08 Jul 2011 03:38
Re: Inseting underscore in between the test for a variable
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
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
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!
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
@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
Dave Benham
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