Question

With windows' cmd.exe, when you run a command like

output.exe > out.txt

with an existing, non-empty file 'out.txt' what actually happens?

Is a new 'out.txt' created while the old one is deleted in 'rm out.txt' fashion?
I assume it doesn't literally write over the old 'out.txt', because that seems possibly dangerous.

P.S. Could you use cmd.exe commands to write over the old file's data specifically?

i.e.

>type out.txt
This is a sentence.  
>strangeEchoes That > out.text
>type out.txt
That is a sentence.

P.P.S

Information is always welcome, the more and longer-winded the better =)

P.P.P.S.

Sorry if I'm bad at searching for answers / asking questions.

Was it helpful?

Solution

Based off a Process Monitor trace, when echo text > filename.txt is ran, a call is made to CreateFile with the dwCreationDisposition flag set to CREATE_ALWAYS, which

Creates a new file, always.

If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183).

If the specified file does not exist and is a valid path, a new file is created, the function succeeds, and the last-error code is set to zero.

echo demo2 >> filename.txt, on the otherhand, calls CreateFile with CREATE_NEW and opens the existing file. The process then gets the end of the file, reads one byte from the end and starts writing thereafter. I would guess it is looking for a ^Z character at the end which it needs to overwrite when it reads the last character, but that is just a guess.

OTHER TIPS

append the output to an existing file:

>>out.txt output.exe
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top