Read two txt files and set to variable bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Abreu
Posts: 2
Joined: 30 May 2014 11:25

Read two txt files and set to variable bat

#1 Post by Abreu » 30 May 2014 11:31

Hi Good evening. I would like to read two text files and set variables in the .bat file
One of the files is Name.txt and other is the lastName.txt

In Name.txt i have

John

Mike

Jonson

And in the lastName.txt i have

Smith

Michell

Brown

I try this

@echo on
set /a number=0
:begin
for /f "skip=%number%" %%G IN (name.txt) DO if not defined name set "name=%%G"
for /f "skip=%number%" %%H IN (lastName.txt) DO if not defined lsName set "lsName=%%H"

echo %name% %lsName%

set /a number = number +1
pause
goto begin



The idea is simple. Print on the prompt the name and the last name, reading the two data files and set together in different variables. Sorry, I believe the logic is simple, but I could not make it work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Read two txt files and set to variable bat

#2 Post by foxidrive » 30 May 2014 12:19

What do you wish to do with the names? It may affect which way to extract the names.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Read two txt files and set to variable bat

#3 Post by Squashman » 30 May 2014 12:21

Code: Select all

@echo off
setlocal enabledelayedexpansion
< fname.txt (for /f "delims=" %%G IN (lname.txt) DO (
   set /p fname=
   echo !fname! %%G
   )
)
Pause

Output

Code: Select all

John Smith
Mike Michell
Jonson Brown
Press any key to continue . . .

Abreu
Posts: 2
Joined: 30 May 2014 11:25

Re: Read two txt files and set to variable bat

#4 Post by Abreu » 30 May 2014 12:55

Just want to display on the screen with echo
Exactly that Squashman. Thank you very much. Sorry expose a simple logic problem, is that I am not very familiar with programming in bat.

Post Reply