Solved

Executing a batch file using ASP running Windows Server 2003

Posted on 2004-10-18
6
1,134 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
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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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