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.
Merging txt files by same part filename (500+ files)
Moderator: DosItHelp
Merging txt files by same part filename (500+ files)
Last edited by bnwimmer on 09 Jun 2016 00:40, edited 1 time in total.
Re: Merging 500+ doubled files with a batch command
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.
Other readers may try to follow your requirements, but I find it difficult to know what you want to do.
Re: Merging txt files by same part filename (500+ files)
Ok I tried to improve. Waiting for approvement
Re: Merging txt files by same part filename (500+ files)
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.
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.
Re: Merging txt files by same part filename (500+ files)
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):
penpen
"<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