How Can I Use Long File Names as Parameters?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m trying to run a command-line tool from a script, but I need to pass it a long file name like C:\Documents and Settings\All Users\Desktop\logfile.txt. No matter how I try to pass that file name, however, my script blows up on me. Help!

— WK, Birmingham, Great Britain

SpacerHey, Scripting Guy! AnswerScript Center

Hey, WK. Ah, yes: you’ve encountered the dreaded double double-quotes problem. What does that mean? Well, let’s say you’re trying to run Notepad and pass it a long file name. In order to do that, you need to type the following command from the command prompt:

notepad.exe "c:\my scripts\logfile.txt"

As you no doubt know, any time you need to do something from the command prompt that requires spaces in the path name, you need to enclose the entire path in double quote marks. Hence “c:\my scripts\logfile.txt”.

So why does that cause problems with your script? Well, you’re trying to start Notepad using the Windows Script Host Run method. To start a program using this method, you must enclose the complete command in double quote marks:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "ping.exe 192.168.1.1"

That works fine with the Ping example, but here’s what you get when you try to run Notepad and pass it a long file name:

Set objShell = CreateObject("Wscript.Shell")
objShell.Run "notepad.exe "c:\my scripts\logfile.txt""

As you have discovered, this doesn’t work: you get an Expected end of statement error. That’s because the script interpreter sees “notepad.exe ” as being a complete line of code; consequently, as soon as it sees the rest of the code it freaks out. So now we’ve got a problem: we need to enclose c:\my scripts\logfile.txt in double quote marks, but WSH freaks out when it sees double quotes inside of quotes. We’re doomed, right?

Right.

No, hey, just kidding. There are actually a couple of ways around this, and here’s one we like because it’s versatile: the same approach can be used to embed single quotes and other reserved characters into command-line parameters. As you probably know, all characters on the keyboard have an ANSI equivalent; that’s just a number that represents the character. For example, the lowercase letter a has an ANSI value of 97; the uppercase A has an ANSI value of 65. Still not impressed? Well it turns out that 34 is the ANSI value for double quote marks. Now you’re impressed, right?

Ok, then we’ll explain it a bit more. VBScript has a function named Chr that converts an ANSI value into its character equivalent. For example, try to guess what you get when you run this script:

Wscript.Echo Chr(65)

That’s right; an uppercase A is echoed to the screen.

Now to try to guess what happens when you run this script:

Wscript.Echo Chr(34)

Yep; double quote marks get echoed to the screen. Now here’s a tricky one:

strArgument = "notepad.exe " & chr(34) & _
    "c:\my scripts\logfile.txt" & chr(34)
Wscript.Echo strArgument

Before you give up, here’s the “equation” we’re using:

Notepad.exe + " + c:\my scripts.logfile.txt + "

Add them altogether and you get this:

notepad.exe "c:\my scripts.logfile.txt"

In other words, we now have the exact string we’d have to type at the command prompt in order to start Notepad and load in C:\My Scripts\Logfile.txt:

notepad.exe "c:\my scripts.logfile.txt"

Try this revised script, and see what happens:

Set objShell = CreateObject("Wscript.Shell")
strArgument = "notepad.exe " & chr(34) & _
    "c:\my scripts\logfile.txt" & chr(34)
objShell.Run strArgument

0 comments

Discussion is closed.

Feedback usabilla icon