views:

181

answers:

5

I am writing a batch file script using Windows command and want to change each occurrence of some blank space with "," What is the simplest way to do that?

A: 

Well it will depend a lot on how you are getting the data, but you may be able to finagle something with a For-Do construct. Make the field seperator be a space then build the string back in the do with a , between each token. Might I suggest a more robust scripting language? You should have VBScript on just about any relatively modern windows bow.

EBGreen
A: 

I am new for command line syntax. co could you please provide me exact solutions , I have one text file and there are some users name and i need to separate those users name with comma separate (,).

In the text file, do you have one line with a bunch of names? Or a bunch of lines with a bunch of names on each line?
EBGreen
A: 

You could download sed.exe from http://unxutils.sourceforge.net/ and run this command:

sed -e "s/ /,/" infile.txt >outfile.txt
Jeremy
A: 

Hi thanks for your response. But i don't want to use other tool or third party exe

+1  A: 

If your users are a list of words on one line in a text file, separated by spaces, eg:

one two three four

Create a batch file SpaceToComma.txt as follows:

@echo off
setlocal
for /F "tokens=* delims= " %%a in (%1) do @set data=%%a
echo %data: =,%
endlocal

Then run it, you'll get the words separated by commas. Is this what you want?

C:\>SpaceToComma.bat data.txt
one,two,three,four

If you have a multi-line file, then this will do it:

data.txt

one two three four
five six seven

SpaceToComma.txt

@echo off
setlocal
for /F "tokens=* delims= " %%a in (%1) do @call :processaline %%a
endlocal
goto :eof
:processaline
setlocal
set data=%*
echo %data: =,%
endlocal
goto:eof

Output

C:\>SpaceToComma.bat data.txt
one,two,three,four
five,six,seven

(There's probably a clever way of doing this without the subroutine, using the !data! syntax for delayed variable expansion, but I couldn't get it to work with the substitution syntax.)

If this is not what you want, then please explain and I can try to help.

(PS: I delight in using batch files where people insist it can't be done.)

Richard A