By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:21**********************************@microsof t.com...
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

Nov 18 '05 #2

P: n/a
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>
===========================================
Nov 18 '05 #3

P: n/a
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
<danl@drawnet_no_spam.com> wrote in message
news:58**********************************@microsof t.com...
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);
===========================================

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>
===========================================

Nov 18 '05 #4

P: n/a
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
<danl@drawnet_no_spam.com> wrote in message
news:E7**********************************@microsof t.com...
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
<danl@drawnet_no_spam.com> wrote in message
news:58**********************************@microsof t.com...
> 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);

> ===========================================
>> 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>>
===========================================

Nov 18 '05 #5

P: n/a
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
??
Nov 18 '05 #6

P: n/a
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.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:CF**********************************@microsof t.com...
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>
??

Nov 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.