I need to parse a tab-delimited text file by grabbing specific columns, like columns 1 and 5, and output each of these columns into a text file. Please find an example of the data file, and the code:
Data file:
COL1 COL2 COL3 COL4 COL5 COL6
123 345 678 890 012 234
234 456 787 901 123 345
etc
Batch file:
@echo off & setlocal
For /F "tokens=1,5*" %%i in (myFile.dat) do call :doSomething "%%i" "%%j"
goto :eof
:doSomething
Set VAR1=%1
Set VAR2=%2
@echo %VAR1%>>Entity.txt
@echo %VAR2%>>Account.txt
This works, however, the For
loop stops on the first line.
Could you help me in finding the issue?
Your code works fine for me, but maybe try this shortened version?
@echo off
for /F "tokens=1,5*" %%i in (myFile.dat) do (
echo %%i >>Entity.txt
echo %%j >>Account.txt
)
While I would recommend using the shortened version by Bali C, the reason the code above might be stopping after the first line is because the goto :eof
or exit
command is not at the end of the :doSomething
function.
@echo off & setlocal
For /F "tokens=1,5*" %%i in (myFile.dat) do call :doSomething "%%i" "%%j"
goto :eof
:doSomething
Set VAR1=%1
Set VAR2=%2
@echo %VAR1%>>Entity.txt
@echo %VAR2%>>Account.txt
goto :eof
Or if Extensions are disabled, it would be outputting the following error message:
/F was unexpected at this time.
To resolve this add EnableExtensions
to your setlocal
command.
@echo off & setlocal EnableExtensions
your code is working fine, check the output below.
Account.txt
"COL5"
"012"
"123"
""
Entity.txt
"COL1"
"123"
"234"
"etc"