Using the Shell function in a web application

  • Thread starter Thread starter Eric Caron
  • Start date Start date
E

Eric Caron

Hi all,

I'm trying to automate some tasks on a server in our intranet by using a web
application. I already have a batch script that registers a DLL passed as a
parameter on the command line. I'd like to be able to first copy a file
from a shared location to the root of the c:\ drive, then call my batch file
with the file as a parameter. I found the Shell function that seemed to do
what I want and did a quick and dirty web form with a textbox and a button
with the following code on the on_click handler of the button:

strCommandLine = "copy E:\Data\" & Me.txtCommand.Text & " c:\"
Response.Write("<p>" & strCommandLine & "</p>" & vbCrLf)
Shell("copy E:\Data\" & Me.txtCommand.Text & " c:\", AppWinStyle.Hide, True)
strCommandLine = "update.bat " & Me.txtCommand.Text
Response.Write("<p>" & strCommandLine & "</p>" & vbCrLf)
Shell("update.bat " & Me.txtCommand.Text)

However, I get the following error when the Shell command is executed:

File not found.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: File not found.

I'm sure of the path and the file is there. I'm wondering if it's a
permission problem and the error message is misleading me.

Any example of a similar task?
 
Yes, it's probably a permissions issue.
You'll likely want to try using impersonation so you're running under a user
account that has the necessary permissions. (Try setting it to use your
username & pw for initial testing purposes.)
Here's more info:
http://msdn.microsoft.com/library/d...-us/cpguide/html/cpconaspnetimpersonation.asp

Also, you might try using the process object since it's a bit more robust
than shell. Here's an example:
Dim proc As System.Diagnostics.Process = New System.Diagnostics.Process()
proc.StartInfo.FileName = "C:\test.exe"
proc.StartInfo.WorkingDirectory = "C:\"
proc.StartInfo.Arguments = strMyArguments
proc.Start()
 
Back
Top