Hey, Scripting Guy! How can I write a script that will clear the contents of any file that I drag and drop on the script icon?
Hey, SN. Say, you didn’t by any chance misplace a few hundred thousand insects did you? OK; just thought we’d ask. This past weekend the Scripting Guy who writes this column had to drive across the state to attend a wedding. As the family was tooling along, on a bright, sunny day, without a cloud in the sky, it suddenly started to rain. Or maybe it was hail. At first all they knew was that – out of the blue – the windshield was being pelted by … something. “How can it rain when there aren’t any clouds in the sky?” asked the Scripting Son.
“That’s not rain,” said the all-wise and all-knowing Scripting Dad. “Those are bugs.”
Sure enough, the Scripting Windshield had been assaulted by thousands and thousands of kamikaze bugs, with so many hitting the windshield it sounded like raindrops (although after a minute or so all you had to do was look at the windshield and you could tell that it wasn’t raining). Definitely one of the most surreal experiences the Scripting Guy who writes this column has been through lately.
Which is saying quite a bit considering that he works at Microsoft.
At any rate, SN, if you did misplace a bunch of insects, well, we know where you can find them.
Or at least what’s left of them.
Note. OK, technically we did just plow through the mass of insects without trying to avoid them; to make matters worse, we then continued driving without stopping to help the survivors. Cruel and callous? Maybe. But we’re not going to apologize for that. After all, they started it.
Of course, we can’t be sure that those were your bugs, SN; it was hard to read their little nametags at 70 miles per hour. If they were yours, however, well, we’d like to try and make it up to you. What do you say we show you a script that can erase the contents of any file that is dragged and dropped on the script icon? Would that help?
Let’s hope so:
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each strArgument in Wscript.Arguments
Set objFile = objFSO.OpenTextFile(strArgument, ForWriting)
As you can see, there’s really not much to the script. (But, then again, it’s not like we ran over 100,000 dogs or cats or something.) We start off by defining a constant named ForWriting and setting the value to 2; we’ll use this constant when we open the file (or files) that have been dropped on the script icon. Once we’ve defined our constant we then create an instance of the Scripting.FileSystemObject, the scripting technology of choice (OK, the scripting technology of necessity) when it comes to working with text files. (We’re assuming you want to erase text files, a topic we’ll revisit in a few minutes.)
Now comes the fun part. One of the more interesting – albeit underutilized – features of Windows Script Host is this: if you drag and drop a file (or group of files) on a script icon (that is, on the .VBS file in Windows Explorer) the complete path name for any dropped file is automatically passed to the script as a command-line argument. That means that the script knows exactly which files have been dragged and dropped on the script icon. That capability turns out to be the secret ingredient for today’s script.
With that in mind, we next set up a For Each loop that loops through the Wscript.Arguments collection, the collection of command-line arguments passed to the script. Inside that loop we use the OpenTextFile method to, one-by-one, open each of the files that were dragged on top of our script icon:
Set objFile = objFSO.OpenTextFile(strArgument, ForWriting)
Nothing very fancy here: we simply call OpenTextFile and pass it the path to the file we want to open (strArgument) followed by the constant ForWriting (which tells the FileSystemObject that we want to write to the file).
As it turns out, the FileSystemObject doesn’t have any sort of pre-defined method for erasing the contents of a file. But that’s OK; we can handle that ourselves. When you open a file for writing, anything you write will completely replace the existing contents of that file. We want to erase the file, which means we want an empty file, one that doesn’t have any contents. Therefore, all we have to do is write an empty string to the file, like so:
That replaces the existing contents of the file with nothing, effectively erasing everything that came before. (Thanks; we thought it was clever, too.) We then close the file, loop around, and repeat the process with the next argument in the collection.
Having shown you the code we now feel compelled to issue a warning: be careful when you use this script. Ostensibly it’s designed to work only with text files; after all, that’s what the FileSystemObject is for. However, this script can erase other types of files: we tried it with Word documents, Excel spreadsheets, .ZIP archives, and .PDF files, and the script succeeded in erasing each and every file we dropped on it. That’s not bad, mind you; it’s just something to keep in mind. If you drag a file onto this script you can pretty much count on that file getting erased. The file itself remains; you’ll still have a file named C:\Scripts\Test.txt. It’s just that the file will now be empty.
Note. We won’t show you how to do this today, but it would be easy enough to have the script prompt you before deleting a file (e.g., “Are you sure you want to delete file x?”). In fact, in digging through the Hey, Scripting Guy! archives we uncovered a column that explains how to do something very similar.
In case you’re wondering, the fact that the Scripting Guy who writes this column was quickly able to identify the culprit as being a huge mass of bugs, as opposed to rain or hail or something else, wasn’t really that big of a deal. Like we said, he does work at Microsoft, and who would know more about a huge mass of bugs than someone who works at Microsoft?