I am trying to merge multiple text files with the same prefix into a single file using a native command in VBScript, rather than writing a loop in VBScript to open each file, read its content, and write it to the destination file. I am having issues with the COPY command, which seems to require flushing the StdOut buffer because it outputs the names of all the files it copies. I am considering using the XCOPY command because it has more flags to configure its behavior and output, such as the /Q flag which suppresses the display of XCOPY messages.

According to the documentation for XCOPY, it is possible to append files by specifying a single file for the destination and multiple files for the source, using wildcards or the “file1+file2+file3” format. However, when I try this using the following command: xcopy "My file pattern whatsoever.*.tmp" "My destination file.tmp", XCOPY prompts me to specify whether the destination is a file or directory. I can suppress this message by using the /i flag, but this does not meet my needs.

Instead, I have tried using the following command: echo F|xcopy "My file pattern whatsoever.*.tmp" "My destination file.tmp". This successfully copies the first file to the destination, but for each subsequent file, a prompt appears asking if I want to replace the destination file. Additionally, the echo command does not provide any input for these prompts, causing all subsequent files to be discarded.

If I use the /y flag to suppress the prompt to overwrite the destination file, the destination file will only contain the content of the last file that I am trying to merge. I have also tried using the “file1+file2+file3” syntax with only two files, using the command xcopy "My file pattern whatsoever.0001.tmp"+"My file pattern whatsoever.0002.tmp" "My destination file.tmp", but this results in an error saying “File not found – My file pattern whatsoever.0001.tmp+My file pattern whatsoever.0002.tmp”. It seems that this syntax does not work with long filenames, including those with spaces.

I am not sure what is causing these issues with my attempts to merge the files.

Askify Moderator Edited question April 16, 2023