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
ASP.NET

Avatar of undefined
Last Comment
jmestep

8/22/2022 - Mon
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 ***
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
GaryFrancisLond

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 ***
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
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?
GaryFrancisLond

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
GaryFrancisLond

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 ***
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
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
GaryFrancisLond

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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.
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck