1

Have this upload script, and it works. But I would like to add a cmd command after the upload has complete. Is this possible? Thank you in advance.

<%@ Page Language=VBScript %>

    <script runat="server">
        Protected Sub Button1_Click(ByVal sender As Object, _
          ByVal e As System.EventArgs)
            If FileUpload1.HasFile Then
                Try
                    FileUpload1.SaveAs("C:\Inetpub\wwwroot\upload\" & _
                       FileUpload1.FileName)
                    Label1.Text = "File name: " & _
                       FileUpload1.PostedFile.FileName & "<br>" & _
                       "File Size: " & _
                       FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                       "Content type: " & _
                       FileUpload1.PostedFile.ContentType
                Catch ex As Exception
                    Label1.Text = "ERROR: " & ex.Message.ToString()
                End Try
            Else
                Label1.Text = "You have not specified a file."
            End If
        End Sub
    </script>
3
  • What do you mean "add cmd button" exactly? Please be more clear. Also, ContentLength property returns the length in bytes not kilo bytes, you have to divide this in 1024 to get KB. Commented Jul 25, 2011 at 11:00
  • Please explain what you mean by "add a cmd command" it's hard to tell what exactly you are referring to. Are you trying to run an external command from the shell? Commented Jul 25, 2011 at 13:54
  • I mean to want in insert a command such as "REN C:\Document.rtf YES.rtf" for example. Commented Jul 25, 2011 at 14:14

1 Answer 1

1

Use ProcessStartInfo:

C#

public static int ExecuteCommand(string Command, int Timeout)
{
   int ExitCode;
   ProcessStartInfo ProcessInfo;
   Process Process;

   ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
   ProcessInfo.CreateNoWindow = true; 
   ProcessInfo.UseShellExecute = false;
   Process = Process.Start(ProcessInfo);
   Process.WaitForExit(Timeout);
   ExitCode = Process.ExitCode;
   Process.Close();

   return ExitCode;
}

VB:

Public Shared Function ExecuteCommand(Command As String, Timeout As Integer) As Integer
Dim ExitCode As Integer
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process

ProcessInfo = New ProcessStartInfo("cmd.exe", "/C " + Command)
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False
Process = Process.Start(ProcessInfo)
Process.WaitForExit(Timeout)
ExitCode = Process.ExitCode
Process.Close()

Return ExitCode
End Function

To use it in your example:

Put the function outside of your sub (like right above it) and put the following line where you'd like the code to execute.

ExecuteCommand("REN C:\Document.rtf YES.rtf",100)

You can check the return value (0 for success) to see if it was successful.

To do without using the command line:

Change your line that saves to file to the following:

FileUpload1.SaveAs("C:\Inetpub\wwwroot\upload\" & _
                   "myFile.txt")
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you Jeff. Where in my script should I insert this?
I would avoid calling a command line option and renaming the file in the code instead... I will add this to my answer
Thanks again Jeff. I probably used a bad example, thanks for the rename tip also. I want the VBscript to send a command to our workflow Server, which will then take over, and do other clever things with the uploaded file. I have a program raiseevent.exe which can be called in cmd. The raiseevent.exe can tell my workflow server that a upload has arrived.
I keep getting a runtime error Server Error in '/' Application. Runtime Error Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
I really don't know much about the error, try to put the tag recommended on your error page in your web.config file so you can see the error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.