Hey, Scripting Guy! Question

Hey, Scripting Guy! For testing purposes I need to create a bunch of empty .WAV files in a folder. How can I write a script that will create those files for me?

-- ZL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, ZL. You know, yesterday was a very exciting day for the Scripting Guy who writes this column: he got to work from home, just like Scripting Guy Jean Ross does on a regular basis. (Well, OK, we’re not sure that she actually works from home. But that’s what she says she does.) Because the Scripting Guys are manning a booth in the Ask the Experts section at TechEd IT Forum (booth 22; stop by, pick up a Fun Book, and maybe win a Dr. Scripto bobblehead doll), it was suggested that they have a cell phone so that the event organizers could reach them as needed.

Note Do event organizers expect that, come 2:00 AM, one of the conference attendees might desperately need a script that can retrieve a list of processes running on a remote computer? Or maybe late one night another attendee will be unable to remember which positions the Scripting Son plays on his baseball team? (Pitcher and first base.) Well, we aren’t sure. But no point in taking any chances, right? Regardless, the Scripting Guys will be on call, 24/7.

Anyway, neither of the Scripting Guys has a cell phone that will work in Europe. (Which gives you an idea as to how much world traveling the Scripting Guys are typically allowed to do.) That meant they had to rent a cell phone, a phone that would be shipped to the home of the Scripting Guy who writes this column. And since someone had to be there to sign for the phone when it was delivered, well ….

So what’s it like to work from home? Well, you end up sleeping in later than you normally would, then you eat breakfast and read the paper. You log on to the network, complaining all the while about how slow everything is going. You read your email, then break for lunch. And because you were reading a magazine while you were eating, you go ahead and finish the magazine once lunch is over. After lunch you’re feeling restless, so you decide to knock off early, go to the gym, and get in a good 40 minutes on the exercise bike. You get home about the same time the Scripting Son does, and the two of you watch ESPN and then get ready to leave for a Husky basketball game.

Now that we think about it, working at home isn’t all that much different than working at work, at least for some us.

Oh, wait. Due to the fact that there isn’t much on TV during the day, the Scripting Guy who writes this column did manage to do one other thing while working at home. (Well, two, if you count running the dishwasher.) He also managed to write a script that can create a bunch of empty .WAV files in a folder:

Set objFSO = CreateObject("Scripting.FileSystemObject")

For i = 1 to 100
    strFileName = objFSO.GetTempName
    strFileName = Replace(strFileName, ".tmp", ".wav")
    strPath = "C:\Temp\" & strFileName
    objFSO.CreateTextFile(strPath)
Next

You know, come to think of it, he did three things: he also wrote an explanation of how the script works. (No wonder he’s so tired; maybe he should take tomorrow off.) We kick things off – speaking of kicking off, did we mention that the University of Washington finally won a football game last weekend, knocking off the Stanford Cardinal? Go Huskies!

Anyway, we kick things off by creating an instance of the Scripting.FileSystemObject. What are we going to do with the FileSystemObject? You’ll find out soon enough.

And yes, that is soon enough. Trust us.

We’ve decided that, for demonstration purposes, we want to populate the folder C:\Temp with 100 empty. WAV files. With that in mind, our next chore is to set up a For Next loop that runs from 1 to 100; that’s what we do here:

For i = 1 to 100

When we finally get around to creating a new file, we’re going to need to specify a complete path for that file, including file name. Step 1 in the process of creating that file path is to use the GetTempName method to create a random name for our file:

strFileName = objFSO.GetTempName

GetTempName has only one purpose in life: it generates random files names. (The “Temp” in GetTempName refers to the fact that these random names are often affixed to temporary files.)

When all is said and done, GetTempName will have cranked out 100 files names similar to these:

rad0F25C.tmp
rad2A9FD.tmp
radEA60F.tmp

What’s that? Hmmm, now that you mention it, those files do all have .TMP file extensions, don’t they? And that’s a problem; we need to have a bunch of files with a .WAV file extension.

Fortunately for us, we included this line of code in our For Next loop:

strFileName = Replace(strFileName, ".tmp", ".wav")

What we’re doing here is using VBScript’s Replace function to replace any instance of the string value .tmp (which just happens to coincide nicely with the file extension for each of these files) with the string value .wav. Just like that, we’ll end up with files that have names similar to these:

rad0F25C.wav
rad2A9FD.wav
radEA60F.wav

Once we have valid file names we can then use this line of code to construct the complete file path:

strPath = "C:\Temp\" & strFileName

Again, there’s nothing very fancy going on here. We want to create all these empty .WAV files in the folder C:\Temp. Therefore, to construct a path for the new file we simply combine C:\Temp\ with the name of the first file generated by GetTempName. That’s going to result in the variable strPath being equal to something like this:

C:\Temp\rad0F25C.wav

If that doesn’t look like a valid path, well, we don’t know what does.

Now all we have to do is call the CreateTextFile method (passing it the variable strPath) and we can create the first of our empty .WAV files:

objFSO.CreateTextFile(strPath)

Now, we know what you’re thinking: we wanted .WAV files and yet we called the CreateTextFile method? What gives?

Well, what gives is that we aren’t trying to create valid .WAV files; that is, we aren’t trying to create actual sound files that can be played in Windows Media Player. Instead, we’re creating “empty” .WAV files, files that are sound files only in the sense that they have a .WAV file extension. And we can do that using the CreateTextFile method because this method doesn’t care what file extension we use; after all, even actual text files don’t always have a .TXT file extension. (They might be .CSV files, .XML files, .HTM files, etc.) If you need specific types of files in order to test a script (for example, you’re trying to write a script that will delete only the .DOC files from a folder), well, go ahead and give CreateTextFile a file name that includes the desired file extension. CreateTextFile will take it from there.

We hope that helps, ZL. We also hope that everyone who attends TechEd IT Forum will swing by the Ask the Experts section (booth 22) and say hi. (Well, OK. But at least drop by to pick up a Fun Book and to try your luck at winning a Dr. Scripto bobblehead.) And remember, if you need anything at all during IT Forum, any time of the day or night, just give Scripting Guy Jean Ross a call. She’ll be more than happy to help you out.