Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers

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? –  ldigas 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 ... –  ldigas 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
1  
@Idigas, did you put a space between echo and the dot? –  Synetech Feb 24 '12 at 4:16
add comment

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
or
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
show 1 more comment

This should work:

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

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. –  ldigas Feb 24 '12 at 4:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.