Computing.Net > Forums > Programming > Batch File-change space with tab

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch File-change space with tab

Reply to Message Icon

Name: Spiderman
Date: July 30, 2008 at 01:01:17 Pacific
OS: XP
CPU/Ram: 1 GB
Product: Dell
Comment:

Hi,
I have a text file with 4 words, space delimited.I need a batch file which replaces the 2 spaces between the first 3 words with a tab, and the last space remains unchanged.
Input:
Mary(space)John(space)Elly(space)Linda
Output:
Mary(tab)John(tab)Elly(space)Linda

I found this code, but it replaces all spaces with tab:
@echo off & setlocal EnableDelayedExpansion
(set TAB= )
for /f "delims=" %%i in (myfile.txt) do (
set row=
for %%j in (%%i) do (set row=!row!%%j!TAB!)
set row=!row:~0,-1!
echo.!row!> myfile.txt
)

Does anybody know how can I do that?
Thank you!

"He who asks is a fool for five minutes, but he who does not ask remains a fool forever" Chinese Proverb



Sponsored Link
Ads by Google

Response Number 1
Name: Elinor
Date: July 30, 2008 at 01:27:08 Pacific
Reply:

How about:

@echo off & setlocal EnableDelayedExpansion
set TAB=
set SPACE=
for /f "delims=" %%i in (myfile.txt) do (
set row=
set count=0
for %%j in (%%i) do (
set /A count=!count!+1
set char=%TAB%
if !count!==3 set char=%SPACE%
set row=!row!%%j!char!
)
set row=!row:~0,-1!
echo.!row! > myfile2.txt
)

hth

Elinor Hurst
http://elinorhurst.blogspot.com


0

Response Number 2
Name: Spiderman
Date: July 30, 2008 at 01:56:47 Pacific
Reply:

Super!I works great!Thank you so much!

"He who asks is a fool for five minutes, but he who does not ask remains a fool forever" Chinese Proverb


0

Response Number 3
Name: Razor2.3
Date: July 30, 2008 at 01:59:35 Pacific
Reply:

Meh, there's an easier way (untested):

(@FOR /F "tokens=1-3*" %%a IN (myfile.txt) DO @ECHO %%a	%%b	%%c %%d) > myotherfile.txt


0

Response Number 4
Name: Mechanix2Go
Date: July 30, 2008 at 14:36:54 Pacific
Reply:

hmm...

:: change 2 of 3 soaces to TAB

@echo off > newfile
setLocal EnableDelayedExpansion

for /f "tokens=1-2* delims= " %%a in (myfile.txt) do (
echo %%a %%b %%c> newfile)
goto :eof



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch File-change space with tab

batch file www.computing.net/answers/programming/batch-file/17799.html

Open shortcut with batch file www.computing.net/answers/programming/open-shortcut-with-batch-file/16269.html

File search / locate Batch file. www.computing.net/answers/programming/file-search-locate-batch-file/17182.html