Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to append the contents of one text file (file1.txt) to another (file2.txt), and to delete the contents of the first file but still keeping it empty using command line tools?

Is there a way to do it, or do I have to resort to other means?

Ideally, it would be best if it could be done using command line tools on Windows XP platform.

share|improve this question
up vote 1 down vote accepted
C:\>type file1.txt >>file2.txt <ENTER>
C:\>echo.>file1.txt  <ENTER>

only thing is file1.txt has a new line in it and is not 0 bytes.

share|improve this answer
file1.txt has a dot inside? – Rook Feb 24 '12 at 4:04
no, the dot there is not literal. "echo." makes a new line. echo with nothin to echo, i.e. just plain "echo", would output "Echo is on." and funnily enough you can use some other punctuation marks instead of dot such as ( or = or / – barlop Feb 24 '12 at 4:09
No, it is quite literal in my case. Upon opening a file with an editor, there is a dot inside. Dot, as in full stop. But, never mind. I think it solved my problem ... – Rook Feb 24 '12 at 4:10
@ldigas strange, when I at the cmd line in xp do echo.. it displays .(dot) but echo. (echodot) just does a new line. – barlop Feb 24 '12 at 4:13
@Idigas, did you put a space between echo and the dot? – Synetech Feb 24 '12 at 4:16

To append it, you can do as barlop suggested if they are plain text:

C:\> echo.>> file2.txt     :: If you want to add a blank line between them
C:\> type file1.txt >> file2.txt

Or if it is binary and/or has any non-text characters in it (eg Unicode, UTF-8, etc.):

C:\> copy /b file1.txt + file2.txt file.tmp
C:\> del file1.txt
C:\> ren file.tmp file1.txt

Now a reliable way (in DOS and Windows at least up to 7) to zero out the second file:

C:\> ren > file2.txt
C:\> ren > file2.txt 2> nul   :: to hide the error message (Windows only)
share|improve this answer
that ren line gives "The syntax of the command is incorrect." and if you meant rem > file2 that doesn't create the file. doesn't work, though also, I hadn't heard of it either.. You're probably right given that you can write an exe in assembly language in notepad.. so be interesting to see how to zero out the file with that line! – barlop Feb 24 '12 at 14:19
Nope, it’s ren not rem. You can hide the error message by redirecting it to nul. – Synetech Feb 24 '12 at 17:12
amazing, where did you read/hear about that? or did you discover it yourself by accident? – barlop Feb 24 '12 at 17:59
No idea. It’s just something I’ve been doing forever. I even wrote a little program (newfile.exe) years ago that simply creates a file (or zeros-out an existing one), and yet I always seem to catch myself using ren instead. – Synetech Feb 24 '12 at 18:10
+1 particularly for the use of ren! and if you remembered how you ran into or discovered it, and I could, i'd give another +1 for it! – barlop Feb 24 '12 at 18:19

This should work:

type file1.txt >> file2.txt && echo. > 1.txt
share|improve this answer

How about just use unixutils for windows.

And then do the append/touch stuffs just like you work on unix.

share|improve this answer
I can resort to DOS, but that's it. Specifically asked for cmd or DOS, for a reason. – Rook Feb 24 '12 at 4:05

You must log in to answer this question.

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