Computing.Net > Forums > Programming > data from txt files as sequential variables

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.

data from txt files as sequential variables

Reply to Message Icon

Name: alkjones
Date: October 22, 2009 at 10:01:19 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 22, 2009 at 13:05:09 Pacific
Reply:

@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


0

Response Number 2
Name: alkjones
Date: October 22, 2009 at 13:54:03 Pacific
Reply:

im going to need to spend some time getting my head around that one tomorrow..

thanks for your reply.


0

Response Number 3
Name: IVO
Date: October 22, 2009 at 14:13:09 Pacific
Reply:

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.


0

Response Number 4
Name: IVO
Date: October 23, 2009 at 02:59:54 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: data from txt files as sequential variables

Read lines from .txt file in DOS? www.computing.net/answers/programming/read-lines-from-txt-file-in-dos/15219.html

Extracting data out from txt file www.computing.net/answers/programming/extracting-data-out-from-txt-file-/20329.html

Batch File|Input from txt file www.computing.net/answers/programming/batch-fileinput-from-txt-file-/17293.html