Merge and organize multiple text files into one file in a certain way

SueKor

Commendable
Jan 11, 2017
1
0
1,510
Hello,

I saw the batch command on this website as I was looking for a way to merge multiple text file into one file.


@echo off
for /r %%i in (*.txt) do (
if not %%~nxi == output.txt (
echo %%~nxi >> output.txt
type "%%i" >> output.txt
echo. >> output.txt
echo ============================================== >> output.txt
)
)


I wonder whether anyone could modify this command to merge files in the way I want. I have multiple response files from each person from a few hundred people. What I am trying to do is to combine multiple files from each person into one file, then combine individuals' files into one. My files are named in the following way and stored in one directory.

Q1_Person A.txt
Q1_Person B.txt
Q1_Person C.txt

Q2_Person A.txt
Q2_Person B.txt
Q2_Person C.txt

Q3_Person A.txt
Q3_Person B.txt
Q3_Person C.txt

I want these multiple files combined into one big text file that looks like this:

Person A (Person ID precedes the data)
Q1
Q2
Q3
========
Person B
Q1
Q2
Q3
========
Person C
Q1
Q2
Q3

I appreciate any help you can provide!!!







 
I am on mobile and do not have windows but what you can do is use the tokens feature in a for loop to grab just 'person a'

Without regex and sort this becomes a bit more complicated because of the way windows will sort a directory.

It's probably easier to write with python

For example to sort the way you want you really need to get file names to be:
Person A_Q1.txt
Person B_Q1.txt
Person C_Q1.txt

Do a simple 'dir /b' and you will see why this is the case.