Link to home
Start Free TrialLog in
Avatar of jmestep
jmestep

asked on

Run robocopy .bat file from .net web page

We have a robocopy routine that executes every 1/2 hour and synchronizes the files on our load balanced web servers. One of the web designers wants to be able to see the results of his changes without waiting- he has ftp access to Server #1, from where the files get copied to Server #2.
Is it possible to execute the robocopy.bat file from a button on a web page? I saw some solutions in C#, but none in VB.
Thanks
Avatar of GaryFrancisLond
GaryFrancisLond

This can be accomplished using the following function

C#
public void ExecuteBatchFile(string filename)
{
  // Define a new process
  System.Diagnostics.Process proc;

  // Start the filename you have specified as this new process
  proc = System.Diagnostics.Process.Start(filename);

  // Wait for the batch file to finish
  proc.WaitForExit();
}

VB.Net
Public Sub ExecuteBatchFile(ByVal filename As String)
  ' Define a new process
  Dim proc As System.Diagnostics.Process

  ' Start tge filename you have specified as this new process
  proc = System.Diagnostics.Process.Start(filename);

  ' Wait for the batch file to finish
  proc.WaitForExit
End Sub


Regards,

Gary Francis
Software Developer
*** URL removed by humeniuk PE ***
Avatar of jmestep

ASKER

One more requirement-
The pages on this website don't use codebehind, so I have to do it with inline code using a button click to start it.
How would I incorporate that?
I've looked for example, but haven't found any so far.
Thanks
Here is an example .aspx page that basically only has 1 ASP Button with an onclick event that runs the above code:

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ExecuteBatchFile("C:\Test.Bat")
    End Sub
   
    Private Sub ExecuteBatchFile(ByVal filename As String)
        ' Define a new process
        Dim proc As System.Diagnostics.Process

        ' Start tge filename you have specified as this new process
        proc = System.Diagnostics.Process.Start(filename)

        ' Wait for the batch file to finish
        proc.WaitForExit()
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
    </form>
</body>
</html>

You can then build the rest of your page around the above example.

Regards,

Gary Francis
Software Developer
*** URL removed by humeniuk PE ***
Avatar of jmestep

ASKER

I've got the code in and it didn't break the page and I think it's finding the correct file, because if I code in a non-existent file, it says it can't be found.
I think now it's a matter of permissions. I've given ASPNET read and execute, then I tried Everyone with full access permissions on the bat file, but it doesn't copy over. Do you have any suggestions?
Where is the application running? Is it a server/desktop with IIS? What version of Windows? What version of .Net Framework?

I will also investigate other potential problems.

Gary
I have tested this on my server and my code seems to run the file fine.

What I would sugges you do is add a "pause" line into your batch file and check to see any error messages that appear, you may find something in this. Then you just need to monitor this on the web server and step through the pause lines when the batch file executes.

Post back if you need some more information.

Regards,

Gary Francis
Software Developer
*** URL removed by humeniuk PE ***
Avatar of jmestep

ASKER

The file and page are on a Windows Server 2003 Web Edition. I'll check with the person who wrote the batch file to see about putting a pause into it.
Thanks
ASKER CERTIFIED SOLUTION
Avatar of GaryFrancisLond
GaryFrancisLond

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jmestep

ASKER

Thanks,
I'll check into that. The bat file has been running thru the scheduler or manually by itself, I'm just trying to get it set up to run from a web page so we can do it on demand without giving someone else logon permissions to the servers.
Avatar of jmestep

ASKER

I haven't got this completed yet, but I don't want to leave the question open and I think it's a permissions problem now.