2

I have multiple .txt files that are being created as outputs from a batch file. When I open them in notepad they all show up correctly, in English. However, if I run:

type file1.txt >> file2.txt

the appended text from file1 shows up as not readable characters within file2. Changing the font does not help.

Is this some kind of encoding that needs to be changed?

How do I get this to append with the appearance and format of the original file?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
LuckyLee
  • 25
  • 1
  • 3
  • 2
    Try: `cmd /U type file1.txt >> file2.txt` – Aacini Jul 15 '14 at 18:19
  • When i did that it just appended the actual command prompt to the file: `_TEXTFROMFILE2_Microsoft Windows [Version 6.2.9200] (c) 2012 Microsoft Corporation. All rights reserved. C:\Users\Tech>` – LuckyLee Jul 15 '14 at 18:29
  • EXCUSE ME! /C option is missed!!! `cmd /U /C type file1.txt >> file2.txt` – Aacini Jul 15 '14 at 18:38
  • Ha! yeah I added the /Q and it worked but then saw that I needed to add the /c. working great now! Thanks! – LuckyLee Jul 15 '14 at 19:35

1 Answers1

4

This question is similar to cmd is somehow writing chinese text as output.

The text files are Unicode text files, most likely with UTF-16 little endian encoding.

To get Unicode text file file1.txt correct appended to Unicode text file file2.txt using command type it is necessary to start command line interpreter cmd.exe with the switch /U to get output of internal commands redirected into a file or piped to another command in Unicode format.

As Aacini suggested already correct, the command line to use is

cmd /U /C type file1.txt >> file2.txt

Another method for concatenating 2 or more text files is to use the copy command.

copy "Text File 1.txt" + "Text File 2.txt" + "Text File 3.txt" "Output File.txt"

It is not necessary to call cmd.exe with switch /U in this case even if the text files are encoded in UTF-16 instead of ANSI.

The command copy automatically removes byte order mark (BOM) of Text File 2.txt and Text File 3.txt if all 3 text files are Unicode text file with a BOM. Output File.txt contains in this case just once the BOM at beginning.

copy *.txt "Output File.txt"

concatenates all text files in a directory to a file with name Output File.txt.

Note 1:
NTFS file system driver returns the list of *.txt files in a directory always in alphabetic order (not alphanumeric!) according to file name. Therefore the file names determine on NTFS partitions the order of concatenation of the text files. But FAT32 file system driver returns the list of *.txt files in a directory according to listing in the file allocation table which results in an unpredictable order of concatenation of the text files on a FAT32 partition if using above command with *.txt.

Note 2:
It is important on usage of command copy that all text files have the same encoding, i.e. all text files are UTF-16 encoded or all text files are ANSI encoded.

Note 3:
Command copy can be also used to concatenate binary files. Enter in a command prompt window copy /? or help copy for a short help explaining also the optional switches /B (binary file) and /A (ASCII/ANSI text file).

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143