Batch text file tab replacement

July 9, 2010 at 09:55:18
Specs: Windows XP
I have had great help studying this post:
twotab.bat

I managed to remove empty double tabs, but besides, i also need to remove empty single tabs.

Finally i would like the batch file file to go through an entire directory and pick up any *.txt files, run the batch on each file and store each new file with the original file name, but new extension *.out.

The data is a 3 column TSV export from excel on the format:

data <tab>data <tab>data <return>
data <tab>data <tab> <return>
data <tab> <tab> <return>

The converted format should be:
data <tab>data <tab>data <return>
data <tab>data <return>
data <return>

Will appreciate any help on writing the batch file for this.


See More: Batch text file tab replacement

Report •


#1
July 9, 2010 at 18:36:09
@echo off & setlocal
for /f "tokens=*" %%a in (file.tsv) do (
set xx=%%a
set xx=%xx:{tab}{tab}=%
call :write
)
goto :eof
:write
>> new echo %xx%
::------- end
the {tab}{tab} is actually two literal tabs contained in the script at this point. forum won't show tabs very well.

Report •

#2
July 9, 2010 at 22:51:31
Thanks for your reply.

I couldn't quite figure out to get this working, so i ended up adding a custom marker in the single blank tabs that i need to remove. Also had to covert , into . for other reason, so i added that as well:

:: Replace , with .
:: Remove blank single tabs by requiring custom marker ¿ in blank single tabs
:: Remove blank double tabs
@echo off > output.txt & setLocal enableDELAYedeXpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set S=%%a
set S=!S:,=.!
set S=!S:	¿=!
set S=!S:		=!
>> output.txt echo.!S!
)

This is working for me and i get the right format on the output.

Any ideas on how to make the batch file go through an entire directory and pick up any *.txt files, run the batch on each file and store each new file with the original file name, but new extension *.out?


Report •

#3
July 9, 2010 at 23:12:24
for /f "tokens=*" %%b in ('dir /b /a-d *.txt) do (
for /f "usebackq tokens=* delims= " %%a in ("%%b") do (
set S=%%a
set S=!S:,=.!
set S=!S: ¿=!
set S=!S: =!
>> %%~nb.out echo.!S!
)
)
:: ----- maybe ... not tested

Report •

Related Solutions

#4
July 10, 2010 at 16:16:58
Thumbs up ;)

I need to learn more on batch commands and this really helps me getting started. Thanks a lot for taking the time to help me out.

I had to make a few modifications to get the batch running and ended up with this which is spot on what i was looking for (added ' and setLocal enableDELAYedeXpansion):

:: Replace , with .
:: Remove blank single tabs by requirering custom marker ¿ in blank single tabs
:: Remove blank double tabs
:: @echo off >
for /f "tokens=*" %%b in ('dir /b /a-d *.txt') do (
setLocal enableDELAYedeXpansion
for /f "usebackq tokens=* delims= " %%a in ("%%b") do (
set S=%%a
set S=!S:,=.!
set S=!S:	¿=!
set S=!S:		=!
>> %%~nb.out echo.!S!
)
)

Only downside is that i had to use the custom marker ¿ in the txt files in order to remove the single tabs, but i can live with that.

Thanks again for your time.


Report •

Ask Question