Login / Register  search  syndication  about

          Ryan Farley's Blog

Ryan Farley on .NET Development with a focus on CRM Development for SalesLogix

How to: Execute a file/program in VBScript

Put this one in your utilities scripts to include when needed:

Sub Run(ByVal sFile)
Dim shell

    Set shell = CreateObject("WScript.Shell")
    shell.Run Chr(34) & sFile & Chr(34), 1, false
    Set shell = Nothing
End Sub

Then to use it include the script and call it as follows:

' Execute the file MyFile.exe
Run "C:\Program Files\MyFile.exe"

Or, use it to execute a file in the app that is registered to handle files of that type:

' Open the file MyFile.doc in Word
Run "C:\My Documents\MyFile.doc"

 

What's This?
  
Bookmark and Share

About Ryan Farley

   Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix since 2001 and believes in sharing with the community. He loves C#, Javascript, Python, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

View the SalesLogix Mobile Seveloper Series
View the SalesLogix SData Developer Series
View the Git for the SalesLogix Developer series



Related Content
   Using a Test Plan
So you have approved the Statement of Work (SOW) for some modifications and are awaiting the delivery of
Posted on Mar 13, 2014 by George Jensen to George Jensen's Blog
 
   Creating a Saleslogix Ticket Pretty Prefix and Suffix in External Applications
Some entities in SalesLogix, such as Tickets, use a "pretty key" as an easier to use value to r
Posted on Feb 28, 2014 by Ryan Farley to Ryan Farley's Blog
 
   Evaluating strings in C# Code snippets
When checking for two (or more) different values for a property in a code snippet, you can't simply l
Posted on Dec 13, 2013 by Jason Buss to Jason Buss' Blog
 
   Updates to the SalesLogix Mobile Developer Toolkit
I've released some updates to the SalesLogix Mobile Developer Toolkit. This new release adds some fix
Posted on Apr 10, 2013 by Ryan Farley to Ryan Farley's Blog
 
   Free Tool to Retrieve SalesLogix User Names and Passwords
As a business partner we work with customer SalesLogix databases often. Along with getting a database bac
Posted on Feb 25, 2013 by Ryan Farley to Ryan Farley's Blog
 
Comments

 

Adam Theaker said:

Thank You,

I am just learning to develop in SalesLogix and finiding examples on how todo things is like pulling your own teeth. I have plenty of experince with VB6 and ASP, and i am still trying to wrap my head around slxVB.

thanks again

Adam
March 10, 2004 9:51 AM
 

Tim said:

This was clear and simple, thanks.

Question: does the executed program run "in line" so the statement after Run "C:\Program Files\MyFile.exe" waits until MyFile.exe is done?

Or does MyFile.exe run asynchronously, sort of like a forked process?

Thanks again.
Tim
August 12, 2004 1:08 PM
 

Ryan Farley said:

Tim,

That's what the last parameter is for, whether or not it is launched asynchronously or not. The example above sets the last parameter to False, so it is not blocking. You could change that to true to achieve the opposite result.

-Ryan
November 3, 2004 1:47 PM
 

Denise Goodheart said:

Hi Ryan,

Thank you for posting your very useful utility script. It helped me enormously. I used your code to execute media player sound files; such as .wav sound files. My question is how can I have the media player software appear on the screen while the sound file is running based on the Windows Folder Option File Type? For instance, a .wav file would be associated with Windows Media Player.

Thank you very much,
Denise Goodheart
July 25, 2005 10:36 PM
 

Sean said:

Hi i would appreciate if you could let me know if i can get the screen shot instead of the message on prompt for the following code..

' Declare variables
Dim WSHShell,strRegKey
' Create the Wscript Shell object, which contains the Registry methods
Set WSHShell = WScript.CreateObject("WScript.Shell")
strRegKey="HKLM\Software\XYZ\Installed\APPNAME\MSI Release Version"
WScript.Echo "Current MSI Version is: " & WSHShell.RegRead(strRegKey)
Wscript.quit(0)

appreciate your help in advance
January 14, 2008 12:14 PM
 

Brandon said:

Can you run the application async and pass data back into SLX?
January 22, 2008 3:38 PM
 

bonald said:

I can't use space with the run command
exemple: gpupdate /force

how can i make this work?
January 29, 2008 8:11 AM
 

Nicolas Galler said:

That is actually very neat - for some weird reason I always felt the neat to do something like shell.run "cmd /c " & file... good to know that I can make it a bit shorter

April 15, 2008 10:11 AM
 

Jenny pretty said:

Very nice utility script in VBScript that I ever saw. Thanks very much for sharing this short script with us.

April 17, 2008 1:39 PM
 

JohnDoe said:

Thank You for sharing your Scripts....

April 25, 2008 5:48 AM
 

Márcio said:

One question: could you give a example of a vbscript that checks if the program is already opened, so it doesn't open it again?

And thanks for the script. Nice and simple.

May 28, 2008 4:01 PM
 

Wyn said:

Thanks for sharing this script. I'm trying to use this script to run some registry keys to be registered under the windows registry. Is there a way to run these registry keys using this script with out any user interaction? Thanks in advance.

June 27, 2008 8:59 AM
 

Wyn said:

Thanks for sharing this script. I'm trying to use this script to run some registry keys to be registered under the windows registry. Is there a way to run these registry keys using this script without any user interaction? Thanks in advance.

June 27, 2008 9:00 AM
 

Ryan Farley said:

Wyn,

You don't need to execute the reg files to read/write to the registry. Just read and write the values as needed. See customerfx.com/.../39077.aspx

Optionally, you could use the "/s" switch when executing the reg files to do it silently.

-Ryan

June 27, 2008 10:05 AM
 

John Lowe said:

How can I run the script remotley, ie from IIS5 so that I can run a program via a webpage?

Thanks.

July 8, 2008 4:48 AM
 

Script to share multiple folders said:

Pingback from  Script to share multiple folders

August 20, 2008 10:43 AM
 

Josh said:

Nice 'n simple script. Thanks Chris Farley! ;)

August 20, 2008 12:43 PM
 

Dmitry said:

thnx, it helped =)

September 3, 2008 1:30 AM
 

foxhenry said:

short script  but good.. thanks

December 11, 2008 12:26 AM
 

Swordstoo said:

WOW! it worked straight up! although i got kinda freaked out when my nortan internet sequrity came up! i thought the stuff was a scam and virus, but i took the risk and it worked! TY! and as a bonus i didnt have to do anything, nothing incorrect!

January 5, 2009 4:33 AM
 

Swordstoo said:

WOW! it worked straight up! although i got kinda freaked out when my nortan internet sequrity came up! i thought the stuff was a scam and virus, but i took the risk and it worked! TY! and as a bonus i didnt have to do anything, nothing incorrect!

January 5, 2009 4:33 AM
 

Swordstoo said:

WOW! it worked straight up! although i got kinda freaked out when my nortan internet sequrity came up! i thought the stuff was a scam and virus, but i took the risk and it worked! TY! and as a bonus i didnt have to do anything, nothing incorrect!

January 5, 2009 4:33 AM
 

Frostfire said:

Great Script!

To add attributes you can modify it like this instead:

Sub Run(ByVal sFile,sAttr)

Dim shell

   Set shell = CreateObject("WScript.Shell")

   shell.Run Chr(34) & sFile & Chr(34) & sAttr, 1, false

   Set shell = Nothing

End Sub

Run "cmd.exe", "/K query farm > farms.txt"

January 21, 2009 8:48 AM
 

links for 2009-02-12 « Yesure said:

Pingback from  links for 2009-02-12 « Yesure

February 12, 2009 11:58 AM
 

Egon said:

Nice Comand Ryan

Now Im try to install a program into the script how I make the instalation with out the next bottons,

I want the script install all the program for me automatically

could yo helpme with this ?

June 26, 2009 12:49 PM
 

Isa said:

Thanks a lot! this was the only straight forward script i could find. helped a lot=)

July 8, 2009 9:23 PM
 

aenis said:

Thank you

August 17, 2009 6:30 PM
 

Doc_U said:

Frostfire, great addition to this script.

I was using the Run command as shown above in my login scripts, but had to write customized lines for commands with attributes.

This does it all. My login scripts thank you.

-Doc

October 8, 2009 12:12 AM
 

Jeremy said:

I am trying to call an .HTA file to display a popup (motd) from my network logon script.  This seems to be opening the hta... but then it imediatly closes.

any ideas?

November 4, 2009 11:18 AM
 

Vivek said:

I have run an exe.

Can you please tell me how do I capture the output of this exe being run?

May 18, 2010 6:02 AM
 

Hiya said:

How do you check if an application is running?  Is this possible in vbscript?

July 4, 2010 6:36 AM
 

Priya said:

Great yaar.... Its working.... Thanks Ryan....

July 27, 2010 3:05 AM
 

Ian said:

Heya,

I have the following codeing in a .vbs file.

Set WshShell = CreateObject("WScript.Shell")

WshShell.Run chr(34) & "C:\Test.exe" & Chr(34), 0

Set WshShell = Nothing

Running this code excutes the test.exe hiding the promopt but without 3 paramters the exe fails. Normally this could be passed by a .bat or shortcut such as

Shortcut "C:\Test.exe" 10 1024 1024

Bat Start C:\Test.exe 10 1024 1024.

Could anyone tell me how i could put these 3 numbers into the vbs coding so my .exe will work.

September 7, 2010 11:22 AM
 

Romeo said:

Thank you!

September 7, 2010 4:39 PM
 

Kman said:

is there anyway you can dictate the program used to open a file? Say I want to open song.mp3 with Audacity.exe despite Audacity not being the default application.

September 29, 2010 1:51 AM
 

Maiko said:

Hi, I've a problem with the focus of a MsgBox after use this code.

Dim sYourCommand

Dim objShell

Set objShell = WScript.CreateObject ("WSCript.shell")

'objShell.Run  "cmd /c " & s,1,True

sYourCommand =  "notepad.exe"

objShell.Run sYourCommand,10,True  

MsgBox " Focus ?", vbYesNo+vbQuestion+vbDefaultButton2

can you help me?

February 8, 2011 10:10 AM
 

Mei said:

Hi Ryan, thanks for the tutorial. Is it possible to run/execute a program to computer client also through the network..?

Thanks again,

Mei.

April 10, 2011 1:43 AM
 

VDV said:

Hi Ryan,

How to call VBS file from ASP page server side codes? I can't get it working.

Thank you!

May 4, 2011 8:24 AM
 

Hugo Plascencia said:

How can I be able to SendKeys from here?

The program open, but I get and error when sending "~" key

Sub Run(ByVal sFile)

Dim shell

   Set shell = CreateObject("WScript.Shell")

   shell.Run Chr(34) & sFile & Chr(34), 1, false

   Set shell = Nothing

End Sub

Run "C:\Program Files\McKesson\STAR Navigator 15.16\HBOC32\MckBoot2.exe"

wscript.sleep (700)

wscript.sendkeys "~"

June 16, 2011 12:16 PM
 

Shell vbs | Dminsitedemo said:

Pingback from  Shell vbs | Dminsitedemo

July 21, 2011 12:30 AM
 

Arindam Jha said:

Hi Ryan,

I am trying to open my custom application through a code like this.....

Dim oWSH

Set oWSH= CreateObject("WScript.Shell")

Dim i

Dim objProc

Set objProc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("Select * from Win32_Process where Name='MyApp.exe'")

i = 0

cpuser = user

For Each Proc in objProc

   Return = Proc.GetOwner(user)

If Return <> 0 Then

i = i    

    Else

i = i+1

    End If

Next

Now my environment in Visual Studio .NET 2001 and VB Script. I am using Windows 7 as operating System and IE 8 as the browser. Now the above code I am using to check if the application is already running by that user. In IE 7 and Windows XP below the code is running fine and I am getting the desired result that is in the "Result" variable I am getting value 0 if the application is not running. But in IE 8 and Windows 7 if the process is not running then it is returning 1 which creates the issue. I am assuming the problem area is the below line....

Set objProc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("Select * from Win32_Process where Name='MyApp.exe'")

Now it would be highly appreciated if I get some help on this. I have to build the code in a generic way so that the application will run on IE 8 or below and Any Windows version.

Thanks

Arindam

August 19, 2011 2:21 PM
 

Manoj said:

AWESOOOOOOOOMMMMMME.......... :D

October 10, 2011 2:50 AM
 

lucas said:

does't work

December 21, 2011 2:22 PM
 

Querk McGoober said:

Whooooooo hoooooo....

scriptkidders love this site

February 23, 2012 4:49 PM
 

GRA0007 said:

WOW, Thanks SOOOOOOO Much. I have been trying to do this forever and finally I can do it!!!

March 22, 2012 5:39 PM
 

wea said:

how to use schedules / time to ShellExecute..?

March 31, 2012 4:22 PM
 

Monu Sahu said:

can anybody tell me , how to use checkj enter key in vb script,

that any body press enter key in vb script

please suggests..

May 9, 2012 1:35 AM
 

monusahu said:

I have one question , how to check enter key in vbscript .

when we have press enter key in vbscript ,

please any body suggests..

May 9, 2012 1:43 AM
 

M.F said:

Hi, Nice Blog

I was wondering if there a good website, e-book or online cource that could help me to learn Vbscript "not the vbscript in the html codes"

thanks

May 14, 2012 2:35 PM
 

Rohit said:

How I can Create a Application object of Audacity software in vb script for automation

please tell me i wait...............

July 18, 2012 1:11 AM
 

Michael said:

That's it!!!

July 25, 2012 5:48 AM
 

Mike said:

This doesn't work in Windows 7 when attempting to run subprograms as administrator, and ShellExecute doesn't wait for a program to complete before the script continues on its merry way.  (Leave it to Micros**t to screw things up for the world.)

August 9, 2012 4:07 PM
 

Vinod Trivedi said:

Sir, How to open AutoCAD (Installed in "C:\Program Files\AutoCAD 2008\acad.exe") through VBA in MS Excel Macro.  Thanking you in advance

November 22, 2012 10:38 AM
 

Deep said:

Vinod, are you asking how to open autocad.exe installed in the path you have described, using VBscript?

If so, use this:

Set obj1 = Wscript.CreateObject("wscript.shell")

Obj1.Run("""C:\Program Files\AutoCAD 2008\acad.exe""")

Make sure you use three starting and ending double quotes for the path as I have written.

November 24, 2012 1:23 PM
 

Ashish Mahajan said:

Hi Ryan and All Guys,

Could anyone tell me how to execute a .vbs file in HTML??

Its very urgent and your suggestion is appreciated.

January 22, 2014 11:53 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
All contents Copyright © 2014 Customer FX Corporation
Customer FX Corporation
2324 University Avenue West, Suite 115
Saint Paul, Minnesota 55114
Tel: 800.728.5783

  Follow @CustomerFX on twitter
Follow the best news, tips, and articles
  Subscribe to Customer FX on youtube
Watch SalesLogix tutorial videos from Customer FX
Login / Register