Link to home
Start Free TrialLog in
Avatar of oswaldbmk
oswaldbmk

asked on

Open the batch file in ASP.Net

I had face the problem to open the batch file as the processing still on. Why?
I had attached the coding below,

Dim myprocess As System.Diagnostics.Process = New System.Diagnostics.Process()
myprocess.StartInfo.FileName = "c:\mybat.bat"
myprocess.StartInfo.WorkingDirectory = "C:\"
myprocess.Start()

The command in the mybat.bat file is as the following
ftp -s:payroll.par lutc28
pause
?If you know how to solve it, pls help me..
ASKER CERTIFIED SOLUTION
Avatar of AsbjornG
AsbjornG

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of oswaldbmk
oswaldbmk

ASKER

Although the myprocess.Start() return true ,it doesn't work.
I want to command.exe to be prompt out and let the user to response in order to continue.
So, i do'nt know whether the bat file is executed or not?
is that any other solution to solve it..
Thx.

oswaldbmk,

adding the following line MAY open the command prompt (I'm saying may because I do not know whether or not a command prompt will ever be opened from a program run on a webserver. It will definitely run the program, though.).

myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized; ' Will run the window maximized

The all you have to do is wait for the user to close the window:

myProcess.WaitForExit();

Asbjorn
for the wait for exit, i try it before, but the web is till running and hang infinitely,
is that any setting need to be set?
i try the javascript also,
function openNotepad() {
                var oShell = new ActiveXObject("Shell.Application");
      var commandtoRun = "C:\payroll.bat";
      var commandParms = "";
      oShell.ShellExecute(commandtoRun, commandParms, "", "open", "2")
}
the error message is talk about the permission denied!
do u have any better idea?
Oswaldbmk,

Because I am doing quite a lot with my batch file, I decided to just do it in the command prompt in the end. It will wait for the batch file to complete execution, and it will also return any errors.

You can see that the batch file has been executed by the output on your screen after running the script.


This solution requires System.IO, system.Net and System.Threading namespaces.
---------------

dim myProcess as Process = new Process()  ' Declare New Process object


myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As StreamWriter = myProcess.StandardInput
sIn.AutoFlush = True
Dim sOut As StreamReader = myProcess.StandardOutput
Dim sErr As StreamReader = myProcess.StandardError
sIn.Write("c:" & System.Environment.NewLine)
Thread.Sleep(500)
sIn.Write("payroll.bat" & System.Environment.NewLine)
' Added thread methods to wait half a second between commands.
Thread.Sleep(500)
sIn.Write("exit" & System.Environment.NewLine)
'----------
MyProcess.WaitForExit()
sIn.close

response.write("<pre>" & sOut.ReadToEnd() & "</pre>" )

dim strErrors as string = sErr.ReadToEnd()
sErr.close


      if strErrors.Length > 0 then
            response.write("<br><br>Errors:<pre>" & strErrors & "</pre>" )
      End If
      
sOut.close
myProcess.Close


----------------

I would also like to request that you up the grade from C. Or, if you thoroughly believe that I deserve a C grade, please explain what you are not happy about so that I can improve my answers in the future.

Cheers,

Asbjorn
Errors:
'payroll.bat' is not recognized as an internal or external command,
operable program or batch file.
this is the erorr that i found in my application.

sorry about the grading!actually i'm new memberin this side,so i don;t knoe about the grading standard. how to upgrade the grade?sorry for everything?
Oswaldbmk,

The program cannot find payroll.bat, and come to think about it I removed the line to actually change to the correct directory! Sorry about that.

If you add this:

sIn.Write("cd \" & System.Environment.NewLine)
Thread.Sleep(500)

right after this:

sIn.Write("c:" & System.Environment.NewLine)
Thread.Sleep(500)

the current working path should be changed to c:\ and the file should be located.

I was a bit confused about grading as well at first, so no problem at all :). Generally, if someone takes the time to help you out and solve your problem the grade should be A. If You get decent answers but not spot on, it should be B. If you get a bit of help but it's only enough to help you a little on the way, you could consider using grade C. You could also have a look at the help section (link in the top right corner), EE has a very neat and easy to understand system.

To change a grade, please make a post in the community support forum https://www.experts-exchange.com/Community_Support/ 

Explain that you would like to change the grade, and leave a link to this thread. An administrator should take care of the rest.

Cheers,

Asbjorn
i c the result oledi,it displays in my browser.
but, do u have any idea in order to prompt the command.exe out to the screen?
It is because i need to get the user response.
the command inside my bat file is below:
ftp -s:payroll.par lutc28
pause

thx a lots
I'm afraid I don't know any way to do that, except getting the input before running the batch file at all.

You may want to look at "virtual FTP" solutions instead, programs that try to simulate a FTP program through HTTP.

Other than that, I'm afraid it would be very difficult to get the users input while running the batch file (but nothing is impossible, just takes more time!)

Cheers,

Asbjorn
i think if u can really help me in the Q, i exactly will give u a A grade.