Batch code that implements alternating output between two text peers _dos/bat

Source: Internet
Author: User
Tags goto
There are two texts as follows (in practice you do not know how many lines are in each of the two texts):

Text 1.txt

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Ccccccccccccccccccccccccccccccccccccccc
Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Ggggggggggggggggggggggggggggggggggggggg
Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Text 2.txt

Hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj


The batch output is requested as follows (two text alternating output):

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
Ccccccccccccccccccccccccccccccccccccccc
Iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
Eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
Ggggggggggggggggggggggggggggggggggggggg

Wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
Zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

Requirements:
1 try not to generate temporary files
2 code to be efficient
3 Code as concise as possible
Objective:
The only purpose is to improve together!

Answer:
More
Copy Code code as follows:

@echo off
Setlocal enabledelayedexpansion
for/f "delims="%%a in (' Type 11.txt ') does (
set/a m+=1
Set "Home!m!=%%a"
)
for/f "delims="%%b in (' Type 12.txt ') does (
set/a n+=1
Set "Bat!n!=%%b"
)
If%m% GTR%n% (
FOR/L%%c in (1 1%m%) do (
If not "!home%%c!" = = "" "Echo!home%%c!
If not "!bat%%c!" = = "" "Echo!bat%%c!
)
) Else (
FOR/L%%d in (1 1%n%) do (
If not "!home%%d!" = = "" "Echo!home%%d!
If not "!bat%%d!" = = "" "Echo!bat%%d!
)
)
Pause

 

Batman
Copy Code code as follows:

@echo off
for/f "delims="%%i in (1.txt) do set/a n+=1&call,set "_%%n%%=%%i"
for/f "delims="%%i in (2.txt) do set/a m+=1&call,set ".%%m%%=%%i"
If%n% GTR%m% (set "num=%n%") Else (set "num=%m%")
FOR/L%%i in (1,1,%num%) do (
If defined _%%i Call,echo%%_%%i%%
If defined.%%i Call,echo%%.%%i%%
)
Pause>nul

 

Pusofalse:
Copy Code code as follows:

@echo off&setlocal Enabledelayedexpansion&set N=-1
for/f "delims="%%a in (1.txt) do (
set/a n+=1
Set flag=
CALL:LP%%a
If not defined flag echo%%a
)
: LP
If "%1" equ "" set/a n+=1
Set m=skip=%n%
If "%m%" Equ "skip=0" set "m="
for/f "%m% delims="%%a in (2.txt) do if "%1" NEQ "" (Echo%1&echo%%a&set) Else echo flag=a&goto:eof
If "%1" equ "" Pause


 

Batman
Copy Code code as follows:

@echo off
Set "N=-1" &set "Flag=a"
: Begin
set/a n+=1
If%n% equ 0 (set "m=") Else (set "m=skip=%n%")
for/f "%m% delims="%%i in (1.txt) do echo%%i&if defined flag goto Next
Set No=a
: Next
for/f "%m% delims="%%i in (2.txt) do echo%%i&goto begin
If not defined no set "flag=" &goto begin
Pause>nul

 

Batman
Copy Code code as follows:

@echo off&setlocal enabledelayedexpansion
Set "num=0"
for%%a In (1.txt 2.txt) does (
for/f "delims="%%i in (%%a) do (
set/a n+=1
If!n! gtr!num! set "num=!n!"
set/p=%%i <nul>>temp.txt
)
Echo.>>temp.txt&set "n=0"
)
: LP
set/a n+ =1
for/f "tokens=%n%"%%i in (temp.txt) do echo%%i
if%n% neq%num% Goto LP
del/q temp.txt&pause& Gt;nul

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.