41

I've got 50 to 60 files in a directory that I need to concatenate into a single file on a regular basis.

I thought about using notepad++ thinking there was probably a plug-in that would help but haven't been able to find one.

Any other thoughts?

tnriverfish
  • 703
  • 2
  • 10
  • 19
  • Are you using Windows or Linux OS? if Windows, then do you have PowerShell installed? – del.ave Aug 05 '10 at 19:46
  • If you don't want this question closed, you had better edit it to ask "How can I write a program to merge files at a scheduled interval". We can easily help you write a small app to do this (batch, vb script, .net, etc)... but you'll need to move your question over to http://superuser.com if you want any recommendations on using third party apps to do the job for you. – Chase Florell Aug 05 '10 at 19:48
  • Concatenating files into one that's readable, or just for archival purposes? I personally routinely concatenate a large number of JPG images into a single ZIP file, using no compression, for easier transport and long-term storage. But you should indicate what kind of files, what operating system, and give some more details if you need programming help. – JYelton Aug 05 '10 at 19:53
  • For windows, make a powershell script and run `cat * > merge-file`. The command works in linux console too. More information here. http://unix.stackexchange.com/questions/3770/how-to-merge-all-text-files-in-a-directory-into-one – Larry Battle Mar 28 '13 at 16:20
  • Lots of great answers! sorry i lost track of this question and didnt mark one. I ended up writing something simple in excel vba. these all seem much more eloquent and efficient. Looks like several people have found this question and used the answers so win all around. – tnriverfish Jul 20 '15 at 13:00
  • @Larry, that solution infinitely re-includes the merge-file, in my case, so that it needed to be adapted to: cat *.txt > merge-file --but it doesn't respect utf-8 encodings, and butchers them into improper ASCII substitutes. Looking for a solution that converts as needed; even things thar didn't work: http://stackoverflow.com/questions/9824902/iconv-any-encoding-to-utf-8 – Alex Hall Jan 07 '16 at 01:52
  • could use `cat` if you install either unxutils / busybox / msysgit / cygwin / mingw / msys – n611x007 Jan 19 '16 at 08:44

10 Answers10

83

Assuming these are text files (since you are using notepad++) and that you are on Windows, you could fashion a simple batch script to concatenate them together.

For example, in the directory with all the text files, execute the following:

for %f in (*.txt) do type "%f" >> combined.txt

This will merge all files matching *.txt into one file called combined.txt.

For more information:

http://www.howtogeek.com/howto/keyboard-ninja/keyboard-ninja-concatenate-multiple-text-files-in-windows/

JYelton
  • 32,870
  • 25
  • 119
  • 184
  • Note that I probably wouldn't do this for very large or a very numerous amount of files. – JYelton Aug 05 '10 at 19:58
  • 1
    This is great if you want a lot of BOM (byte order marks) inserted into the combiled file (you probably don't!) – Darragh Jul 27 '11 at 11:55
  • That depends entirely on the nature and encoding of the files being joined. BOM's are optional in UTF8 and although some Windows apps are famous for adding these three bytes to the start of a file, Windows 7 Notepad for example, does not. However if they are present and causing an issue, there is a writeup here: http://www.heikniemi.net/hardcoded/2009/12/utf-8-preamble-is-a-problem-when-you-concatenate-files/ – JYelton Jul 27 '11 at 15:01
  • Thanks but why won't this script work if I paste this command in .bat file and double click and run it? but it works if I paste the command in cmd prompt window? – Mowgli Aug 29 '13 at 14:35
  • 7
    @Mowgli If you're running it from a batch file you need to double the % symbols. See http://stackoverflow.com/a/7769757/161052 – JYelton Aug 29 '13 at 16:48
  • Worked for me in a .bat with the tip of @JYelton ! Just a little warn : as is, it works in "append" mode, so if you start it multiple times, you'll get multiple times the same content in generated file. But that's not the simplest solution among all the answers of this SO question... – AFract Dec 15 '15 at 15:52
  • It crashed UTF-8 fonts for japanese characters. Copy *.txt combined.txt worked for me. – user1785594 Mar 06 '17 at 03:51
66

Use the Windows 'copy' command.

C:\Users\dan>help copy
    Copies one or more files to another location.

    COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [/A | /B]
         [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

      source       Specifies the file or files to be copied.
      /A           Indicates an ASCII text file.
      /B           Indicates a binary file.
      /D           Allow the destination file to be created decrypted
      destination  Specifies the directory and/or filename for the new file(s).
      /V           Verifies that new files are written correctly.
      /N           Uses short filename, if available, when copying a file with 
                   a non-8dot3 name.
      /Y           Suppresses prompting to confirm you want to overwrite an
                   existing destination file.
      /-Y          Causes prompting to confirm you want to overwrite an
                   existing destination file.
      /Z           Copies networked files in restartable mode.
      /L           If the source is a symbolic link, copy the link to the 
                   target
                   instead of the actual file the source link points to.

    The switch /Y may be preset in the COPYCMD environment variable.
    This may be overridden with /-Y on the command line.  Default is
    to prompt on overwrites unless COPY command is being executed from
    within a batch script.

    **To append files, specify a single file for destination, but 
    multiple files for source (using wildcards or file1+file2+file3 
    format).**

So in your case:

copy *.txt destination.txt

Will concatenate all .txt files in alphabetical order into destination.txt

Thanks for asking, I learned something new!

dwerner
  • 5,972
  • 3
  • 25
  • 41
  • 2
    No need for a PowerShell script afterall. Learned something new too. thanks. – del.ave Aug 05 '10 at 19:54
  • 3
    you can also specify order, according to that help description: copy file1.txt+file2.txt+file3.txt destination.txt – dwerner Aug 05 '10 at 20:01
  • 1
    this (identical answer?!) will (also) butcher e.g. utf8 encoding into unacceptable ANSI substitutes for many characters. – Alex Hall Jan 07 '16 at 01:45
12
copy *.txt all.txt

This will concatenate all text files of the folder to one text file all.txt

If you have any other type of files, like sql files

copy *.sql all.sql
Dan
  • 8,983
  • 5
  • 37
  • 71
abhishek
  • 129
  • 1
  • 2
7

Yes , A plugin is available named "combine" for notepad++.Link: .>> Combine Plugin for Notepad++

You can install it via plugin manager. Extra benifit of this plugin is: "You can maintain the sequence of files while merging, it's according to the sequence of opened files are opened (see tabs)".

Md. Monirul Alom
  • 273
  • 3
  • 13
1

In windows I use a simple command in a batch file and I use a Scheduled Task to keep all the info in only one file. Be sure to choose another path to the result file, or You will have duplicate data.

type PathToOriginalFiles\*.Extension > AnotherPathToResultFile\NameOfTheResultFile.Extension

If you need to join lots of csv files, a good thing to do is to have the header in only one file with a name like 0header.csv, or other name, so that it will allways be the first file in list, and be sure to program all the other csv files to not contain an header.

1

If you like to do this for open files on Notepad++, you can use Combine plugin: http://www.scout-soft.com/combine/

vahid kh
  • 1,706
  • 1
  • 11
  • 14
1

you could use powershell script like this

$sb = new-object System.Text.StringBuilder

foreach ($file in Get-ChildItem -path 'C:\temp\xx\') {
    $content = Get-Content -Path $file.fullname
    $sb.Append($content)
}
Out-File -FilePath 'C:\temp\xx\c.txt' -InputObject $sb.toString()
Iain
  • 6,060
  • 2
  • 28
  • 48
  • Did not work for me, had to change it to something like this (with added newline after each file):`foreach ($file in Get-ChildItem -path 'C:\temp\EXEMPLES_TEST') { $content = Get-Content -Path $file.fullname $content = $content + [Environment]::NewLine $content | Out-File 'C:\temp\EXEMPLES_TEST\combined.txt' -append }` – Janiek Buysrogge Apr 02 '15 at 09:50
0

I know this is an old post, but I found it and then found someone who suggested Total Mail Converter. I was able to convert my folder with 2k .msg files into .txt. It also allows you to convert into PDF and other popular formats.

It's a great tool that I am glad someone suggested as it will save me several days.

FYI - My project is combining the .msg files into one text file so that I can run a script to extract certain information from the files (ie: email and links). Instead of 2k files, I can work with one.

Osensnolf
  • 15
  • 5
0

I used this script on windows powershell:

ForEach ($f in get-ChildItem *.sql) { type "$f" >> all.sql }
asoifer1879
  • 111
  • 1
  • 5
0

There is a convenient third party tool named FileMenu Tools, that gives several right-click tools as a windows explorer extension.

One of them is Split file / Join Parts, that does and undoes exactly what you are looking for.

Check it at http://www.lopesoft.com/en/filemenutools. Of course, it is windows only, as Unixes environments already have lots of tools for that.

JProgrammer
  • 2,680
  • 23
  • 36
PPC
  • 1,572
  • 1
  • 17
  • 38