.bat from asp.net w/System.Diagnostics.Process?

G

Guest

I need to be able to execute a .bat file from a C# web application
I have the following code that compliles and seems to run fine, but the bat file never does it's work
===================================
System.Diagnostics.Process p = new Process()
p.StartInfo.RedirectStandardOutput=false
p.StartInfo.FileName = "C:\\ftp_scripts\\script.bat"
p.StartInfo.UseShellExecute=true
p.Start()
p.WaitForExit()
p.Dispose();
====================================
The bat file works fine when i don't call it from the web app

What could be wrong?
Is it a permissions thing?
Why am I not getting any errors?
How do I get errors back to my calling code

Any help would be greatly appreciated
-Dan
 
A

Alvin Bruney [MVP]

how do you know the bat file is not working? Are you testing the results of
what the bat file executed to know this? You cannot run the application and
look for a bat file window. If you insist that the bat file is not firing
off, here is another way to invoke it using the command line modifying your
code.

System.Diagnostics.Process p = new Process();
// p.StartInfo.RedirectStandardOutput=false;
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardInput=true;

p.StartInfo.FileName = @"cmd"; //"C:\\ftp_scripts\\script.bat";
// p.StartInfo.UseShellExecute=true;
p.StartInfo.UseShellExecute=false;

p.Start();
// p.WaitForExit();
// p.Dispose();

StreamWriter sIn = myProcess.StandardInput;
sIn.AutoFlush = true;
StreamReader sOut = myProcess.StandardOutput;

sIn.Write(@"C:\ftp_scripts\script.bat" + System.Environment.NewLine);
sIn.Close();

string results = sOut.ReadToEnd().Trim();
p.Close();

The results of the script, whether it failed to run or errored out will be
contained in results variable so you can know exactly what is going on.
 
G

Guest

Thanks for your help Alvin, but I still can't get the sript to run. I tried to simplify the code by executing the command that was in the bat file directly from this script, still no luck.

The cmd simply takes a file and ftp's it to another server. the output is recorded in a log file. If I execute the cmd from the cmd line, it works fine (the file is moved, and there is an entry in the log file)

When I execute it with the following script from an .aspx, no file moved and no log entry.

I'm stumped!! Any other idea's why this might not be working

======================================
System.Diagnostics.Process p = new Process()
p.StartInfo.RedirectStandardOutput=true
p.StartInfo.RedirectStandardInput=true
p.StartInfo.FileName = @"cmd"
p.StartInfo.UseShellExecute=false
p.Start()
StreamWriter sIn = p.StandardInput
sIn.AutoFlush = true
StreamReader sOut = p.StandardOutput

sIn.Write(@"ftp -s:c:\ftp_scripts\ftp.txt >> c:\ftp_scripts\ftp_log.txt" + System.Environment.NewLine)
sIn.Close()

string results = sOut.ReadToEnd().Trim()
p.Close()

Response.Write(results)
==========================================

OUTPU
==========================================
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >> c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>
===========================================
 
A

Alvin Bruney [MVP]

what is the results of the script from the command prompt when you run using
a webpage?
i'm not familiar with the syntax of the ftp command you are using. from the
docs, ftp takes a verb followed by command line options. Post your script
code if you still have problems.
 
A

Alvin Bruney [MVP]

Great, so here is where you start. After each line in your script, echo it
to the screen. Then run the app. Just follow the screen output to find which
line is failing.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
The results of the script from the command prompt when run from an .aspx
are
====================
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft
Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >>>
c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>
====================
The ftp call syntax that I am using allows you to have a text file with
all of your ftp connection info in it. The contents of the
c:\ftp_scripts\ftp.txt file is
===================
open 11.22.33.44
uname
pword
cd mydir
put "C:\SQLData\MSSQL\BACKUP\test.txt"
quit
===================

I'd really like to figure out why my code won't run from a web page, but I
really just need to be able to ftp from an .aspx page. If you know of a
better way, I'm open to anything that works.

Thanks for your help.
-Dan


----- Alvin Bruney [MVP] wrote: -----

what is the results of the script from the command prompt when you run
using
a webpage?
i'm not familiar with the syntax of the ftp command you are using.
from the
docs, ftp takes a verb followed by command line options. Post your
script
code if you still have problems.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
Thanks for your help Alvin, but I still can't get the sript to run. I
tried to simplify the code by executing the command that was in the bat
file directly from this script, still no luck.
The cmd simply takes a file and ftp's it to another server. the
output is
recorded in a log file. If I execute the cmd from the cmd line, it works
fine (the file is moved, and there is an entry in the log file).
When I execute it with the following script from an .aspx, no file
moved
and no log entry.
I'm stumped!! Any other idea's why this might not be working?
=======================================
System.Diagnostics.Process p = new Process();
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.FileName = @"cmd";
p.StartInfo.UseShellExecute=false;
p.Start();
StreamWriter sIn = p.StandardInput;
sIn.AutoFlush = true;
StreamReader sOut = p.StandardOutput;
sIn.Write(@"ftp -s:c:\ftp_scripts\ftp.txt >>
c:\ftp_scripts\ftp_log.txt" +
System.Environment.NewLine);
sIn.Close();
string results = sOut.ReadToEnd().Trim(); p.Close();
===========================================
OUTPUT
===========================================
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft
Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >>>
c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>>
===========================================
 
G

Guest

ok, I think I've found a problem, but I'm not sure why it's happening

If I use the command prompt, from "C:\Windows\system32", if i type in ftp and hit enter the next line I get is "ftp>", I can then enter open 11.22.33.44 and it promts me for my uname.... and I'm on my way

When I execute the following code from the .aspx, the output shows
"Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\WINDOWS\system32>ftp C:\WINDOWS\system32>".

Where I would have expected
"Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\WINDOWS\system32>ftp ftp>"
-----------------------------------------------------------------
System.Diagnostics.Process p = new Process()
p.StartInfo.RedirectStandardOutput=true
p.StartInfo.RedirectStandardInput=true
p.StartInfo.FileName = @"cmd"
p.StartInfo.UseShellExecute=false
p.Start()
StreamWriter sIn = p.StandardInput
sIn.AutoFlush = true
StreamReader sOut = p.StandardOutput
sIn.Write(@"ftp" + System.Environment.NewLine)
//sIn.Write(@"open 11.22.33.44" + System.Environment.NewLine)
//sIn.Write(@"myuname" + System.Environment.NewLine)
//sIn.Write(@"mypword" + System.Environment.NewLine)
//sIn.Write(@"cd dbdump" + System.Environment.NewLine)
//sIn.Write(@"put ""C:\SQLData\MSSQL\BACKUP\test9.txt""" + System.Environment.NewLine)
sIn.Write(@"quit" + System.Environment.NewLine)
sIn.Close()
string results = sOut.ReadToEnd().Trim()
p.Close()
Response.Write(results)
----------------------------------------------------------------
(NOTE that the commands after ftp are commented out

Why from the .aspx page is it going back to
C:\WINDOWS\system32
and not to
ftp
??
 
A

Alvin Bruney [MVP]

you may have to put a "ftp" followed by a newline command so that it is
sitting in the ftp prompt before you start firing your command line params.
That should fix it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top