Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Executing a batch file using ASP running Windows Server 2003

Posted on 2004-10-18
6
Medium Priority
?
1,154 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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