Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a file of a specified size using a script?

-- RE

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RE. Well, it’s day 2 of the Walla Walla Invitational Baseball tournament, but at the moment the Scripting Guy who writes this column isn’t thinking about baseball; instead, he’s getting ready to head out to the Walla Walla Regional Airport and Industrial Park. Why is he getting ready to head out to the Walla Walla Regional Airport and Industrial Park? That’s easy: what visit to Walla Walla would be complete without a trip to the Walla Walla Regional Airport and Industrial Park?

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’ll see.

Note. At least he won’t have to worry about parking. At the Walla Walla airport you can park in long-term parking for a maximum of two weeks, absolutely free. By comparison, this same Scripting Guy paid over $50 to park his car for 5 days near (but not exactly at) 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’s pretty clear who’s getting the last laugh here.

While we have a few minutes, however, we’ll see if we can answer RE’s 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:

Const ForWriting = 2

intBytes = InputBox("Enter the size of the file, in bytes:", "File Size")

intBytes = intBytes / 2

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.CreateTextFile _
    ("Testfile.txt", ForWriting, True)

For i = 1 to intBytes
    objFile.Write "."
Next

objFile.Close

Note. What do we mean by “pretty darn close?” 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’re 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’t seem worth it.)

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’t 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 – voila! – a file of the desired size.

That’s the general notion behind the script; more specifically, here’s how the code actually works. We start out by creating a constant named ForWriting, assigning ForWriting the value 2; we’ll 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:

intBytes = InputBox("Enter the size of the file, in bytes:", "File Size")

Keep in mind that we didn’t 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’t provide a way to cancel the script; clicking the Cancel button will simply cause the script to fail. If you’d 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:

intBytes = InputBox("Enter the size of the file, in bytes:", "File Size")

If intBytes = "" Then
    Wscript.Quit
End If

If you take a peek at the first line of code, the one that calls the InputBox function, you’ll 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:

intBytes = intBytes / 2

At this point we’re ready to create the file. For starters, we create an instance of the Scripting.FileSystemObject object; we then use this line of code to create a new text file:

Set objFile = objFSO.CreateTextFile _
    ("Testfile.txt", ForWriting, True)

As you can see, we’re using the CreateTextFile method, passing this method three parameters:

Testfile.txt, the path to the file we want to create. (Because we didn’t specify a complete path, the file will be created in the same folder in which our script lives.)

ForWriting, the constant that tells the script we’re going to write to this new file.

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’t exist then the script will create it.

Once we have a file to work with we can then use this block of code to make it the desired size:

For i = 1 to intBytes
    objFile.Write "."
Next

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:

................................................................................
................................................................................
................................................................................
................................................................................
................................................................................

Admittedly, the resulting file isn’t the most exciting thing we’ve ever read. Still, it’s at least a step or two ahead of most Hey, Scripting Guy! columns.

As soon as we’ve written the last period to the file we exit the loop, call the Close method and, for good measure, call it a day as well.

Full disclosure: we haven’t tested this script on all platforms. That means that, as the saying goes, your mileage may vary. On top of that, we didn’t 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.

Pretty darn close.

And now it’s time to leave for the airport. Although he doesn’t 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’s 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.

Well, except during the holiday rush. During those times they recommend you arrive an hour early.

Like we said, it’s all-too-obvious who’s getting the last laugh here.