1

I want to merge multiple (text) files having the same prefix into a single file. This task is part of a whole batch (in VBScript) but due to performance expectations, I'm more likely to use a native command rather than programming a loop (opening each file, reading the whole content, then writing it down to the destination file) in VBScript.

Currently facing a problem with COPY which seems to require you to flush the StdOut buffer (because it outputs all files' names it copies) I was looking towards XCOPY which has a lot more flags to configure its behavior and output (like /Q which Suppresses the display of xcopy messages).

Reading the doc: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb491035(v=technet.10)

I came across this:

Appending files

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

OK so I write: xcopy "My file pattern whatsoever.*.tmp" "My destination file.tmp"

Unfortunately it asks if My destination file.tmp is a file or a directory, to which I should answer with either F (for file) or D (for Directory). Note: it's language dependent (in my case - French - it's F or R).

I noticed this in the above doc:

Specifying whether Destination is a file or directory

If Destination does not contain an existing directory and does not end with a backslash (\), the following message appears:

Does destination specify a file name 
or directory name on the target 
(F = file, D = directory)? 

Press F if you want the file or files to be copied to a file. Press D if you want the file or files to be copied to a directory.

You can suppress this message by using the /i command-line option, which causes xcopy to assume that the destination is a directory if the source is more than one file or a directory.

Well, as this /i flag does not fit my need, I resort to a trick I read here & there, like (amongst answers): https://stackoverflow.com/a/45134466

So I try this: echo F|xcopy "My file pattern whatsoever.*.tmp" "My destination file.tmp"

OK now, the first file is copied to the destination file, but for every following file, a question pops up about replacing the destination file (?!!). By the way all are discarded because the echo command does not provided any input for them.

If I use this flag:

/y : Suppresses prompting to confirm that you want to overwrite an existing destination file.

then I end up with a destination file containing only the content of the last file I want to merge (i.e. the last file matching the source pattern)!!!

I also tried checking if it works with only two files, e.g.: xcopy "My file pattern whatsoever.0001.tmp"+"My file pattern whatsoever.0002.tmp" "My destination file.tmp" but I get an error: File not found - My file pattern whatsoever.0001.tmp+My file pattern whatsoever.0002.tmp`

Same if I add spaces around the +; says Invalid number of parameters, probably because the file1+file2+file3 syntax doesn't work with long (including spaces) filenames, as suggested here: https://stackoverflow.com/questions/30651776/xcopy-returns-error-invalid-number-of-parameters-when-exclude-parameter-is-set

So what is wrong with what am I doing (or trying to)?

1
  • Note: I also tested the * trick at the end of destination to avoid the /i + echo F| like advised here stackoverflow.com/a/50451992 - and it works - but that doesn't help with the desired goal of merging all source files into 1.
    – maxxyme
    Jul 10, 2019 at 14:38

1 Answer 1

2

According to some of the comments on this Stack Overflow question, xcopy doesn't do the appending that its documentation says it does.

So you can use something like this instead.

type "My file pattern whatsoever.*.tmp" > "My destination file.tmp"

But type also outputs the filenames as it processes them, so you might run into the same problem that you are having with copy. To avoid that, you can redirect that unwanted output to NUL, like this:

type "My file pattern whatsoever.*.tmp" > "My destination file.tmp" 2>NUL

(Using 2>NUL, because type displays the filenames on StdErr.)

And if you are doing that anyway, you can go back to using copy, like this:

copy "My file pattern whatsoever.*.tmp" "My destination file.tmp" >NUL

(Using >NUL, because copy displays the filenames on StdOut.)

4
  • Thanks. Indeed I read this question earlier but as there wasn't any clear answer/explanation about why xcopy does not append/merge, I decided to ask myself. At the same time I tried to fix the copy issue and finally resorted to use this > NUL redirection. BTW do you know if there is an advantage to use type over copy or that's basically the same?
    – maxxyme
    Jul 11, 2019 at 8:35
  • Also I didn't pay attention to a link that was posted in the comments; in fact it's now 404 but with the help of Archive.org I could find what it was before, and the answer is explicit: XCOPY does not merge files due to (so-called) compatibility issues. See my comment here: stackoverflow.com/questions/25548431/…
    – maxxyme
    Jul 11, 2019 at 8:52
  • 1
    As for advantages, type and copy are going to be the same for most situations. Copy can copy binary files or text files, while type will only work well with text files. And I've seen discussion about type being able to read locked files, while copy can not. But in my testing, neither one can read a locked file.
    – Doug Deden
    Jul 11, 2019 at 12:31
  • OK thanks again. For what I'm dealing with, I have to use the /B flag because some of my files are in UTF-8 thus the default copy mode doesn't handle encoding properly e.g. adding a SUB character at the end of the resulting file.
    – maxxyme
    Jul 11, 2019 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .