Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Scenario:

Have two files in "C:\TestFolder\"
->Batch.bat
->Command.cmd

Question:

How do i run these files?

What i tried so far and failed

PHP
Method1
$shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host");
$exePath = ("C:\TestFolder\Batch.bat");//also tried Command.cmd
$shell->exec($exePath);

Method2
$result = `C:\TestFolder\Batch.bat`;//also tried Command.cmd

Method3
exec("C:\TestFolder\Batch.bat", &$Result);//also tried Command.cmd

Method4
system("cmd /c C:\TestFolder\Batch.bat");//also tried Command.cmd
Posted
Updated 28-Oct-13 8:24am
v2
Comments
ZurdoDev 28-Oct-13 14:56pm    
Why would you want to? Browsers won't let you run files like that, for security reasons.
amarasat 28-Oct-13 15:39pm    
Ok, this code is not on a browser!! An xml http request will be posted to a php server file and the server php file will run this batch file or command file.

This batch or command file will inturn launch a .exe application file.

Do you still think this is a browser issue?
ZurdoDev 28-Oct-13 15:41pm    
You still want web code to run a local file.

What doesn't work about what you have tried? Errors??
amarasat 28-Oct-13 15:43pm    
No errors!! nothing happens!! cmd.exe shows up in task messenger in all cases and hangs up the webpage.
ZurdoDev 28-Oct-13 16:08pm    
You should see the UI of cmd then. Can you see what is happening?

1 solution

Don't use "WScript.Shell". Simply use exec("MyBatch.bat"), or use any other executable file name. Please see:
http://php.net/manual/en/function.exec.php[^].

Better don't use absolute paths. Use the path relative to the page. There can be another problem: HTTP servers run applications in the sandboxed environment which won't allow to access any files beyond the root directory reserved for your side. Most likely, directory like "C:\TestFolder" is not legitimate.

—SA
 
Share this answer
 
Comments
amarasat 28-Oct-13 16:30pm    
No, it didnt work. The same thing happens. cmd.exe is launched up in taskmessenger and the webpage hangs up and nothing happens after that. The xml http request hangs up.

I have already went through the manual and tried everything there. Nothing happens everything hangs up the page.
Sergey Alexandrovich Kryukov 28-Oct-13 16:34pm    
Did you move the file from "C:\TestFolder" to some location under the root?
Also, why CMD.EXE has been launched and how do you know that. It all has nothing to do with "CMD.EXE", which is just another application you should NOT use. And what's inside your batch? Of course the batch can be blocking, no doubt about that...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900