error appending file to itself

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Sponge Belly
Posts: 231
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

error appending file to itself

#1 Post by Sponge Belly » 05 Sep 2017 09:46

Hello All! :)

I recently noticed some unexplained behaviour when appending a file to itself using type (from the command line):

Code: Select all

<nul set /p "=###" >hashes.tmp
for /l %I in (1 1 8) do type hashes.tmp >>hashes.tmp


So far, so good. The file size is 768 bytes, as expected. But when I do this:

Code: Select all

type hashes.tmp >>hashes.tmp


The file size jumps to 1792 bytes instead of the expected 1536. :shock:

Where are those additional 256 bytes coming from? Only happens on the 9th iteration. And doesn’t happen if the file size is a power of 2 to begin with. :?

- SB

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: error appending file to itself

#2 Post by aGerman » 05 Sep 2017 10:24

I wouldn't have expected that it works. You append to the file while TYPE still reads. So in my opinion this should rather end up in an infinite loop. Seems that some kind of internal buffering protects you from terribly crashing. Depending on the buffer size you may get a file size that isn't a multiple of the former content.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: error appending file to itself

#3 Post by Squashman » 05 Sep 2017 11:19

aGerman wrote:I wouldn't have expected that it works. You append to the file while TYPE still reads. So in my opinion this should rather end up in an infinite loop. Seems that some kind of internal buffering protects you from terribly crashing. Depending on the buffer size you may get a file size that isn't a multiple of the former content.

Steffen

Dave introduced me to that technique in one of my threads.
viewtopic.php?f=3&t=2727&start=15#p12603

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: error appending file to itself

#4 Post by Squashman » 05 Sep 2017 11:26

I don't seem to have the problem when starting with a single character.

Code: Select all

C:\Users\Squashman\hashes><nul set /p "=A">hashes.tmp

C:\Users\Squashman\hashes>for /l %I in (1 1 9) do @type hashes.tmp >>hashes.tmp

C:\Users\Squashman\hashes>for %G in (hashes.tmp) do @echo %~zG
512

C:\Users\Squashman\hashes><nul set /p "=A">hashes.tmp

C:\Users\Squashman\hashes>for /l %I in (1 1 10) do @type hashes.tmp >>hashes.tmp

C:\Users\Squashman\hashes>for %G in (hashes.tmp) do @echo %~zG
1024

C:\Users\Squashman\hashes>@type hashes.tmp >>hashes.tmp

C:\Users\Squashman\hashes>for %G in (hashes.tmp) do @echo %~zG
2048

Seems like there is a 1024 byte buffer.

Code: Select all

C:\Users\Squashman\hashes><nul set /p "=A">hashes.tmp

C:\Users\Squashman\hashes>for /l %I in (1 1 10) do @type hashes.tmp >>hashes.tmp

C:\Users\Squashman\hashes>for %G in (hashes.tmp) do @echo %~zG
1024

C:\Users\Squashman\hashes><nul set /p "=A">>hashes.tmp

C:\Users\Squashman\hashes>for %G in (hashes.tmp) do @echo %~zG
1025

C:\Users\Squashman\hashes>@type hashes.tmp>>hashes.tmp

C:\Users\Squashman\hashes>for %G in (hashes.tmp) do @echo %~zG
2561

Post Reply