Merging txt files by same part filename (500+ files)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bnwimmer
Posts: 2
Joined: 08 Jun 2016 06:50

Merging txt files by same part filename (500+ files)

#1 Post by bnwimmer » 08 Jun 2016 07:01

Hello and greetings to the batch experts.
My problem:
I have 2 txt files from each of my students (500+ at all) in a folder. Pattern like this:
12345678_V1_blog_1.txt -> from Student x
12345678_V1_blog_2.txt -> from Student x
29458273_V1_blog_1.txt -> from Student y
29458273_V1_blog_2.txt -> from Student y
.
.
.
My wish is to merge the matching txt files with a command like
merge.bat *.txt
that merges
29458273_V1_blog_1.txt
29458273_V1_blog_2.txt
to 29458273_V1_blog.txt
and so on.

Anyone here who can show how this can be achieved?
Thank you very much. You support is much appreciated.
Last edited by bnwimmer on 09 Jun 2016 00:40, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merging 500+ doubled files with a batch command

#2 Post by foxidrive » 08 Jun 2016 10:31

I would ask you to try and improve your description.

Other readers may try to follow your requirements, but I find it difficult to know what you want to do.

bnwimmer
Posts: 2
Joined: 08 Jun 2016 06:50

Re: Merging txt files by same part filename (500+ files)

#3 Post by bnwimmer » 09 Jun 2016 02:32

Ok I tried to improve. Waiting for approvement

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Merging txt files by same part filename (500+ files)

#4 Post by foxidrive » 14 Jun 2016 22:45

I just returned from my holiday in Bermuda, so just saw that you had edited your first post.
Many people read messages based on new posts and would not see your changed first post.

Your task is not difficult, but an actual listing of the files will show us if different code needs to be used - because the filenames themselves are part of the way batch code often works.

We'd waste our time writing code if you returned to say the code doesn't work, and it's because some aspect you had shown of the lists was actually different.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Merging txt files by same part filename (500+ files)

#5 Post by penpen » 15 Jun 2016 03:34

I think the filenames have this format:
"<matrikel number>_<lecture id>_<exercise group id>_<week no>.<ext>", or
"[0-9]^{7-8}_[A-Z][1-9]*[0-9]+_[a-z]+'.'[A-Z,a-z,0-9]+".

If that is true then this may help (untested):

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
if not exist "merged" md "merged"

set "id="
set "max=0"
for /f "tokens=1,4 delims=_." %%a in ('dir /b /o:n "*_V1_blog*.txt"') do (
   if "%%~a" == "!id!" (
      if !max! lss %%~b set "max=%%~b"
   ) else (
      if not "" == "!id!" call :merge "!id!" "!max!"
      set "id=%%~a"
      set "max=0"
   )
)
if not "" == "!id!" call :merge "!id!" "!max!"
endlocal
goto :eof

:merge id max
>"merged\%~1_V1_blog.txt" (
   for /L %%a in (1, 1, %~2) do (
      if exist "%~1_V1_blog_%%~a.txt" (
         echo(========= "%~1_V1_blog_%%~a.txt" =========
         type "%~1_V1_blog_%%~a.txt"
         echo(
      ) else (
         echo(========= "%~1_V1_blog_%%~a.txt" is missing =========
         echo(
      )
   )
)
goto :eof


penpen

Post Reply