Shell Function Parameter with Spaces

  • Thread starter Mr Utkal Ranjan
  • Start date
M

Mr Utkal Ranjan

Hi Friends

I want to launch notepad with a specific file on a
command click event. So I was using the "Shell" function
from VB on a command click event.For ex:

Shell "Notepad.exe C:\Program
Files\Test.txt",VbNormalFocus

But here is the problem in the space of the "Program
Files" folder, ie, "Program[SPACE]Files". The Shell
function not reading any character after the SPACE.

So HOW CAN I Launch my programs which has a SPACE in the
PATH parameter ???

Thanx
 
H

Herfried K. Wagner [MVP]

* "Mr Utkal Ranjan said:
I want to launch notepad with a specific file on a
command click event. So I was using the "Shell" function
from VB on a command click event.For ex:

Shell "Notepad.exe C:\Program
Files\Test.txt",VbNormalFocus

But here is the problem in the space of the "Program
Files" folder, ie, "Program[SPACE]Files". The Shell
function not reading any character after the SPACE.

So HOW CAN I Launch my programs which has a SPACE in the
PATH parameter ???

Try 'System.Diagnostics.Process.Start'.
 
M

Mike Labosh

Shell "Notepad.exe C:\Program
Files\Test.txt",VbNormalFocus

But here is the problem in the space of the "Program
Files" folder, ie, "Program[SPACE]Files". The Shell
function not reading any character after the SPACE.

Mr. Wagner's response is probably a better way, but here's the rundown on
the spaces:

When you called shell, you passed it a command line that is interpreted by
the operating system:

Notepad.exe C:\Program Files\Test.txt

According to the way the operating system parses command lines, you have
specified 3 tokens:

1. Notepad.exe
2. C:\Program
3. Files\Test.txt

The operating system recognises the first as a program, finds it in the
path, and executes it, passing the other two tokens as two command line
parameters. Notepad thinks that "c:\program" is the name of the file you
want to open.

To prevent this, any file system references can be enclosed in double quotes
so that the operating system doesn't misinterpret the meaning of the embeded
spaces, like this (you can actually try this in a Start -> Run box):

Notepad.exe "C:\Program Files\Test.txt"

The way you would issue your shell statement to include the quotes as part
of the string is to use two double quotes for each double quote that you
want inside the string:

Dim s As String
s = "Notepad.exe ""C:\Program Files\Test.txt"""
MessageBox.Show(s) 'Note the way the quotes are embedded
Shell(s, AppWinStyle.NormalFocus)

This quote technique works anywhere in Windows where the operating system or
a [well written] program expects a path or filename.
--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top