Concatenate files depending on their name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
antonio.baena
Posts: 2
Joined: 23 Dec 2013 05:04

Concatenate files depending on their name

#1 Post by antonio.baena » 23 Dec 2013 05:22

Hello!

I need your help to do this, I 've been trying it for a couple hours unsuccessfully :(.

Every day I receive some files like this:

209_077_131223230450.txt
102_077_131223230450.txt
019_077_131223230451.txt
319_077_131223230451.txt
209_077_131222230450.txt
102_077_131222230450.txt
019_077_131222230451.txt
319_077_131222230451.txt

You can notice that the first six characters after _ are the date (2013-12-23).

Well I want to create a program that concatenate all the files of the same date.

What I'm doing by now is writing this code:

copy *131222*.txt 131222.txt

But I d like something more automatic, something like

for %%date in (!each date that appear in my directory) do (copy *%%date*.txt %%date.txt)

But I don't know how to do it :(.

Thanks for your help. If you need more information, please let me know.

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

Re: Concatenate files depending on their name

#2 Post by foxidrive » 23 Dec 2013 05:38

Test this on some sample files.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for %%a in (*_*_*.txt) do (
   for /f "tokens=1-4 delims=_." %%b in ("%%a") do (
      set "var=%%d"
      set "var=!var:~0,6!"
      if not exist "!var!.tmp" copy "*_!var!??????.txt" "!var!.tmp"
   )
)
ren *.tmp *.txt
pause

antonio.baena
Posts: 2
Joined: 23 Dec 2013 05:04

Re: Concatenate files depending on their name

#3 Post by antonio.baena » 23 Dec 2013 06:03

It worked! :D Can you explain me this line? please:

if not exist "!var!.tmp" copy "*_!var!??????.txt" "!var!.tmp"


Btw sometimes in the .txt appears an arrow (i cant copy it here). Do you know how to erase it?

Thank you so much.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Concatenate files depending on their name

#4 Post by Squashman » 23 Dec 2013 09:43

antonio.baena wrote:Btw sometimes in the .txt appears an arrow (i cant copy it here). Do you know how to erase it?

Thank you so much.

Do you mean the very last character in the file? The copy command will append an End of File character to the end of the file when it is combining multiple files together into one file.

You can actually use the /B option and it will keep the end of file character from appending to the end.

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

Re: Concatenate files depending on their name

#5 Post by foxidrive » 23 Dec 2013 09:57

antonio.baena wrote:Can you explain me this line? please:

if not exist "!var!.tmp" copy "*_!var!??????.txt" "!var!.tmp"


The loop takes every filename and extracts the date (6 characters0 into a variable named var.
If the file for that date has not been created then it will copy all files that match *_ with the date !var! and then 6 more characters (? is a wildcard) and with .txt on the end.
When the rest of the files for that date are processed, it will detect that the date.txt file has already been created and it will skip doing it again.

It uses .tmp as the extension to workaround a bug in for in do and then renames the files later.

@squashman Thanks for following up with the /b copy switch.

Post Reply