Sign in to follow this  
Followers 0
HØLLØW

Loop to 2 files and echo the lines together ("array"!?)

10 posts in this topic

Hey Scripting-Heroes ;)

I need to do the following in a Batch-Script (YES, it has to be Batch...!).

I have 2 files with this sample content:

line1line2line3line4
line5line6line7line8

I need a Batch-Script that runs to these 2 files, read the lines and the give this output:

line1,line5line2,line6line3,line7line4,line8

Is this possible???

Thank you!!!

0

Share this post


Link to post
Share on other sites

Using my superhuman Google powers (dripping with sarcasm LOL), the very first response for the search string of -- "batch read from two text files at the same time" -- gives...

It seems this EXACT same question was handled here a little over a year ago. (In fact the similarity makes me wonder if this is a homework question in some programming course?) It is an interesting question however. All credit of course goes to the participants in that discussion, with the main answers given by dbenham, with input from wmz.

There are three or four different methods proposed, two quite similar, but this is my favorite: (I made one slight change to meet your requirement that the content from the files are comma separated, and I used the Echo: construct.)


Option 3

Use SET /P to read both files. FIND is used to get a count of the number of lines in file 1 because SET /P cannot tell the difference between a blank line and end of file.

This option eliminates a lot of limitations and complexity, but introduces its own limitations.

Limitations:

- Lines must use Windows style line terminators of <CR><LF>. Unix style <LF> will not work.
- lines are limited to 1021 bytes
- trailing control characters are stripped from each line.

This option is by far the fastest. It is preferred as long as the limitations are acceptable.

@echo offsetlocal enableDelayedExpansionset "file1=a.txt"set "file2=b.txt"set "out=out.txt" for /f %%N in ('type "%file1%"^|find /c /v ""') do set "cnt=%%N">"%out%" 9<"%file1%" <"%file2%" (    for /l %%N in (1 1 %cnt%) do (        set "ln1="        set "ln2="        <&9 set /p "ln1="        set /p "ln2="        echo:!ln1!, !ln2!    ))type "%out%"

On the off chance that this was some kind of assignment, or not, rather than just take this solution and say "Thank You", it would do you a lot of good to respond with a detailed explanation of exactly what is happening in this code as proof that you fully understand WHY it works. Understanding the why is the important thing and is what will allow you to apply this solution to future problems. You are also free to prefer an alternate solution, and if so, it would be interesting to see it and for you to tell us why you prefer it.

Cheers and Regards

Edited by bphlpt
0

Share this post


Link to post
Share on other sites

You could also try this!

@ECHO OFFSETLOCAL ENABLEEXTENSIONSSET I1=File1.txtSET I2=File2.txtSET OF=Result.txtSET "DLM=,">%OF% (SETLOCAL DISABLEDELAYEDEXPANSION	FOR /F "DELIMS=" %%a IN (%I1%) DO (SET "I1_LINE=%%a"		SETLOCAL ENABLEDELAYEDEXPANSION		SET/P "I2_LINE="		ECHO(!I1_LINE!!DLM!!I2_LINE!		ENDLOCAL)	ENDLOCAL)<%I2%ENDLOCALGOTO :EOF
Your output will be found in the file defined at Line 5

Change the filenames in Lines 3 & 4 to suit your actual input files.

0

Share this post


Link to post
Share on other sites

Here is something similar in powershell as an example for future readers.

$FILE1 = Get-Content .\File1.ini$1 = $FILE1[0]$2 = $FILE1[1]$3 = $FILE1[2]$4 = $FILE1[3]$FILE2 = Get-Content .\File2.ini$5 = $FILE2[0]$6 = $FILE2[1]$7 = $FILE2[2]$8 = $FILE2[3]$1+","+$5$2+","+$6$3+","+$7$4+","+$8
0

Share this post


Link to post
Share on other sites

@Yzöwl,

Nice! Even faster and more compact.

Would you please explain why you need to DISABLEDELAYEDEXPANSION the FOR statement? Or a link to a tutorial would be fine. And I assume the layers of SETLOCAL alleviate the need to clear the two line variables, even if there are blank lines, or the second file has fewer lines than the first file? I don't guess you really needed the I1_LINE variable since you could have just used %%a, or is it needed because of the SETLOCAL layer? Another advantage that your solution has, I think, is that the 1021 byte line length limit now only applies to the second file, while the first file now has a maximum line length limit of 8191 bytes, if I understand correctly. It's not a problem, merely a matter of specification and implementation, but both of our solutions quit when the first file runs out of lines.

@MrJinje.

I think your solution will quickly get out of hand for very large input files, but I know it could be made flexible and compact with a loop or two.

Cheers and Regards

Edited by bphlpt
0

Share this post


Link to post
Share on other sites

@bphlpt

My solution only works if there are no blank lines, (especially in File 1). The disabling of delayed expansion was probably an OTT addition to protect the possibility of exclamaion marks in File1, you could very likely remove SETLOCAL DISABLEDELAYEDEXPANSION from line 7 and the corresponding ENDLOCAL from line 13.

@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION SET I1=File1.txtSET I2=File2.txtSET OF=Result.txtSET "DLM=,">%OF% (FOR /F "TOKENS=*" %%a IN (%I1%) DO (SET "I1_LINE=%%a"		SET/P "I2_LINE="		ECHO:!I1_LINE!!DLM!!I2_LINE!))<%I2%
Here's the powershell solution tidied a little.

$x = Get-Content .\File1.txt$y = Get-Content .\File2.txt$i = 0; $x | ForEach-Object {($_, $y[$i]) -join ","; $i++}>Results.txt
Once again see Results.txt for your expected output and change the filenames at the end of lines 1 and 2 as necessary.
0

Share this post


Link to post
Share on other sites

Thanks for the info. Actually, both of our solutions handle blank lines in the second file just fine, at least it produces the output I would expect and everything remains synced up. But you're right that your solution will get out of sync if there are blank lines in the first file, while mine handles those OK as well. [Please note that everywhere I say "my" solution I mean the one by dbenham, of course.]

Cheers and Regards

0

Share this post


Link to post
Share on other sites

Ok thank you guys!

It works fine for now - But I tried to modify the script to get it working with multiple files, no chance for me...

So is it possible to get it working with more than 2 files?

In my case there are 3 scenarios I need this:

  1. 4 files to "connect" together
  2. 5 files to "connect" together
  3. 6 files to "connect" together

The output should be the same as described in this topic.

Thank you!!!

0

Share this post


Link to post
Share on other sites

As expected :whistle: the initial question was a "generic" one and making use of "fake" data.

As an example the following answers the original question, but it won't help you with the actual GOAL that you have (which was already changed).

@ECHO OFFSETLOCAL ENABLEDELAYEDEXPANSIONFOR /L %%? IN (1,1,2) DO SET /A?=1&FOR /F %%§ IN (file%%?.txt) DO SET %%??!?!=%%§&&SET /A?+=1SET /A?-=1&FOR /L %%? IN (1,1,!?!) DO ECHO !1?%%?!,!2?%%?!

I would advise you to state EXACTLY and including ALL the details, the actual GOAL you have, provide as much background about the question as possible and provide some "non fake" data.

jaclaz

0

Share this post


Link to post
Share on other sites

As expected :whistle: the initial question was a "generic" one and making use of "fake" data.

Technically not, the Subject Title, "Loop 2 files…" and the following

I have 2 files…<snip />

I need a Batch-Script that runs to these 2 files…<snip />

Since the original question has been solved, I will be closing this Topic.

I would suggest that the OP uses the original solution, renames Result.txt and inputs that renamed file with File3.txt etc. until they have all 4, 5 or 6 files done. Alternatively copy each seperate file into a different spreadsheet column and output it as a .csv.

0

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.