2 text files to 1

June 28, 2010 at 15:52:20
Specs: Windows XP, 2g
I know there is a simple solution to this but I'm not very expreianced with changing text files in batch.

I would like to make a text file out of 2 exsisting text files.
this is how its setup

File 1:

File 2

I would like to somehow combine lines into one file with each text staying on the same line with a dash seperator, like so:
Dan - apples
Jerry - oranges
Bob - cherrys
Harry - grapes
Tim - peaches

What would be the simplist way to accomplish this?



See More: 2 text files to 1

Report •

June 28, 2010 at 18:41:10
no, actually, there isn't! (a simple solution). You've hit on one of batch's weakenesses: only one file channel is available at any one time (ie: no parallel file access). Once one file is established for input/reading, any additional file(s) must be handled in a "start-to-finish" mode as inner-loops, making your operation somewhat tricky.
here's one solution, i'm sure there are better ones out there.
(another method is to buffer the entire file into vars, but i went
for space-optimized vs time optimized.)

@echo off & setlocal enabledelayedexpansion
set xx=
set cc=0
for /f "tokens=*" %%a in (filea.txt) do call :aa
goto :eof
for /f "%xx% tokens=*" %%b in (fileb.txt) do (
>> merg_A_B.txt echo %%a - %%b
set /a cc+=1
set xx=skip=!cc!
goto :eof

Report •

June 28, 2010 at 20:01:03
Here's a variation on the same concept, using skip= as a value in a variable:

SET /A row=0
FOR /F "%skip%usebackq tokens=2,3 delims==," %%a IN ("\Your\Path\To\FileA.txt") DO (
    FOR /F "%skip%usebackq tokens=* delims=," %%c IN ("\Your\Path\To\FileB.txt") DO (
        ...Do stuff with %%a, %%b and %%c...
        SET /A row+=1
        SET skip=skip=!row! 
        GOTO Read_Files

It's not directly applicable to the problem at hand (unrelated delims, tokens and such),
but it is another example of getting over a stumbling block :-)

Insomniac at large

Report •

June 28, 2010 at 20:17:48
is it just me, or has anyone else had this problem:
set xx=1
for /f "skip=%xx% tokens=*" dah dah dah
doesn't work for me, but:
set xx=skip=1
for /f "%xx% tokens=*" dah dah
(i found out skip=0 is not allowed, btw.)
light some shed on this, please o learned gurus!!

Report •

Related Solutions

July 17, 2010 at 16:26:07
If you're not committed to using the DOS batch language, this would probably be quite simple in Perl. You would open both input files and an empty output file. Read the next line in both input files, and write a new record in the output file. Repeat until EOF (be careful, in case one input ends before the other).

Report •

Ask Question