{"id":64453,"date":"2007-07-14T00:50:00","date_gmt":"2007-07-14T00:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/14\/how-can-i-create-a-file-of-a-specified-size\/"},"modified":"2007-07-14T00:50:00","modified_gmt":"2007-07-14T00:50:00","slug":"how-can-i-create-a-file-of-a-specified-size","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-file-of-a-specified-size\/","title":{"rendered":"How Can I Create a File of a Specified Size?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I create a file of a specified size using a script?<BR><BR>&#8212; RE <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RE. Well, it\u2019s day 2 of the Walla Walla Invitational Baseball tournament, but at the moment the Scripting Guy who writes this column isn\u2019t thinking about baseball; instead, he\u2019s getting ready to head out to the Walla Walla Regional Airport and Industrial Park. <I>Why<\/I> is he getting ready to head out to the Walla Walla Regional Airport and Industrial Park? That\u2019s easy: what visit to Walla Walla would be complete without a trip to the Walla Walla Regional Airport and Industrial Park?<\/P>\n<P>Actually, the airport looks pretty nice, at least judging from the pictures found on its Web site. The only bad thing about this side trip is the fact that Friday is one of the busy days at the Walla Walla airport. Today there are a total of six flights arriving and departing; on most days there are only four flights. Hopefully the Scripting Guy who writes this column will be able to fight through the traffic and find his passenger with a minimal amount of difficulty. We\u2019ll see.<\/P>\n<TABLE class=\"dataTable\" id=\"EAD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. At least he won\u2019t have to worry about parking. At the Walla Walla airport you can park in long-term parking for a maximum of two weeks, <I>absolutely free<\/I>. By comparison, this same Scripting Guy paid over $50 to <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/monday.mspx\"><B>park his car<\/B><\/A> for 5 days near (but not exactly <I>at<\/I>) Sea-Tac Airport in Seattle. The Scripting Guy who writes this column might be poking a little good-natured fun at the Walla Walla airport, but it\u2019s pretty clear who\u2019s getting the last laugh here.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>While we have a few minutes, however, we\u2019ll see if we can answer RE\u2019s question. How can you create a file of a specified size using a script? The following script comes pretty darn close to achieving those results:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>intBytes = InputBox(&#8220;Enter the size of the file, in bytes:&#8221;, &#8220;File Size&#8221;)<\/p>\n<p>intBytes = intBytes \/ 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.CreateTextFile _\n    (&#8220;Testfile.txt&#8221;, ForWriting, True)<\/p>\n<p>For i = 1 to intBytes\n    objFile.Write &#8220;.&#8221;\nNext<\/p>\n<p>objFile.Close\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EZD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. What do we mean by \u201cpretty darn close?\u201d Well, consider this: when we asked the script to create a file of 33,000 bytes, the actual file size turned out to be 33,002 bytes. A requested file of 100,000 bytes came out to 100,002 bytes. That seemed pretty darn close, to say the least. (We\u2019re assuming the extra 2 bytes represents the end-of-file marker. And sure, we could probably adjust the script to account for those 2 bytes. But it didn\u2019t seem worth it.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how does our script work its magic? Well, originally it was based on a pretty simple idea, the notion that a single period typed into a text file would be 1 byte worth of data (and thus create a file with a file size of 1 byte). As it turned out, that didn\u2019t work: each file we created ended up being twice as big as we wanted it to be. But that was OK: all we had to do was divide by 2 and \u2013 voila! \u2013 a file of the desired size.<\/P>\n<P>That\u2019s the general notion behind the script; more specifically, here\u2019s how the code actually works. We start out by creating a constant named ForWriting, assigning ForWriting the value 2; we\u2019ll use this constant when we set out to create our text file. We then use this block of code to pop up an input box asking the user to enter the desired file size, in bytes:<\/P><PRE class=\"codeSample\">intBytes = InputBox(&#8220;Enter the size of the file, in bytes:&#8221;, &#8220;File Size&#8221;)\n<\/PRE>\n<P>Keep in mind that we didn\u2019t include any error handling in this script; as a result, the script will fail if someone enters anything other than a number. In addition, we didn\u2019t provide a way to cancel the script; clicking the <B>Cancel<\/B> button will simply cause the script to fail. If you\u2019d prefer a more graceful way for people to exit the script then modify the code so it looks like this, including the additional If-Then block:<\/P><PRE class=\"codeSample\">intBytes = InputBox(&#8220;Enter the size of the file, in bytes:&#8221;, &#8220;File Size&#8221;)<\/p>\n<p>If intBytes = &#8220;&#8221; Then\n    Wscript.Quit\nEnd If\n<\/PRE>\n<P>If you take a peek at the first line of code, the one that calls the <B>InputBox<\/B> function, you\u2019ll see that the desired file size is stored in a variable named intBytes. Because that results in files twice as big as we want we next use this line of code to divide the value intBytes by 2:<\/P><PRE class=\"codeSample\">intBytes = intBytes \/ 2\n<\/PRE>\n<P>At this point we\u2019re ready to create the file. For starters, we create an instance of the <B>Scripting.FileSystemObject<\/B> object; we then use this line of code to create a new text file:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.CreateTextFile _\n    (&#8220;Testfile.txt&#8221;, ForWriting, True)\n<\/PRE>\n<P>As you can see, we\u2019re using the <B>CreateTextFile<\/B> method, passing this method three parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Testfile.txt, the path to the file we want to create. (Because we didn\u2019t specify a complete path, the file will be created in the same folder in which our script lives.)<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>ForWriting, the constant that tells the script we\u2019re going to write to this new file.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>True, which tells the script to overwrite any existing instances of Testfile.txt. If Testfile.txt already exists the script will overwrite it; if Testfile.txt doesn\u2019t exist then the script will create it.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Once we have a file to work with we can then use this block of code to make it the desired size:<\/P><PRE class=\"codeSample\">For i = 1 to intBytes\n    objFile.Write &#8220;.&#8221;\nNext\n<\/PRE>\n<P>Nothing too fancy here; this is simply a For Next loop that starts at 1 and runs through the value of the variable intBytes. Each time through the loop all we do is tack a period onto the end of the file; when all is said and done, that means Testfile.txt is going to look something like this:<\/P><PRE class=\"codeSample\">&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..\n&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..\n&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..\n&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..\n&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..\n<\/PRE>\n<P>Admittedly, the resulting file isn\u2019t the most exciting thing we\u2019ve ever read. Still, it\u2019s at least a step or two ahead of most <I>Hey, Scripting Guy!<\/I> columns.<\/P>\n<P>As soon as we\u2019ve written the last period to the file we exit the loop, call the <B>Close<\/B> method and, for good measure, call it a day as well.<\/P>\n<P>Full disclosure: we haven\u2019t tested this script on all platforms. That means that, as the saying goes, your mileage may vary. On top of that, we didn\u2019t test this with all possible file sizes, either. However, we did try creating a 10,000,000 byte (approximately 10-megabyte) file. The end result: a file with a file size of 10,000,002 bytes.<\/P>\n<P>Pretty darn close.<\/P>\n<P>And now it\u2019s time to leave for the airport. Although he doesn\u2019t exactly qualify as a world traveler, the Scripting Guy who writes this column is definitely more familiar with larger airports like Sea-Tac. At Sea-Tac, for example, it\u2019s suggested that you arrive at least two hours before departure time, with three hours considered a more reasonable recommendation. At the Walla Walla airport they recommend that you arrive 45 minutes before departure time. <\/P>\n<P>Well, except during the holiday rush. During those times they recommend you arrive an hour early.<\/P>\n<P>Like we said, it\u2019s all-too-obvious who\u2019s getting the last laugh here.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a file of a specified size using a script?&#8212; RE Hey, RE. Well, it\u2019s day 2 of the Walla Walla Invitational Baseball tournament, but at the moment the Scripting Guy who writes this column isn\u2019t thinking about baseball; instead, he\u2019s getting ready to head out to the Walla [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,3,12,5],"class_list":["post-64453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a file of a specified size using a script?&#8212; RE Hey, RE. Well, it\u2019s day 2 of the Walla Walla Invitational Baseball tournament, but at the moment the Scripting Guy who writes this column isn\u2019t thinking about baseball; instead, he\u2019s getting ready to head out to the Walla [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}