Page 1 of 1

Capturing tokens only once from two .txt files?

Posted: 14 Feb 2012 22:24
by Cat
I'm writing a code that will combine lines from two text files. The first text file contains:

Code: Select all

A
B
C
D

The second one contains:

Code: Select all

1
2
3
4

I want the output to be:

Code: Select all

A 1
B 2
C 3
D 4


Any ideas how to make this work? My best attempt results with:

Code: Select all

A 1
A 2
A 3
A 4
B 1
B 2
B 3
B 4
C 1
...


And so on like that. This uses two seperate FOR commands with synchronized tokens.

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 01:52
by Aacini

Code: Select all

@echo off
setlocal EnableDelayedExpansion
< file2.txt ( for /F "delims=" %%a in (file1.txt) do (
   set file2Line=
   set /P file2Line=
   echo %%a !file2Line!
   )
)

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 02:45
by foxidrive
That's neat Aacini

This modification does away with the need for delayed expansion.

Code: Select all

@echo off
setlocal
< file2.txt ( for /F "delims=" %%a in (file1.txt) do (
   set file2Line=
   set /P file2Line=
   for /f "tokens=1* delims==" %%b in ('set file2Line') do (
   echo %%a %%c
   )
   )
   )
pause


Poison characters and all.

Code: Select all

A 1 & a !b!
B 2 ^c
C 3 %d
D 4 |e

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 03:01
by jeb
But now file contents with equal signs are removed

file2.txt wrote:===1
=2
===3


As the "delims==" option split at the delim "=" and removes all subsequential delims

jeb

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 03:44
by foxidrive
jeb wrote:But now file contents with equal signs are removed

file2.txt wrote:===1
=2
===3


As the "delims==" option split at the delim "=" and removes all subsequential delims

jeb


Only leading equals signs.

Code: Select all

A 1 & a !b! = =
B 2 ^c ===
C 3 %d ==
D 4 |e1 =

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 08:43
by jeb
Or you could use the delayed toggling technic to be always safe :)

Code: Select all

@echo off
setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
      echo(!file1Line! !file2Line!
   )
)


jeb

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 10:05
by foxidrive
Yep! That's a winner!

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 11:12
by Ed Dyreen
jeb wrote:

Code: Select all

@echo off
setlocal DisableDelayedExpansion
< file2.txt (
   for /F "delims=" %%a in (file1.txt) do (
      set file2Line=
      set /P file2Line=
      set "file1Line=%%a"
      setlocal EnableDelayedExpansion   
      echo(!file1Line! !file2Line!
      endlocal
   )
)
Jeb, you are mean, you should endlocal :twisted: , besides you are eating memory AND slowing down performance :lol:

Re: Capturing tokens only once from two .txt files?

Posted: 15 Feb 2012 14:16
by jeb
You are right.

I forgot one of the key elements of the toggling technic.
Without the endlocal it doesn't toggle and it would fail :!:

I am so lazy :oops: