C:\_G\WWW\~ELISANET\INFO\tscmd164.html
<http://www.elisanet.fi/tsalmi/info/tscmd164.html>
Copyright © 2003- by Prof. Timo Salmi  
Last modified Fri 7-Dec-2018 10:59:13

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



164} How to concatenate (join/paste side by side) two files?

Assume the following LFN-type test files:
My File 1.txt
My File 1.txt Line 1
My File 1.txt Line 2
My File 1.txt Line 3
My File 1.txt Line 4
My File 1.txt Line 5

My File 2.txt
My File 2.txt Line 1
My File 2.txt Line 2
My File 2.txt Line 3
My File 2.txt Line 4

Using CONCAT.EXE "Join two text-files side by side" from tsfltc22.zip
  @echo off & setlocal enableextensions
  for /f "tokens=*" %%f in ("C:\_D\TEST\My File 1.txt") do set myfile1_=%%~sf
  for /f "tokens=*" %%f in ("C:\_D\TEST\My File 2.txt") do set myfile2_=%%~sf
  CONCAT %myfile1_% %myfile2_% /B2
  endlocal & goto :EOF

The output will be
C:\_M>c:\_d\test\cmdfaq
My File 1.txt Line 1  My File 2.txt Line 1
My File 1.txt Line 2  My File 2.txt Line 2
My File 1.txt Line 3  My File 2.txt Line 3
My File 1.txt Line 4  My File 2.txt Line 4
My File 1.txt Line 5

A VBScript-aided solution
  @echo off & setlocal enableextensions
  set myfile1_=C:\_D\TEST\My File 1.txt
  set myfile2_=C:\_D\TEST\My File 2.txt
  if not exist "%myfile1_%" (
    echo Exiting: File "%myfile1_%" not found
    goto :EOF)
  if not exist "%myfile2_%" (
    echo Exiting: File "%myfile2_%" not found
    goto :EOF)
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  findstr "'%skip%VBS" "%~f0" > "%vbs_%"
  ::
  :: Run the VBS script with Microsoft Windows Script Host Version 5.6

  cscript //nologo "%vbs_%" "%myfile2_%" "%myfile1_%"
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist "%%~f" del "%%~f"
  endlocal & goto :EOF
  '
  '................................................................
  'The Visual Basic Script
  '

  Const ForReading = 1, ForWriting = 2, ForAppending = 8 'VBS
  Dim fso, arg, f1, f2, line1, line2 'VBS
  Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
  Set arg = WScript.Arguments 'VBS
  Set f1 = fso.OpenTextFile(arg(0), ForReading) 'VBS
  Set f2 = fso.OpenTextFile(arg(1), ForReading) 'VBS
  Do While (Not f1.AtEndOfStream) Or (Not f2.AtEndOfStream) 'VBS
    line1 = "     " 'VBS
    If Not f1.AtEndOfStream Then 'VBS
      line1 = f1.ReadLine 'VBS
    End If 'VBS
    line2 = "" 'VBS
    If Not f2.AtEndOfStream Then 'VBS
      line2 = f2.ReadLine 'VBS
    End If 'VBS
    WScript.Echo line1 & " " & line2 'VBS
  Loop 'VBS

The output will be
C:\_D\TEST>cmdfaq
My File 2.txt Line 1 My File 1.txt Line 1
My File 2.txt Line 2 My File 1.txt Line 2
My File 2.txt Line 3 My File 1.txt Line 3
My File 2.txt Line 4 My File 1.txt Line 4
     My File 1.txt Line 5

Now what if you wish to start the first file at the third, not the first line? Better than trying to adjust the methods above, is first separately to filter the first two lines from the first file to an auxiliary file. For example see the method given in item #69.

[Previous] [Next]

C:\_G\WWW\~ELISANET\INFO\tscmd164.html
C:\_G\WWW\~ELISANET\FTPCMD\TSALMI.CMD /tscmd164
http://www.elisanet.fi/tsalmi/info/tscmd164.html
file:///c:/_g/www/~elisanet/info/tscmd164.html