Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Running app with spaces in path with wscript
Message
From
18/12/2018 08:16:47
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Miscellaneous
Thread ID:
01664594
Message ID:
01664598
Views:
53
Likes (1)
>Hi there,
>
>I am reading a path from an ini file, add a file Name and try to execute it with the wscript.shell-run command.
>But when the Path contains strings the call does not work like
>
>wsh = createobject("wscript.shell")
>cDoUpdatefile = "c:\program files\galileo\download\update_galileo_1_19_Start.exe"
>nRet = wsh.Run((cDoUpdatefile),1,.T.)
>
>What can I do to make it work? Running the same as awindows-command works fine.
>
>Best regards
>
>Thomas

If using wsh.Run is not a must (it needs activex) the IMHO ShellExecute is a better API to use. ie:
* This is in my D: drive
* d:\vfpcommonclasses\shexec.prg

#Define SW_HIDE             0
#Define SW_SHOWNORMAL       1
#Define SW_NORMAL           1
#Define SW_SHOWMINIMIZED    2
#Define SW_SHOWMAXIMIZED    3
#Define SW_MAXIMIZE         3
#Define SW_SHOWNOACTIVATE   4
#Define SW_SHOW             5
#Define SW_MINIMIZE         6
#Define SW_SHOWMINNOACTIVE  7
#Define SW_SHOWNA           8
#Define SW_RESTORE          9
#Define SW_SHOWDEFAULT      10
#Define SW_FORCEMINIMIZE    11
#Define SW_MAX              11

Define Class ShellExecutor As Custom
	cError = ''
	cErrors = ''

	Procedure Init
		Declare Long ShellExecute In "shell32.dll" ;
			long HWnd, String lpszOp, ;
			string lpszFile, String lpszParams, ;
			string lpszDir, Long nShowCmd

TEXT to this.cErrors noshow
0,The operating system is out of memory or resources. \n
2,The specified file was not found. \n
3,The specified path was not found. \n
11,The .exe file is invalid (non-Win32® .exe or error in .exe image). \n
5,The operating system denied access to the specified file.  \n
27,The file name association is incomplete or invalid. \n
30,The DDE transaction could not be completed because
other DDE transactions were being processed. \n
29,The DDE transaction failed. \n
28,The DDE transaction could not be completed because the request timed out. \n
32,The specified dynamic-link library was not found.  \n
31,There is no application associated with the given file name extension.
This error will also be returned if you attempt to print a file that is not printable. \n
8,There was not enough memory to complete the operation. \n
26,A sharing violation occurred. \n
ENDTEXT

	Endproc

	Procedure Execute
		Lparameters tcExecutable,tcParams,tcWorkingDir,tnShowType,tcOperation

		*!*	ShExecute('c:\temp\nodoc.doc')
		*!*	ShExecute('c:\temp\nodoc.xyz')
		*!*	ShExecute('d:\fpd26\foxprox.exe','mytest.prg','d:\fpd26',0)
		*!*	ShExecute('d:\fpd26\foxprox.exe',,'d:\fpd26')
		tcOperation  = Iif(Empty(m.tcOperation), 'Open', m.tcOperation)
		tcExecutable = Iif(Empty(m.tcExecutable), '', m.tcExecutable)
		tcParams     = Iif(Empty(m.tcParams), '', m.tcParams)
		tcWorkingDir = Iif(Empty(m.tcWorkingDir), '', m.tcWorkingDir)
		tnShowType   = Iif(Type('m.tnShowType') # 'N', SW_SHOWNORMAL, m.tnShowType)
		Local lnResult
		lnResult = ShellExecute(0,m.tcOperation,m.tcExecutable,m.tcParams,m.tcWorkingDir,m.tnShowType)
		If ( m.lnResult <= 32 )
			This.cError = This.GetErrorMsg(m.lnResult)
		Else
			This.cError = ''
		Endif
		Return ( m.lnResult > 32 ) && No Error
	Endproc

	Procedure GetErrorMsg
		Lparameters tnErrNum
		Local Array aErrors[1]
		Local lcMessage, ix
		For ix=1 To Alines(aErrors,This.cErrors,.T.,'\n')
			If ( Val(Chrtran(Left(aErrors[m.ix],;
					At(',',aErrors[m.ix])-1),Chr(13)+Chr(10),'')) = m.tnErrNum )
				lcMessage = Substr(aErrors[m.ix],At(',',aErrors[m.ix])+1)
				Exit
			Endif
		Endfor
		If Empty(m.lcMessage)
			lcMessage = 'An unspecified error occurred.'
		Endif
		Return m.lcMessage
	Endproc
Enddefine
And this is a sample call:
Local lcFile
lcFile = 'C:\Program Files (x86)\LINQPad5\LinqPad.exe'

Local oShellExec As 'ShellExecutor' Of 'd:\vfpcommonclasses\shexec.prg'
oShellExec = Newobject('ShellExecutor','d:\vfpcommonclasses\shexec.prg')
If !oShellExec.Execute(m.lcFile,'','',1)
	Messagebox( oShellExec.cERROR,0,'An error occurred')
Endif
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform