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.
Perl
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 “>”.
Alternatives?
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 Comments
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
Very nice, thanks!
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
Nils
Cool, I love one-liners ;-)
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.
And to complete:
powershell:
Set-Content -Value (new-object byte[] 1mb) -encoding byte -Path C:\file.txt
Windows built-in:
FSUTIL.Exe File CreateNew C:\file.txt 1024
Thanks, those two are very nice, too!
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
Nice! Funnily almost identical to Holger’s solution (which you could not see because I had not yet approved it).
PowerShell:
“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.
Very cool!
+1
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