?
Solved

Executing a batch file using ASP running Windows Server 2003

Posted on 2004-10-18
6
Medium Priority
?
1,147 Views
Last Modified: 2008-01-09
I am trying to create an ASP page that will cause a batch file to be executed on my Internet Server running Windows Server 2003. I am familiar with, and have attempted to use the following services:

Option 1:
set newobj=createobject("wscript.shell")
newobj.Run "cmd.exe /c C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat"

I have also tried a variation to the above that seems to be the only codeset that actually does something - but it just seems to hang forever, and the batch file is never run - but the variation is:
newobj.Run("cmd.exe /c C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat", 0, True)

Option 2:
Using ASPExec from www.serverobjects.com
Set Executor = Server.CreateObject("ASPExec.Execute")
Executor.Application = "cmd.exe /c"
Executor.Parameters = "C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat"
strResult = Executor.ExecuteDosApp

Option 3:
Using DynuExec from www.dynu.com
Set myexec = Server.Createobject("Dynu.Exec")
Response.Write(myexec.execute("C:\InetPub\wwwroot\SBO\admin\BCI\unzip.bat"))

All of the above code does absolutely nothing. ( With the exception of adding the ", 0, True" to Option 1 variation 2 above - and again - it just hangs ).

I have successfully used these services ( with the exception of Dynu ) on previous versions of Windows ( 2000, NT, etc. ).

I have even opened security much wider than I would like by setting full control permission to the following files:
unzip.bat
cmd.exe
aspexec.dll
dynuexec.dll

In fact, I have even given full permissions for the following users on each of the above files:
Internet Guest Account
Interactive
Launch IIS Process
IIS_WPG
Administrators
Everyone

The contents of my batch file executes perfectly using the Command Prompt Window logged in as Administrator or when double clicked from the explorer window, but when called within the ASP page - nothing - no error - no activity - no hang up - nothing. Result from Shell produces no visible result when called by Response.Write().

The contents of my batch file is as follows - although I seriously doubt that this is even being called due to the nature of the problem.
c:\progra~1\winzip\wzunzip -n C:\Inetpub\wwwroot\SBO\admin\BCI\BB2ANE4.upd c:\inetpub\wwwroot\sbo\admin\BCI\Unzipped\
c:\progra~1\winzip\wzunzip -n C:\Inetpub\wwwroot\SBO\admin\BCI\BE620.UPD c:\inetpub\wwwroot\sbo\admin\BCI\Unzipped\

Can anyone help?

Thanks in advance!
0
Comment
Question by:CyberQuestions
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 12344033
How about using Windows scheduler?
Check out this PAQ of earlier today:
 http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21172502.html

-tushar
0
 

Author Comment

by:CyberQuestions
ID: 12344080
Thanks for the fast reply tushar.

I have though about that or just running the batch using a Windows Scheduler and for some applications this is a decent workaround - however, this is intermittently timed and when requested, need to display the results of the batch file.
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 2000 total points
ID: 12348300
Run a .BAT file from ASP.NET

Okay, running .BAT files from ASP.NET using  the System.Diagnostics.Process object and static methods this should be easy, right?  Well, this might work for you, but it certainly won't work on my machines. And after doing lots of reasearh on the issue, it seems that other people are also having problems with this too.  

I wrestled with permissions and all sorts of other stuff, trying to get a simple batch file to run, with no luck.  I tried lauching the bat file directly, launching cmd.exe and calling the bat file using stin. No dice.  It seems that something on my machine was keeping an unattended process from running bat files.  This makes sense, but I was never able to pinpoint what was preventing this, so I came up with a workaround.

I realized that since I could sucessfully run cmd.exe, and send commands to it via stin, I could just open the batch file, and send each line to cmd.exe, which is essentially the same as running a batch file itself.  This technique works great, and I thought I'd pass along the code here.

// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;

// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;

// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);

// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);

// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;

// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;

// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}

strm.Close();

// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";

sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");

// Close the process
proc.Close();

// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();

// Close the io Streams;
sIn.Close();
sOut.Close();

// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));

http://dotnetjunkies.com/WebLog/bsblog/archive/2004/05/13/13484.aspx

Aeros
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question