Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

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

- - - - -

  • This topic is locked This topic is locked
9 replies to this topic

#1
HØLLØW

HØLLØW

    Member

  • Member
  • PipPip
  • 138 posts
  • Joined 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

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:

line1
line2
line3
line4
line5
line6
line7
line8

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

line1,line5
line2,line6
line3,line7
line4,line8

Is this possible???

 

 

Thank you!!!


Mit freundlichen Grüßen / Kind regards / Salutations distinguées / Atentamente / Distinti saluti

HØLLØW


How to remove advertisement from MSFN

#2
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag

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 off
setlocal enableDelayedExpansion

set "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, 15 October 2013 - 02:37 AM.

Posted Image


#3
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

You could also try this!
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET I1=File1.txt
SET I2=File2.txt
SET OF=Result.txt
SET "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%
ENDLOCAL
GOTO :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.

#4
MrJinje

MrJinje

    Tool™ Developer

  • Developer
  • 1,051 posts
  • Joined 14-October 09
  • OS:Server 2012R2
  • Country: Country Flag

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



#5
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag

@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, 15 October 2013 - 05:24 AM.

Posted Image


#6
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

@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 OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION 
SET I1=File1.txt
SET I2=File2.txt
SET OF=Result.txt
SET "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.

#7
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag

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


Posted Image


#8
HØLLØW

HØLLØW

    Member

  • Member
  • PipPip
  • 138 posts
  • Joined 01-March 07
  • OS:Windows 7 x64
  • Country: Country Flag

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


Mit freundlichen Grüßen / Kind regards / Salutations distinguées / Atentamente / Distinti saluti

HØLLØW

#9
jaclaz

jaclaz

    The Finder

  • Developer
  • 14,567 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag

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 OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /L %%? IN (1,1,2) DO SET /A?=1&FOR /F %%§ IN (file%%?.txt) DO SET %%??!?!=%%§&&SET /A?+=1
SET /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



#10
Yzöwl

Yzöwl

    Wise Owl

  • Super Moderator
  • 4,540 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag

Donator

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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users