Quickest Way to Create Text File of Specific Length

In order to perform a specific test of my new application SetACL Studio I needed a string with a length of exactly 32,739 bytes.

How to Create Such a Beast

That was the question I asked myself. I was not thrilled by the prospect of copying and pasting blocks of text in an editor until the required length had been reached.


Luckily I remembered that Perl has a function that makes such a task trivial: the ‘x’ operator. Here is what I came up with:

perl -e "print '1' x 32739" > 32739.txt

If you have Perl installed, that line can be executed from the regular Windows command line. “Perl -e” executes one command string (enclosed in double quotes). The result is then written to the file 32739.txt with the command line’s output redirection operator “>”.


Are there any other elegant ways to create a text file with one line of exactly 32,739 characters? If you know of one, please let me know by commenting below.

13 Responses to Quickest Way to Create Text File of Specific Length

  1. Nils Kaczenski August 21, 2011 at 20:36 #

    Moin Helge,

    VBScript does not require any installation on a Windows machine and allows you to do pretty much the same.

    Put the following line into a file named, say, a.vbs:

    WScript.Echo String(32768, “N”)

    Now run this line from a CMD windows in the folder where a.vbs resides:

    cscript a.vbs //nologo > 32768.txt

    Almost as elegant as your Perl stuff (although Perl is more geeky just by itself) but requires nothing ouside standard Windows.

    Bye, Nils

    • Helge August 21, 2011 at 20:39 #

      Very nice, thanks!

      • Nils Kaczenski August 21, 2011 at 20:43 #

        I just made a one-liner out of it. That one even looks pretty cool I think. ;)

        echo WScript.Echo String (25, “N”) > tmp.vbs & cscript //nologo tmp.vbs > 25.txt & del tmp.vbs


        • Helge August 21, 2011 at 20:53 #

          Cool, I love one-liners ;-)

        • ildar September 11, 2011 at 08:06 #

          WScript.StdOut.Write usage is better than WScript.Echo because the Echo method appends new line characters to the output stream whereas the StdOut.Write method does not.

  2. frank vandebergh August 21, 2011 at 20:47 #

    And to complete:

    Set-Content -Value (new-object byte[] 1mb) -encoding byte -Path C:\file.txt

    Windows built-in:
    FSUTIL.Exe File CreateNew C:\file.txt 1024

    • Helge August 21, 2011 at 20:52 #

      Thanks, those two are very nice, too!

    • Jose Barreto August 21, 2011 at 21:46 #

      Simpler PowerShell version. You can simply “multiply” the character by a number and pipe to the file:

      “1” * 32739 > 32739.txt

      However, since powershell uses unicode, you get a file with twice the size you expect. You can fix by using:

      “1” * 32739 | out-file 32739.txt -Encoding UTF8

      • Helge August 21, 2011 at 21:52 #

        Nice! Funnily almost identical to Holger’s solution (which you could not see because I had not yet approved it).

  3. Holger August 21, 2011 at 21:28 #

    “x” * 32737 | out-file “32739.txt” -encoding ASCII

    You can use add-content instead of out-file as well. However both add a line break at the end, hence the 2 bytes less than desired in the multiplication. Encoding is set to ASCII to ensure one byte per character.

    • Helge August 21, 2011 at 21:51 #

      Very cool!

    • jgreasy October 22, 2012 at 16:07 #


  4. AlexII September 10, 2011 at 23:26 #

    Simple, but long-time command for NT-based command processor:

    >nul copy nul 32739.txt & for /l %i in (1, 1, 32739) do @(>32739.txt

    The brackets need only for prevent redirection in this case (as … 1>>file.txt).

    This way is more easy and quickly:

    (for /l %i in (1, 1, 32739) do @32739.txt

Leave a Reply