Page 1 of 1

Read two txt files and set to variable bat

Posted: 30 May 2014 11:31
by Abreu
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.

Re: Read two txt files and set to variable bat

Posted: 30 May 2014 12:19
by foxidrive
What do you wish to do with the names? It may affect which way to extract the names.

Re: Read two txt files and set to variable bat

Posted: 30 May 2014 12:21
by Squashman

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 . . .

Re: Read two txt files and set to variable bat

Posted: 30 May 2014 12:55
by Abreu
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.