Edit: another way to do this (perhaps more straight forward), this has a different limit - the size of the available variable / env. space (about ~ 32 kB for XP, not limited to Vista up - at least for MS docs). It creates the appearance of array variables to hold your transposed lines, and then displays them:
@echo off setlocal enabledelayedexpansion set "tab= " for %%F in (file*.txt) do ( set /a count=0 for /f "tokens=2 delims=%tab%" %%L in (%%F) do ( set /a count+=1 for /L %%T in (!count!,1,!count!) do ( set L[!count!]=!L[%%T]!%%L%tab% ) ) ) for /L %%L in (1,1,%count%) do echo !L[%%L]!
First, let me say that batch not suitable for this task (which would be trivial, say, in C ), but if you need to use it, here is the way:
@echo off setlocal enabledelayedexpansion set "tab= " for /f %%C in ('dir /b file*.txt') do set /a count+=1 (for %%F in (file*.txt) do ( set line=0 for /f "tokens=2 delims=%tab%" %%V in (%%F) do ( set "outline=%%F" for /l %%N in (1,1,!line!) do (set "outline=%tab%!outline!") set outline=!outline!%tab%%%V set /a line+=1 echo !outline! ) )) >presorted.txt set /a cutoff=%count%-1 set line=0 for /f "tokens=2 delims=%tab%" %%O in ('sort /r presorted.txt') do ( set outp=%%O%tab%!outp! set /a lc=!line!^%%count% if !lc!==%cutoff% ( echo !outp! set "outp=" ) set /a line+=1 )
How it works:
- Fist, check how many files will be processed. This gives the number of columns. (First
for and count variable) - Read each input file, edit the required column so that it is preceded by the number of tabs, equal to the line number and file name, in the intermediary file. (Second block bounded by
() ) - Sort this file so that consecutive lines correspond to consecutive columns in the final release (
sort /r presorted.txt ) - Move blocks of rows into columns with
modulo count to start a new row (last for ).
Notes:
- the order of the columns depends on the sort order of the file names.
- all files must have the same number of lines
"tab= " (in the third line) should contain the actual tab- it accepts ascii-ish encoding (where
tab sorted before any printable character). - it does not accept any characters
! in inputs (they require special processing when slow expansion is enabled) - is displayed on the screen. If file output is required, put the code in a batch file, and then run
mybatchfile.bat >output.txt
source share