Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Trying my first VB script - run external program

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,60127
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Trying my first VB script - run external program
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Trying my first VB script - run external program - 5/12/2008 2:24:09 PM   
  santhas

 

Posts: 8
Score: 0
Joined: 5/12/2008
Status: offline
Greetings to VB Gurus,

I am writing my first VB script, sp please bare with me.

These are my lines to call external program and capture the out put ( Lines adapted from MS site).

Set objShell = CreateObject("Wscript.Shell")
strCommand = "%comspec% /k dir " & Chr(34) & "C:\Program Files\VERITAS\NetBackup\bin\admincmd\bperror" & chr(34)
objShell.Run(strCommand), 1, TRUE

I am not sending that output to a file yet.

and this is the error message I am getting
Volume in drive C has no label.
Volume Serial Number is 54A4-28F6

Directory of C:\Program Files\VERITAS\NetBackup\bin\admincmd

File Not Found

D:\Scripts>

What am I doing wrong here?
Thanks
 
 
Post #: 1
 
 RE: Trying my first VB script - run external program - 5/12/2008 5:46:44 PM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi santhas,

as the error message indicates, you are asking dir for a file or directory
that doesn't exist. You could try versions of

  dir "C:\Program Files\VERITAS\NetBackup\bin\admincmd\bperror"

to see that the output of your script is 'correct'.

As this is your first script, why not do it exactly right:

Option Explicit
Const SW_SHOWNORMAL      =  1
Dim
objShell
Set objShell = CreateObject("Wscript.Shell")
Dim
strCommand
strCommand = "%comspec% /k dir " & Chr(34) & "C:\Program Files\VERITAS\NetBackup\bin\admincmd\<existing item>" & chr(34)
objShell.Run strCommand,
SW_SHOWNORMAL, True

See the Docs for the .Exec method of the WScript.Shell object; capturing the output
will be easier, if you can use this instead of .Run and redirection.


Good luck!

ehvbs



(in reply to santhas)
 
 
Post #: 2
 
 RE: Trying my first VB script - run external program - 5/14/2008 3:24:08 PM   
  santhas

 

Posts: 8
Score: 0
Joined: 5/12/2008
Status: offline
Hi ehvbs,

Thanks for your input. I changed to shell.exec as you said and when I run either of below line it produces blank file.

objShell.Exec "%comspec% /k C:\Program Files\VERITAS\NetBackup\bin\admincmd\bperror.exe > bperror.txt"


'objShell.Exec "%comspec% /k" & Chr(34) & "C:\Program Files\VERITAS\NetBackup\bin\admincmd\bperror.exe > bperror.txt" & chr(34)

Which is not correct. Could someone point my mistake pl?

Thanks

(in reply to ehvbs)
 
 
Post #: 3
 
 RE: Trying my first VB script - run external program - 5/14/2008 5:12:34 PM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi santhas,

switching to .Exec should be based on actually reading the Docs. You may use
this


      

to start your experiments.

Good luck!

ehvbs

(in reply to santhas)
 
 
Post #: 4
 
 RE: Trying my first VB script - run external program - 5/14/2008 10:41:22 PM   
  santhas

 

Posts: 8
Score: 0
Joined: 5/12/2008
Status: offline
I don't understand this "§D§". What is this? Thanks

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: Trying my first VB script - run external program - 5/15/2008 3:31:06 AM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi santhas,

applying

  Replace( sCmd, "§D§", sDir )

on?/to?

  "%comspec% /c dir ""§D§"""

will replace the place holder §D§ with the content of sDir (better
named sFSpec); so the result will be:

  "%comspec% /c dir ""C:\Program Files\Microsoft Windows Script\ScriptDocs\script56-de.chm"""

This method makes putting variable content into a string more lucid.

Regards

ehvbs


(in reply to santhas)
 
 
Post #: 6
 
 RE: Trying my first VB script - run external program - 5/15/2008 8:39:49 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
The .run command, let alone a DOS command will never work with white spaces in the path.
Use short, DOS compatiible paths like "C:\Progra~1\Microso~1\..."

_____________________________

Fred

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: Trying my first VB script - run external program - 5/15/2008 8:45:32 AM   
  ebgreen


Posts: 4613
Score: 31
Joined: 7/12/2005
Status: offline
Actually run commands work fine with spaces in them. You just need to quote them properly.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Fredledingue)
 
 
Post #: 8
 
 RE: Trying my first VB script - run external program - 5/15/2008 9:05:24 AM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
The .Human brain, let alone a NORMAL .Human brain will never work with
gibberish like "C:\Progra~1\Microso~1\...". Use long, brain compatible
pathes like "C:\Program Files\Microsoft Windows Script\ScriptDocs\Script56.CHM"
and - as ebgreen said - quote them.

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: Trying my first VB script - run external program - 5/16/2008 6:06:10 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
Example? (always ready to learn)

_____________________________

Fred

(in reply to ehvbs)
 
 
Post #: 10
 
 RE: Trying my first VB script - run external program - 5/16/2008 6:51:18 AM   
  ehvbs

 

Posts: 2078
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi Fredledingue,

this code will open the VBScript Docs twice:


      

if the path to the Docs on your system contains spaces, it proves that
.Run and .Exec can handle spaces. The output (etc):


      

shows that even DOS (ok, the DOS of modern Windows versions) can handle such
input, as long as you double quote it to help the parser.

Regards

ehvbs

(in reply to Fredledingue)
 
 
Post #: 11
 
 RE: Trying my first VB script - run external program - 5/16/2008 10:23:38 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
I didn't know that.
Hey I tested and it works!
As you see I learn everyday.

Yet, most of the reported errors with .run I have seen here came from these white spaces. Using shortpaths remove the white spaces, but adding double quotes is of course simplier.

Now, I wonder how to pass parameters to the app in question...
oWSH.Run """C:\Program Files\my.EXE"" " & Param   '?
Will that work?

I also don't understand very much this little exercice:
 Dim sCmd        : sCmd        = """§P§"""
 sCmd = Replace( sCmd, "§P§", sPathToDocs )
 
Why do you do that? ??
[quote="ehvbs"]This method makes putting variable content into a string more lucid. [/quote]
==> ???? ==> That was the only reason why I didn't see that trick in your replies above: I didn't understand anything in your code... Well, if it's to make things more lucid, then it's fine. LOL.




< Message edited by Fredledingue -- 5/16/2008 10:27:01 AM >


_____________________________

Fred

(in reply to ehvbs)
 
 
Post #: 12
 
 RE: Trying my first VB script - run external program - 5/17/2008 9:41:10 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
I'm realy stunned by this revelation! It even works in a DOS prompt manualy written. I can't believe it.

Note that long file name support for DOS  (that's what these quote allows in fact) works only in a windows environement.
Plain DOS will not work with that. That's why the quote trick is the less documented and the most ignored DOS command.




_____________________________

Fred

(in reply to Fredledingue)
 
 
Post #: 13
 
 RE: Trying my first VB script - run external program - 5/19/2008 9:59:17 PM   
  santhas

 

Posts: 8
Score: 0
Joined: 5/12/2008
Status: offline
Hi ehvbs,

Thanks for you help. I spend some time to add switches to that command and finaly made. This how I ended with (adapted  from your stesps not fine tuned yet ).

Const SW_SHOWNORMAL      =  1
Dim oFS  : Set oFS  = CreateObject( "Scripting.FileSystemObject" )
Dim sDir : sDir     = "C:\Program Files\VERITAS\NetBackup\bin\admincmd\bperror"
Dim mProg : mProg   = "C:\windows\system32\blat.exe"
Dim sSwitch : sSwitch = "-hoursago 24 -t backstat -U"
Dim mSwitch : mSwitch = " -t backupadmin@mydomain.com -i backups@mydomain.com -server mymailserver"
Dim sLog : sLog     = "bperror.txt"
Dim oWSH : Set oWSH = CreateObject( "Wscript.Shell" )
Dim sCmd : sCmd     = "%comspec% /c ""**"""
Dim mCmd
mCmd = mProg & " " & sLog &" "& "-s " & """bk001 - Backup status report""" & mSwitch
sDir = oFS.GetAbsolutePathName( sDir )
sCmd = Replace( sCmd, "**", sDir )
sCmd = sCmd & " " & sSwitch

' WScript.Echo ".Exec Cmd:", sCmd
'WScript.Echo oWSH.Exec( sCmd ).Stdout.ReadAll

sCmd = sCmd & " >" & sLog
' WScript.Echo ".Run Cmd:", sCmd
oWSH.Run sCmd, SW_SHOWNORMAL, True
' WScript.Echo oFS.OpenTextFile( sLog ).ReadAll
' WScript.Echo oWSH.Exec( mCmd ).Stdout.ReadAll
oWSH.Run mCmd, SW_SHOWNORMAL, True

No efficient but does what I wanted.

Thanks again.

(in reply to Fredledingue)
 
 
Post #: 14
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Trying my first VB script - run external program Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts