data from txt files as sequential variables

October 22, 2009 at 10:01:19
Specs: Windows XP
I have 3 text files, each containing one item of data per row. viz

1.txt
---------
1
2
3


2.txt
----------
a
b
c


3.txt
----------
alpha
beta
gamma


The data in the text files is such that data on line 1 in text file 1 is directly related to data on line 1 in file 2 and data on line 1 in text file 3 and so on.

I need to loop through each text file in such a way that I can output each line from each file as arguments to another script:

Script.cmd 1 a alpha
Script.cmd 2 b beta
Script.cmd 3 c gamma

But I cant get it to work..

Can anyone help?

Cheers all
Alastair


See More: data from txt files as sequential variables

Report •


#1
October 22, 2009 at 13:05:09
@echo off & setlocal EnableDelayedExpansion > out.tmp

set cnt=0& for /F %%j in (1.txt) do call :NORM A %%j
set cnt=0& for /F %%j in (2.txt) do call :NORM B %%j
set cnt=0& for /F %%j in (3.txt) do call :NORM C %%j

set tail=
set cnt=0
for /F "tokens=2 delims=~" %%j in ('sort ^< out.tmp') do (
  set /A cnt+=1
  set tail=!tail! %%j
  if !cnt! equ 3 (
    script.cmd !tail!
    set tail=
    set cnt=0
  )
)
del out.tmp
goto :EOF

:NORM
  set fcnt=%cnt%
  if %cnt% lss   10 set fcnt=0%fcnt%
  if %cnt% lss  100 set fcnt=0%fcnt%
  if %cnt% lss 1000 set fcnt=0%fcnt%
  echo.%1%fcnt%~%2>> out.tmp
  set /A cnt+=1
goto :EOF


Report •

#2
October 22, 2009 at 13:54:03
im going to need to spend some time getting my head around that one tomorrow..

thanks for your reply.


Report •

#3
October 22, 2009 at 14:13:09
Sorry there is a bug in the script, replace

  echo.%1%fcnt%~%2>> out.tmp

with

  echo.%fcnt%%1~%2>> out.tmp

that is the third line from the bottom.

The script is modular and quite simple in its structure. If you need more support contact me by a private message.


Report •

Related Solutions

#4
October 23, 2009 at 02:59:54
There is another issue in the script posted to be mentioned.

The way to run the script.cmd may vary according to the kind of the script itself, i.e.

  .bat or .cmd        call script !tail!
  .exe (program)      prog !tail!

not sure in the case of a vbscript, probably like calling an executable, i.e. "vbscript !tail!".

.cmd is interpreted as .bat and is a legacy suffix used under OS/2 systems.


Report •


Ask Question