Harryhermit
Harryhermit

Reputation: 41

How to add column to txt tab delimited file

I have some data exported through a batch file from my sql server database in tab delimited format. How can I add a column to the file through a batch script with the heading "channel" and the text "channel1" to each row beneath the heading?

Upvotes: 1

Views: 405

Answers (1)

npocmaka
npocmaka

Reputation: 57252

@echo off
setlocal

set "file_path=F:\scriptests\textsql\Book1.txt"

break>tempfile
rem setlocal enableDelayedExpansion
set "firstline="
for /f "useback tokens=* delims=" %%A in ("%file_path%") do (
    if not defined firstline (
        rem after %%A the symbol is tab but not space
        echo %%A    "channel"
        set "firstline=."
    ) else (
        rem after %%A the symbol is tab but not space
        echo %%A    "channel1"
    )
)>>tempfile

rem first check created tempfile before uncomment the  line bellow
rem move /y tempfile "%file_path%"

endlocal

Upvotes: 1

Related Questions